Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • 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 Question from Marcus Green

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


Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here

0
1
2
3
4

I answered 2, but the answer given was 0. Why is this so?
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Zero is correct. Line 1 creates a new integer object. In line a copy of refernce x is passed to method. In the method findOut, the copy is reassigned , not the original object. The original object pointed by x stil exists. Line 4 causes one more reference to point to object created at line 3. z =null makes only y point to the object. Apply similar logic as applied previously for findout(y). When the commented line is reached all objects are refernced and are not eligible for GC. Hope this helps
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
According to me also it should be 2 only. Lets see if any one can explain !!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pardeep,
As you have explained that at the commented line since all the objects have the references so they are not eligible for the GC. But since that line is the last line of the method , so after that line all the references to all the objects will vanish , so all the Live objects should be eligible for GC.

Please explain me this as i am very much confused about GC.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree none are eligible for GC.

But if the findOut() is modified like the one below then 2 objects are eligible for garbage collection , am i right ?


[ April 21, 2005: Message edited by: Srinivasa Raghavan ]
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasa Raghavan:
I agree none are eligible for GC.

But if the findOut() is modified like the one below then 2 objects are eligible for garbage collection , am i right ?



yes
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manish Nijhawan:
Hi Pardeep,
As you have explained that at the commented line since all the objects have the references so they are not eligible for the GC. But since that line is the last line of the method , so after that line all the references to all the objects will vanish , so all the Live objects should be eligible for GC.

Please explain me this as i am very much confused about GC.



The objects will be eligible for GC after the method completes.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I understand that the object holding 111 in findOut() becomes eligible for GC, but which is the other object that's eligible after the modified findOut() ?

Thanks,
Soumya
[ April 21, 2005: Message edited by: soumya ravindranath ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
findOut() is called twice check the code

 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooops! thanks
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to me only one object will be elgible for garbage collection..i.e
new Integer(10);
this object has refernce count =0;as u can check .Intially this object was refrenced by x but when x=y is executed .This object is no longer refrenced.
new Integer(99) is being still refernced by y.so only one object is elgible for garbage collection
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nish vats:
According to me only one object will be elgible for garbage collection..i.e
new Integer(10);
this object has refernce count =0;as u can check .Intially this object was refrenced by x but when x=y is executed .This object is no longer refrenced.
new Integer(99) is being still refernced by y.so only one object is elgible for garbage collection



Where is x=y I dont see it. What about others?
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I see it, there is no x =y in the code given.

When z = y and then z = null, as Pradeep said, it means nothing to z or y. x continues to point to 10.
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I understand why "0" was the answer. The copy of the reference object was reassigned in findOut method and not the original reference.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is so clear.

This is the sample of pass by reference problem.

This problem look like this

https://coderanch.com/t/248832/java-programmer-SCJP/certification/Dan-exam

This explanation very clear, I think.

http://www.javaranch.com/campfire/StoryPassBy.jsp

And this flash playing is interesting.I think it is very good to show how it work.


http://www.geocities.com/mcglonec1978/javacert/paramPassing_highres.html

This it main url.
http://www.geocities.com/mcglonec1978/javacert/javacert.html#param_passing

Hope it useful for you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic