• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage Collection

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


public class TestGC
{
public static void main(String [] args)
{
Object o1 = new Integer(3); // Line 1
Object o2 = new String("Tutorial"); // Line 2
o1 = o2; // Line 3
o2 = null; // Line 4
// Rest of the code here
}
}

In this program after assiging o2 value to o1 we arr nulling o2.That means it is eligible for garbage colection.in the line 4 it is eligible to gc.but answer is not garbage collected.how it is?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Suhita Reddy:
Hi,


public class TestGC
{
public static void main(String [] args)
{
Object o1 = new Integer(3); // Line 1
Object o2 = new String("Tutorial"); // Line 2
o1 = o2; // Line 3
o2 = null; // Line 4
// Rest of the code here
}
}

In this program after assiging o2 value to o1 we arr nulling o2.That means it is eligible for garbage colection.in the line 4 it is eligible to gc.but answer is not garbage collected.how it is?



The object originally referred to by o1 is eligible for garbage collection. In line 3, you assign o1 to point to the same object o2 is pointing at. So setting o2 to null has no effect.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i am agree with Keith!!!

let's say at line1 . you have created an object say Object1 which resides inside heap memory while the ref of it o1 is inside stack.
similarly with line 2 ----
object2 in heap
o2 in stack..
now come at line3

o1=o2 // here o1 assigned a ref of object2, while o2 is allready refer object2...now object1 is referenceless...means not reachable by any stack var....hence it is ready gor garvage collection...

---now at line 4 when yoy say

o2=null // it means now object2 has only one ref in stack is o1, before this line object2 has 2 refrences in stack o1 and o2...but it has a ref means it is reachable and not eligible for garvage collection

--------may this help
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith wrote:
"The object originally referred to by o1 is eligible for garbage collection. In line 3, you assign o1 to point to the same object o2 is pointing at. So setting o2 to null has no effect."

Keith, in Java circles, one does not "point to" or "point at", it's rude. In polite society one "refers to".
 
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