This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

exam lab gc question  RSS feed

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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: 138
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator

Please can someone explain me the code as I could not get it.
 
Ankit Garg
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

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

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!