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 garbagecollection 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 "garbagecollection" Watch "garbagecollection" New topic
Author

garbagecollection

Manu Kumar
Greenhorn

Joined: Aug 31, 2003
Posts: 8
class I {
private String name;
public void finalize() {System.out.print(name);}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1); //5
System.gc();
}
}

in this after line //5 there are three objects created at //2,3,4
objects eligible for garbage collection
while in the below question

class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1 = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1); //5
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}
in this no object is elligible for garbage collection after //5pls explain me


SCPJ2 aspirant
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
Originally posted by Manu Kumar:
class I {
private String name;
public void finalize() {System.out.print(name);}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1); //5
System.gc();
}
}

in this after line //5 there are three objects created at //2,3,4
objects eligible for garbage collection
while in the below question

class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1 = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1); //5
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}
in this no object is elligible for garbage collection after //5pls explain me

First you need to understand that you are not passing an object to m1 or a var referring to an object, but rather a copy of a var referring to an object.
In the first example the method m1 has a copy of a reference to your array object, but not the actual reference var created in the main method. You then take this reference and instruct it to take the array it is pointing to and nullify each of its three elements. Now once the method returns you no longer have live references to the objects that were referred to by the arrays elements (the objects created on lines 2,3 and 4) thus u have your 3 objects eligible for garbage collection.
In the 2nd example all m1 is doing is taking its copy of the var that points to your array and nullifying it, but you still have your original reference to the array in your main method, thus you still have a way to reach the array and the elements it contains. Therefore, you have no objects elgible for gc.
Don't be fooled by the fact that method m1 names its copy of the var a1. It is not the same var as in the main method but rather a local var that happens to share the same name. It could very easily have been abracadabra and you would obtain the same results
Hope that helps.
[ September 12, 2003: Message edited by: Damien Howard ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbagecollection
 
Similar Threads
Doubt in GC
Garbage Collection II
Garbage Collection
A question on Dan's GC Question
Dan exam doubt 19