• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

garbage collection question

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John Hunt mock test Question 31
In the following code which is the earliest statement, where the object originally held in employee, may be garbage collected:
public class Test {
public static void main (String args []) {
Employee e = new Employee("Bob", 48);
e.calculatePay();
System.out.println(e.printDetails());
e = null;
e = new Employee("Denise", 36);
e.calculatePay();
System.out.println(e.printDetails());
}
}
A. Line 10
B. Line 11
C. Line 7
D. Line 8
E. Never
The answer is 'C'
Can anyone explain why?
Thanks!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quoted from TIJ


A simple but slow GC technique is reference counting. This means that each object contains a reference counter, and every time a reference is attached to an object the reference count is increased. Every time a reference goes out of scope or is set to null, the reference count is decreased. ... The garbage collector moves through the entire list of objects and when it finds one with a reference count of zero it releases that storage. ... Reference counting is commonly used to explain one kind of garbage collection but it doesn't seem to be used in any JVM implementations.


The answer hence, I believe, is the line of code:
e=null;

When :
Employee e = new Employee("Bob", 48);
is executed, the object is created and the reference count for it is increased to 1. When :
e = null;
is executed the ref count for the object Employee is decreased by 1 and thus becomes 0. This causes it to be made available for gc.
Regds
Sathish
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To emphasize more, the answer is after line e=null and not at the line e=null.So, 7 is correct.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys!!!
 
We begin by testing your absorbancy by exposing you to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic