-1

I know that static variables persisted across the life of the application if the they are not de-referenced.

But are static variables garbage collected if they are de-referenced? Or do I just not know what I'm talking about?

Thanks

edit

Okay I think I get it. There are no static objects per se, there are static references. Static references persist throughout the application life-cycle, and are never garbage collected. Therefore, any object they point to will not be garbage collected. Additionally, all non-static references that go out of scope will be garbage collected.

Is my understanding correct?

Thanks again

user207421
  • 305,947
  • 44
  • 307
  • 483
the_prole
  • 8,275
  • 16
  • 78
  • 163
  • 1
    Possible duplicate of [Are static fields open for garbage collection?](https://stackoverflow.com/questions/453023/are-static-fields-open-for-garbage-collection) – Tom Jan 21 '18 at 03:14
  • Check this out : https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/ – Srikanth Jan 21 '18 at 03:16
  • 2
    *"I know that static variables persisted across the life of the application if the they are not de-referenced."* - This statement is wrong. Reachability (and GC) are not directly related to the action of following a pointer (dereferencing). – Stephen C Jan 21 '18 at 03:19
  • 2
    Variables are *never* GC'd. Only objects are. That you're asking this in this way *is* a concern. – Hovercraft Full Of Eels Jan 21 '18 at 03:31
  • 1
    Yes indeed. (I hadn't noticed ...) This mistake suggests that you don't understand the difference between a variable and an object. That is *really* concerning. – Stephen C Jan 21 '18 at 04:43
  • @Stephen C what do you mean reachability is not directly related to dereferencing? Yes, I do understand the differenece between an object and a variable. – the_prole Jan 21 '18 at 10:29
  • I mean that you don't need to dereference a reference for the object or the reference to it to be reachable. – Stephen C Jan 21 '18 at 10:38
  • 2
    *"Yes, I do understand the differenece between an object and a variable."* - So why did you ask if a variable can be garbage collected? (If you really understand the difference, then that question is plainly nonsensical.) – Stephen C Jan 21 '18 at 10:41
  • Okay, so if there are no refereneces pointing to an object, it is unreachable. This is what I'm saying. This statement is correct. How should I know what happens to refereneces? If someone said say hey, references are basically objects pointing to other objects and get garbage collected, it would make sense to me. So it natural for one to wonder if maybe variables are also garbage collected. And yes, I know refereneces don't have references like they can in c, but they do have scope, so there is some underlying concept of them being "reachable" – the_prole Jan 21 '18 at 17:30
  • In other words, as that nonsense would make sense to you, you don't understand the difference at all. References are not objects. What hapoens to references is that they go out of scope in the case of local variables, or disappear when the containing object is GC-d in the case of instance members, or disappear with the class in the case of static variables. – user207421 Jan 21 '18 at 23:03
  • Right, but if the reference is static, is persists in the heap, even if it does not point to an object. No they're not objects, but they are contiguous sequences of bytes, and they do exists either in the heap or the stack. So it's not really nonsense to think of them as having similarities to objects, even if they aren't technically objects per say. – the_prole Jan 22 '18 at 01:27
  • Wrong. It is nonsense to think of references as having similarities to objects, and specifically it is totally meaningless to talk about them being garbage-collected. The *objects containing them* can be garbage-collected. It is also nonsense to speak of references that don't point to objects. You can sensibly talk about reference *variables* whose *value* is *null,* which is *instead of* containing a reference to an object. Not the same thing. – user207421 Jan 22 '18 at 20:37

2 Answers2

1

Objects reside in Heap, references can reside in heap or Stack which is in non heap area. When an object is marked by the garbage collector as non reachable it is eligible for garbage collection. References can be marked as static, the object is just object.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
1

Variables aren't garbage collected, and aren't dereferenced either. Objects are garbage-collected, maybe, if there are no references. static really has nothing to do with it.

EDIT

Okay I think I get it.

Let's see.

There are no static objects per se, there are static references.

Correct.

Static references persist throughout the application life-cycle, and are never garbage collected.

Incorrect. Static references are held in class objects. Those class objects can be garbage-collected under some circumstances. It is meaningless to speak of references being garbage-collected.

Therefore, any object they point to will not be garbage collected.

There is no 'therefore' about it. An object is eligible for garbage-collection when there are no more references to it. Zero. One object containing a reference to it being garbage-collected is necessary but not sufficient. There have to be no other references, and the garbage-collector has to actually reach the object.

Additionally, all non-static references that go out of scope will be garbage collected.

Incorrect. It is meaningless to speak of references being garbage-collected.

Is my understanding correct?

No.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks. What do you mean static refereneces are held in class objects which can be garbage collected under some circumstnaces? I know what a class and an object is, but I've never heard of a class object. All I know about static references, is they go in the heap, not the stack like automatic variables. – the_prole Jan 23 '18 at 03:00
  • 1
    @the_prole the JVM has to keep contents of the `static` fields as well as meta information in memory. You may call this an object or not. But then, there is the [`Class`](https://docs.oracle.com/javase/8/docs/api/?java/lang/Class.html) object on the Java side, which *may* get garbage collected (if it has been loaded through a custom class loader and the class loader itself becomes unreachable). When a `Class` object is collected, all of its associated JVM side data will get freed too, which includes the `static` fields of the class. – Holger Jan 23 '18 at 12:50