Win a copy of Head First Android this week in the Android 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

gc in array

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Ar {
public void finalize(){
System.out.println("i am out");
}

public static void main(String []a)throws Exception{


int[][] qq = {{5,2,4,7}, {9,2}, {3,4}}

qq=null;
System.gc();
System.out.println(qq);

}
}
In here it creates 4 objects according to k&B book,
so when i null the reference 4 objects are eligible for gc
there is one out put
null
plese explain me
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
were you expecting the output to be "i am out"??

well null is displayed as System.out.println(qq); will display null because qq is set to null before calling qq.

"i am out" is not displayed as it will be displayed only when objects of class Ar become eligible for GC and are garbage collected. Run this modified code and you might get "i am not" as output(I am writing might as GC is not guaranteed to execute)

 
Asanka Vithanage
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit
i want to know is after nulling array reference
don't array objects are eligible for gc
here i am expecting 7 "i am out" outputs
but it prints only 3 "i am out "s

class Ar {
public void finalize(){
System.out.println("i am out");
}

public static void main(String []a)throws Exception{


Ar[][] qq = {{new Ar()}, {new Ar()}, {new Ar()}};

qq=null;
System.gc();
Thread.sleep(1000);

}
}
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Array objects will also be garbage collected, but in that case the finalize method will not run. finalize method is invoked when single objects are garbage collected and not when arrays are garbage collected...
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ankit,

If the code from Asanka is modified as follows , we can say that four objects are available for garbage collection after the line "qq=null".What do you say?



thanks
Jyothsna
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Ar {

public static void main(String []a)throws Exception{
int[][] qq = {{5,2,4,7}, {9,2}, {3,4}}
qq=null;
}
}



As Ankit said,

finalize method is invoked when single objects are garbage collected and not when arrays are garbage collected...



I think the above statement is about primitive type in Array instead of object type. It seem like array initialized to null? Am I right?

Please correct if I am wrong.

Thanks

Regards,
Edmen
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankit Garg:
Array objects will also be garbage collected, but in that case the finalize method will not run. finalize method is invoked when single objects are garbage collected and not when arrays are garbage collected...



Keep in mind that the Ar class and the Ar array class are two different class types. Of course, array objects finalize methods (if one existed)will run. It is just not the finalize method that prints "i am out". It is a different class type.

Henry
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ar[][] qq ={ {new Ar()},{new Ar()} };

qq=null;

This is want i understood from the above discussion ,please correct me if i am wrong...
When these statements are executed ....
The finalize is executed TWICE for the objects..
And finalize is not exectued for the q[][] even if the array object is eligible for GC
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one throw some focus on whether array objects will be garbage collected or not , if so , whether the finalize method runs or not ?

To my understanding , all the 4 objects should be garbage collected and as mentioned in K & B the jvm's version of finalise method will be called.

Please correct me if I am wrong.
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ar[][] qq ={ {new Ar()},{new Ar()} };

qq=null;

This is want i understood from the above discussion ,please correct me if i am wrong...
When these statements are executed ....
The finalize is executed TWICE for the objects..



Makes sense. There is only two Ar objects, so it would makes sense that the code that prints "i am out" is executed TWICE.

And finalize is not exectued for the q[][] even if the array object is eligible for GC



First of all, what finalize() method for the q[][]? Can you even override an array class to add a finalize() method? .... Or are you assuming that the Ar array will run the Ar class finalize method too?

Henry
[ November 25, 2008: Message edited by: Henry Wong ]
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jyothsna Panchagnula:
Can any one throw some focus on whether array objects will be garbage collected or not , if so , whether the finalize method runs or not ?

To my understanding , all the 4 objects should be garbage collected and as mentioned in K & B the jvm's version of finalise method will be called.

Please correct me if I am wrong.




Yes. Array objects are garbage collected. Array objects don't have a finalize method -- but if they did, they would be finalized too.

In the original post, four array objects will be collected. And the original poster mistakenly assumed that output would be generated, by attaching a finalize() method -- a finalize method that is not related to the array objects. Hence, nothing is printed.

Henry
[ November 25, 2008: Message edited by: Henry Wong ]
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for the clarification. Its helpful indeed.

-Jyothsna
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic