Garbage collection question?

K

Knute Johnson

John said:
An object automatically becomes eligible for garbage collection when it
ceases to be reachable from any live thread via a chain of strong
(normal) references. That is the only criterion. Modern VMs typically
do not monitor assignment statements to make that determination; instead
they periodically trace reference chains to find which objects need to
be /kept/.

In the example you present, the Object created at the top of each loop
iteration becomes eligible for GC as soon as a new reference is assigned
to variable o, *provided that* the old Object's reference has not been
assigned to some object that persists across loop iterations. (For
example, if the loop added the object to a List then the Object would
not become eligible for GC before the List did.)



Local variables occupy space on the stack, but the number of local
variable slots in a stack frame is determined by the compiler. Almost
certainly, each iteration of the loop will use the same slot to
represent variable o. Even if different slots were used, the stack
frame size is fixed at compile time, so an infinite loop will quickly
have to start reusing variable slots, so you will not get a memory leak
from this direction.



When a thread returns from a method, its stack frame for that method
invocation is popped, freeing all the memory occupied by the local
variables of that method for that invocation. This isn't garbage
collection per se, but I think it addresses your concern even better.

Thanks John and everybody else that answered. I appreciate the time and
effort for my enlightenment.
 
H

HalcyonWild

Knute said:
HalcyonWild said:
But it does not even compile, forget about being collected by GC.

public class test {
static void method(Object o) { ; }

public static void main(String[] args) {
method(new Object());
}
}


Knute, you earlier said this in your original post. -----
If I have a method and that method has as a parameter, a new Object()
(no reference variable), will that Object get marked for garbage
collection after the method returns? Or ever?


method(new Object()) { ... }

-------[end of yoru original post]

Now, where is the semicolon here. There is a difference in your
original post and this post above. Here you are calling the method, and
in your original post you were defining the method.
Well, you got me confused. Anyway, in this case, I dont know if new
Object in the method call, would be GCed after the caller exits, or the
called exits.
 
K

Knute Johnson

HalcyonWild said:
Knute Johnson wrote:

HalcyonWild said:
But it does not even compile, forget about being collected by GC.

public class test {
static void method(Object o) { ; }

public static void main(String[] args) {
method(new Object());
}
}



Knute, you earlier said this in your original post. -----
If I have a method and that method has as a parameter, a new Object()
(no reference variable), will that Object get marked for garbage
collection after the method returns? Or ever?


method(new Object()) { ... }

-------[end of yoru original post]

Now, where is the semicolon here. There is a difference in your
original post and this post above. Here you are calling the method, and
in your original post you were defining the method.
Well, you got me confused. Anyway, in this case, I dont know if new
Object in the method call, would be GCed after the caller exits, or the
called exits.

The ... means some statements. It is pseudo code, not complete but only
there for example. To make the example compilable it needed a
statement. ; is the shortest statement possible.
 
K

Knute Johnson

Roedy said:
the problem with that notation now is that sometimes it is
meaningful, for the array constructor shortcut.

I guess "// some statement" would have been better but - hindsight :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top