• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Basics on how Garbage collector works.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
I came across this question on net.

class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3; //6
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}
After line 6, how many objects are eligible for garbage collection?

--> I feel that answer of this question must be 1. I want to know whether I am correct or not.

 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a direct answer, but maybe this helps: https://coderanch.com/t/266329/java-programmer-SCJP/certification/garbage-Collector. It's certainly fun :-)
 
Tabassum Momin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for sharing that link. It was really helpful and interesting too...
As far, what I understood from that discussion is :
-->The object created within m1() method is the only object ready for garbage collection after the execution of line 6.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tabassum Momin wrote:As far, what I understood from that discussion is :
-->The object created within m1() method is the only object ready for garbage collection after the execution of line 6.


Sounds right to me.

Basically, an object is available for garbage collection as soon as there is no reference in scope that points to it. And in order for a reference to be "in scope" (and I'm not sure if that's the correct term, but it's the way I always think of it) it has to either:
(a) have a name that can be used.
(b) be returned from a method as part of a compound statement, in which case it will be in scope for as long as it's needed in the statement.

For example:
BigInteger cube256 = BigInteger.valueOf(256L).pow(3);
returns an unnamed BigInteger object from valueOf(), which is then used to return its cube (a different object). The unnamed object will be eligible for gc as soon as the pow() method has finished, but the one assigned to cube256 will only be eligible when either:
  • cube256 is reassigned, or
  • no piece of code can "see" code256 any more.

  • Quite honestly, unless you need this information for passing the SCJP exam, it's rarely something you need to obsess about. If you follow good programming practises and avoid using lots of static fields, the chances of you holding on to objects for a lot longer than you need to is fairly slim.
    One exception is Closeable objects (see java.io.Closeable), which you should remember to close() as soon as you're finished with them. And if you're using version 7, there is the new try-with-resources statement that helps you out with that stuff.

    HIH

    Winston
     
    Tabassum Momin
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank You. I am right now, preparing for SCJP exam, your suggestion will help me to focus on right path.
    It is very well explained and had cleared all my doubts
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tabassum Momin wrote:Thank You. I am right now, preparing for SCJP exam, your suggestion will help me to focus on right path.
    It is very well explained and had cleared all my doubts


    Glad to help.

    Winston
     
    I wasn't selected to go to mars. This tiny ad got in ahead of me:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic