wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exam lab gc question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exam lab gc question " Watch "exam lab gc question " New topic
Author

exam lab gc question

vineet walia
Ranch Hand

Joined: Feb 16, 2008
Posts: 138
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;
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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.....


KARTHICK.C , SCJP6-93%
(Born to Win)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Vineet please Use Code Tags when you post a source code. Edit your message using button and then add code tags to it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
vineet walia
Ranch Hand

Joined: Feb 16, 2008
Posts: 138
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;
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

I believe, the representation in the below image would clear that only two objects would be available for garbage collection.



Regards
Salil Verma


Regards
Salil Verma
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

Please can someone explain me the code as I could not get it.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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

Joined: Sep 06, 2009
Posts: 255

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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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

Joined: Aug 12, 2009
Posts: 68

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

Thanks
 
Consider Paul's rocket mass heater.
 
subject: exam lab gc question