-1

I think if an object gets created locally within a method and is never leaked outside then that object will be eligible for garbage collection as soon as that method executed is completed.

But I am confused because I read that there are 4 GC roots - and one of then is the thread object, so I am confused that is it the case that method local objects will become eligible for garbage collection only when the thread in which it got created is complete and the GC Root of that thread is taken off?

Also, if someone can throw some light on how method local objects are linked to stack area of the heap.

PLEASE NOTE that I have read about GC of method local objects but nowhere I got a clear and detailed answer so I raised this question.

pjj
  • 2,015
  • 1
  • 17
  • 42
  • 1
    Why are you confused? You haven't shown any code. But when the last reference of an object goes out of scope **it is *eligible*** for GC. The method that it is in (or that it is even in a method) is immaterial. It could be in a block in a method. **Count** the references. When the count is 0 it is eligible for garbage collection. Exactly when the references will be counted is an implementation detail. – Elliott Frisch Mar 13 '22 at 16:37
  • @ElliottFrisch I'm not aware of any JVMs that implement garbage collection using reference counting. – boneill Mar 13 '22 at 16:52
  • @boneill But a perfect garbage collector **could**. I interpret the question as when "objects will become eligible for garbage collection"; and that's when they're eligible. – Elliott Frisch Mar 13 '22 at 17:12

2 Answers2

2

Think of the thread call stack as a doubly linked list of stack frame nodes. Each node references all the local variables for a given method, and these in turn reference any live objects. The GC root for the thread is essentially the head node of the linked list, and garbage collection traces the nodes that are still linked, thus finding all live objects. When a method returns, the tail node of the linked list is removed, and thus it's not discoverable by GC anymore.

In practice, the call stack isn't implemented this way, but the behavior is essentially the same.

boneill
  • 1,478
  • 1
  • 11
  • 18
2

Method local objects are eligible for garbage collection as soon as they stop being used, whether or not the method is finished.

Methods really have almost nothing to do with garbage collection.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Ok. But if method is not finished then how JVM can decide whether object will be used further or not? – pjj Mar 13 '22 at 20:05
  • @pjj The object reference must go away. Either all variables that refer to it must be set to null, or the variables must be set to reference something else. – boneill Mar 14 '22 at 00:20
  • The compiler notes the range in which a local variable is present, by reading the entire method. It's not like it's only reading the method as it executes it. – Louis Wasserman Mar 14 '22 at 01:52
  • 2
    @pjj the JVM knows what a method is doing. A JVM may even change what a method is doing (as long as the semantics do not change), which is what optimizations are all about. See [Can java finalize an object when it is still in scope?](https://stackoverflow.com/q/24376768/2711488) – Holger Mar 14 '22 at 08:41