Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbagecollection

 
Manu Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic