File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collector" Watch "Garbage collector" New topic
Author

Garbage collector

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
Vector v1 = new Vector()
Vector v2 = new Vector()
v1 = null//object v1 is eligible for gc
Vector v3 = v1;//object v1 is not gced becoz v3 has reference
v1 = v2 // object v1 is eligible for gc here once again
correct me..
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Vector v1 = new Vector() // 1st Vector object is created in heap
Vector v2 = new Vector() // 2nd Vector object is created in heap
v1 = null // 1st Vector object is eligible gor GC
Vector v3 = v1; // v3 ref is null now
v1 = v2 // v1 points to 2nd Vector Object
// Finally only 1st Vector obj is eligible
// for GC. 2nd Vector object is ref.d by
// both v1 and v2 not eligible for GC

regds
maha anna

[This message has been edited by maha anna (edited February 20, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The key here is that v1 and v2 are not objects, they are references. References are never collected, but the objects they used to refer to may be.
Vector v1 = new Vector()
Vector v2 = new Vector()
Now reference v1 refers to one Vector object, and v2 refers to another Vector object.
v1 = null//object v1 is eligible for gc
The Vector which was previously (originally) referenced by v1 is eligible for collection.
Vector v3 = v1;//object v1 is not gced becoz v3 has reference
Reference v1 is null, so now reference v3 is null also. The Vector which became eligible for GC on the last line is still eligible, as there are no references to it anywhere, and never will be again.
v1 = v2 // object v1 is eligible for gc here once again
Reference v1 now refers to the same Vector object that v2 does, which has never been eligible for collection (and certainly is not now, because it has two references to it.)

[This message has been edited by Jim Yingst (edited February 21, 2000).]


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collector
 
Similar Threads
About gc
arrayOutOfIndex
Help please
Help with Objects..
doubt on garbage collection