Garbage collecting on nulls.......

R

Roger Irwin

Java documentation states that objects are garbage collected when a
variable goes out of scope, or explicitly released by setting all
references to null.

I assume that this also means they will be garbage collected when a
reference is changed to point at some other object (and nothing else
points to the object).

Put another way, is the spec really trying to say "Objects will be
garbage collected when there is no reference pointing at them".
 
M

Marc Rochkind

Java documentation states that objects are garbage collected when a
variable goes out of scope, or explicitly released by setting all
references to null.

I assume that this also means they will be garbage collected when a
reference is changed to point at some other object (and nothing else
points to the object).

Put another way, is the spec really trying to say "Objects will be
garbage collected when there is no reference pointing at them".

There is a distinction between "garbage collected" and "eligible for
garbage collecting." What you say in your last paragraph is true about
eligibility.

When and if garbage collection occurs is up to the system, although you can
use System.gc() to force a collection.

--Marc
 
B

Brooks Hagenow

Marc Rochkind said:
There is a distinction between "garbage collected" and "eligible for
garbage collecting." What you say in your last paragraph is true about
eligibility.

When and if garbage collection occurs is up to the system, although you can
use System.gc() to force a collection.

The last sentence is not entirely true. Calling System.gc() is not required
to do anything. It is really just a recommendation but the JVM is not
required to do anything.

One book I would recommend is "Sun Certified Programmer & Developer for Java
2 Study Guide (Exam 310-035 & 310-027)" by Kathy Sierra and Bert Bates.
Kathy Sierra is actually a co-developer of the SCJP 1.4 exam. Even if you
don't plan on taking the exam I think this book would be very helpful.

Anyway, the answer to the original question starts on page 430. The part on
System.gc() is on page 437.

"Theoretically, after calling System.gc(), you will have as much free memory
as possible. We say theoretically because this routine does not always work
that way. First, the JVM you are using may not have implemented this
routine; the language specification allows this routine to do nothing at
all. Second..."

-Brooks
 
D

Dario


No.

An object may be garbage-colllected
even if there are references pointing to it.
The true important thing
is the fact that the object is unreachable,
not the fact that there are references to it!

E.g. Each iteration of the following program creates
a 1-million objects in a circular-list.
Each object in the list has a reference to the next one
and to the previous one.

If you launch the application with the OFF argument
then after few iteration (3 or 4 on my PC)
the memory is exausted because the garbage-collector
is never called.

If you launch the application with the NULL argument
then in each iteration the 1-million object in the
circular-list becomes unreachable EVEN IF
each one of these 1-million objects still have references to them.
In this case (objects with references to them but unreachables)
the garbage-collector may be invokec so the program never crash
for OutOfMemory exception.

Try it....
.... and learn the difference
between "unreachable" and "references to it" !

- Dario

class Java46 {
Java46 next;
Java46 prev;

Java46(int n) {
this(null);
while(--n > 0)
new Java46(this);
}

Java46(Java46 j46) {
if(j46 == null) {
next = this;
prev = this;
} else {
next = j46.next;
prev = j46;
j46.next.prev = this;
j46.next = this;
}
}

public static void main(String argv[]) {
if(argv.length != 1 ||
(! argv[0].equals("NULL") && !argv[0].equals("OFF"))) {
System.out.println("usage: java Java46 NULL|OFF");
return;
}
Java46 j[] = new Java46[1000];
for(int i=0; i<j.length; i++) {
j = new Java46(1000*1000);
// Now there are 1 million objects
// in the circular list pointed by j.
// Each object in the circular list is pointing
// to the prev and to the next.
if(argv[0].equals("NULL")) {
j = null;
// Now the 1 million objects are not reachables
// but they still are referenced by next and prev:
// so they may be garbage-collected!
}
System.out.print(" "+i);
}
}
}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top