• 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

Garbage Collection

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

Anyone please who could help me to clear this doubt. I have read that objects passed to the method cannot be garbage collected inside the method, because this was not the place they were created. This program is crude, just listed the above concept in program. Is this right.


In method tt() , k is made null, means can be garbage collected, also when the method is executed all objects created are lost, unless the reference is passed. So based on the above, n is not garbage collected, as it was just passed to the method, and was created outside the method.


public class NewMain58 {

public void tt(NewMain58 k){ k = null;}

public static void main(String[] args) {
NewMain58 n = new NewMain58();
n.tt(n);

}

}
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Function tt() returns the control back to the Main Program. after this return garbage collection happens
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushraf Khan wrote:GoodDay Everyone,

Anyone please who could help me to clear this doubt. I have read that objects passed to the method cannot be garbage collected inside the method, because this was not the place they were created. This program is crude, just listed the above concept in program. Is this right.


In method tt() , k is made null, means can be garbage collected, also when the method is executed all objects created are lost, unless the reference is passed. So based on the above, n is not garbage collected, as it was just passed to the method, and was created outside the method.


public class NewMain58 {

public void tt(NewMain58 k){ k = null;}

public static void main(String[] args) {
NewMain58 n = new NewMain58();
n.tt(n);

}

}




hello Mushraf Khan,

as saahil gupta said when it will return to main method it gets GC (garbage Collected) yes...there is one more point is , Object('s) get garbage collected when it's scope gets finished means "{ }" this bracket..

(may be m right )

Thanking you.
 
Mushraf Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, to bother again. Let me be bit specific. In the method tt() when k = null, does not make n null. Is that right. And as you were saying that after the {} of main method , objects get garbage collected.Please comment on this. i would greatly appreciate.

Thanks in advance
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I used "Code" tag with your code-it gives better readability.
Now coming to your question, only one object was created in heap. 'n' refers to it in main. When we invoke the method 'tt' on the same object, a copy of the reference variable 'n' was passed to it, not the actual object. Inside 'tt', the argument k,is a copy of the reference variable 'n'. 'k' was originally referring to the object created in main. After executing 'k=null', k doesn't refer to anything. But 'n' still refers to the same object in main. Hence, the object created in main won't be eligible for GC before main returns. Moreover, tt is an instance method and you are using the object referred by n to invoke it.

Let us consider a slight modification below,where I created two objects in main.

The object referred by n1 won't be eligible for GC after executing n.tt(n1), as it is still reachable from main.
 
Mushraf Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sebanti. Understood it clearly. really appreciate it. Thanks again
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic