• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

exam lab gc question

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exam lab gc question


1. class A{
2.
3. A a1;
4. A a2;
5.
6. public void finalize(){
7. System.out.println("-");
8. }
9.
10. public static void main(String args[]){
11. A s1=new A();
12. s1.a1=new A();
13. s1.a2=s1;
14. s1.a1.a2=new A();
15. s1.a1.a2.a2=new A();
16. s1.a1.a2.a2.a1=s1.a1;
17. s1.a2.a1.a2=null;
18. System.gc();
19. }
20.
21. }

answer is 2
what i think 4 can you explain?

1. s1.a2.a1.a2=null;
2. s1.a1.a1;
3. s1.a1.a2.a1;
4.s1.a1.a2.a1.a2;
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to draw diagram for this......


all the members of an object are in heap.....so here a1 and a2 are in heap.....(of course all objects are in heap....)(i mentioned this for diagrammatic purpose)
all the object references are in stack..... here s1 only.....

see that at least one reference in stack is pointing to that object on heap(either directly or through chaining)
if no reference in stack is pointing to that object (either directly or through chaining) then the object has lost it's reference....
there is no way that your program can access it.....
it is eligible for garbage collection.....
 
Sheriff
Posts: 9692
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
Vineet please Use Code Tags when you post a source code. Edit your message using button and then add code tags to it...
 
vineet walia
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vineet walia wrote:exam lab gc question




answer is 2
what i think 4 can you explain?

1. s1.a2.a1.a2=null;
2. s1.a1.a1;
3. s1.a1.a2.a1;
4.s1.a1.a2.a1.a2;

 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe, the representation in the below image would clear that only two objects would be available for garbage collection.



Regards
Salil Verma
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please can someone explain me the code as I could not get it.
 
Ankit Garg
Sheriff
Posts: 9692
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
Lets see a memory map till the statement at line 16 (s1.a1.a2.a2.a1=s1.a1;)



Then when s1.a2.a1.a2 (which is equivalent to s1.a1.a2) is set to null, then the memory map becomes



The two objects that I've marked as GC, are eligible for GC as they don't have any reference to them...
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ankit,

I was looking for some tool which could help me in making the memory map of specific execution. Could you let me know the name of the tool that you used to get the memory map ?

Regards
Salil Verma
 
Ankit Garg
Sheriff
Posts: 9692
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

Salil Vverma wrote:Could you let me know the name of the tool that you used to get the memory map ?


Fingers (don't go looking for a tool named fingers, I meant I created it in notepad myself ). I don't know of any tool that can do that, maybe someone else can help...
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I must say Ankit you have a great understanding of GC. I understood it very well.

Thanks
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic