File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection 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 collection" Watch "garbage collection" New topic

garbage collection

durga krishna

Joined: Jan 19, 2001
Posts: 17
Hi all
what do u mean by this?
Circular references do not prevent objects from being garbage collected.
Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi durga..
can u give some example code with ur ques pls??


Hima<BR>Sun Certified Java Programmer
Revati Pathak

Joined: Feb 19, 2001
Posts: 26
Try This Example
// ******************
class Test1 {
public static void main(String arg[]) throws Exception{
MyObj a = new MyObj("a");
MyObj b = new MyObj("b");

//circular reference here
a.o = b;
b.o = a;
a = null;
b = null;
// a and b are both eligible for GC now

MyObj c = new MyObj("c");
c.o = new MyObj("d"); //when c dies, d dies too.
c = null;

// an infinite loop here
// use ^c to kill it

MyObj[] objAry = new MyObj[1024];
int i = 0;
while (true){
// suggest JVM to GC

// use more memory here
i %= 1024;
objAry[i] = new MyObj("X" + i);

Thread.sleep(5); // Give CPU some breath time
class MyObj {
MyObj o;
String s;
long[] ary = new long[4096]; // make MyObj big

MyObj(String s) {
this.s = s;

protected void finalize() throws Throwable {
// Make GC visible
System.out.println(s + ": I am dying");
// ******************

[This message has been edited by Revati Pathak (edited March 27, 2001).]
I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!