Garbage Collection
MyType myType = new MyType();
myType=null; // Line 1
arrayList.add(new String("waste"));
}
}
}
class MyType{
protected void finalize() {
System.out.println("bye");
}
}
output:
bye
Exception in thread "main" java.lang.OutOfMemoryError
Why this program is printing bye?
Why this program is printing bye followed by OutOfMemoryError?
java.lang.OutOfMemoryError:
Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
This Program demonstrates how JVM runs Garbage Collecter atleast once before giving error(OutOfMemoryError). Garbage Collector calls "finalize()" method before trying to delete Objects. Since no Objects can be deleted we get runtime error.
This program also demostrates how a strong reference stops an Object from being Garbage Collected.
output:
Exception in thread "main" java.lang.OutOfMemoryError
1 Comments:
after adding a SOP in while loop memory error doesn't come...
S T R A N G E ....
while(true)
{System.out.println(++i);
arrayList.add(new String("waste"));
}
Post a Comment
<< Home