• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

GC problem

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

how many object are eligible for GC???
 
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think four objects are eligible for GC.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 objects:


 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anastasia is right, there are 3 objects eligible.
It will be 4 If gb1 is assigned to null.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vineet walia says
how many object are eligible for GC???



I guess you should draw a diagram and first give your answer so that you can verify with the ranchers.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Number of objects actually created - Number of objects still having references assigned
 
Sheriff
Posts: 9690
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 Quote Your Sources when you post a question. You've posted multiple questions without a proper source. Your questions will be deleted if you don't give the source properly...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a famous examlab question....
 
Ankit Garg
Sheriff
Posts: 9690
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

Nitish Bangera wrote:This is a famous examlab question....



Its good that it is from a legal mock exam, but generally we expect the topic creator himself/herself to specify the source. That way its easier for everyone. Anyways its good that you are making efforts
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 objects ,right?
 
Ankit Garg
Sheriff
Posts: 9690
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
Its easy to find the number of object eligible for GC using a diagram

At line 7, the memory map looks like



At line 8 it becomes



at line 9 it becomes



and at line 10 it is



So 3 objects are eligible for GC (The one with the capital G in them)...
 
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

Anastasia Sirotenko wrote:3 objects:



bro not geting your handy notation can you exp lain little bit??
 
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

Ankit Garg wrote:Its easy to find the number of object eligible for GC using a diagram



thank Ankit Garg i thnk i will help me out

[Edit AG: Removed long quotes which were looking messy]
 
Ankit Garg
Sheriff
Posts: 9690
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, you can use the @<NAME> to direct your message to someone specific instead of quoting whole posts. Like what you said in the last two posts can be done like -

@Anastasia, bro not geting your handy notation can you explain little bit??

@Ankit, thanks I thnk I will help me out

That way its easier to read as quoting long posts can look very messy...
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody tell me why this line No. 7



does not give a null pointer exception, since, ultimately it does try to cast a null type to GARB type in the constructor..

I have tries this code and it runs fine!

Also, if i replace line no. 7 by,



I DO get the null pointer exception , why is this ???

 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NullPointer comes from line 8, where you try to instantiate gb1.g.g , tho gb1.g is null after line 7
 
Ankit Garg
Sheriff
Posts: 9690
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
Tanya this is from the JLS

The null reference can always be cast to any reference type.

 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just check where exatly you get the exception, and you shall know
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit that was a GREAT diagram.
Absolutely wonderful. I wonder how long it took you to draw it out.

The first time I answered this question I drew the diagram but I still got the answer wrong. I drew an extra object becuase I was confused.

Then I reviewed/drew the diagram again before looking in the forums for more explaination.

When I reviewed it for the 2nd time, I got the answer right. and double checked the forum.

The trick with all of these GC questions is that the class always HAS-A instance variable of some object type, and that can be confusing.

The best thing you can do is keep practicing and draw it out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic