Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

garbagecollection

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic