Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Garbage collection

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Whether a static reference to an object if either reassigned or assigned to null, the object becomes eligible to garbage collection?

2.Whether a string if reassigned to another or assigned to null becomes eligible for garbage collection?
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Geethakrishna Srihari:
1.Whether a static reference to an object if either reassigned or assigned to null, the object becomes eligible to garbage collection?

2.Whether a string if reassigned to another or assigned to null becomes eligible for garbage collection?



1) Yes the object will be eligible for collection if there are no live references to it.

2) A String object yes, but a String constant no. The garbage collector only deals with objects and thus anything in the string constant pool is not dealt with by the garbage collector
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B - says :

"No. There isn�t a mechanism for marking an
object as undeletable. You can instead create a static
member of a class, and store a reference to the object
in that. Static members are considered live objects"

Does it not mean that static references are always live?

How garbage collection differs for static and non-static references?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1) Yes the object will be eligible for collection if there are no live references to it.

2) A String object yes, but a String constant no. The garbage collector only deals with objects and thus anything in the string constant pool is not dealt with by the garbage collector


Both of these are untrue.

An object will become eligible for garbage collection when there are no strong references to it that can be rooted (e.g. exclusively circular strong references to an object make eligible garbage collection). A static member exists for the lifetime of the class loader that loaded it, therefore, if that class loader is ever eligible for collection, that is the time that the object referred to by a static reference becomes eligible.

String constants may certainly be garbage collected. To be honest, I'm tired of this little myth on this forum and explaining it away every time, but I've attempted an explanation here: http://qa.jtiger.org/GetQAndA.action?qids=68
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:

Both of these are untrue.

An object will become eligible for garbage collection when there are no strong references to it that can be rooted (e.g. exclusively circular strong references to an object make eligible garbage collection). A static member exists for the lifetime of the class loader that loaded it, therefore, if that class loader is ever eligible for collection, that is the time that the object referred to by a static reference becomes eligible.

String constants may certainly be garbage collected. To be honest, I'm tired of this little myth on this forum and explaining it away every time, but I've attempted an explanation here: http://qa.jtiger.org/GetQAndA.action?qids=68




pls can u explain in easy words... the string reference you have given is bit tough to understand...
what i understand is that string constant is general(broader) type of string literal and nothing much...

can you pls explain in easy words (i.e summary) so we can easily put in our mind and clear the funda...
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. According to me it will become eligible to garbage collection.

public class Runner {
static MyType myType = new MyType();
public static void main(String[] args) {
Runner.myType=null; //line 4
java.util.ArrayList arrayList = new java.util.ArrayList();
while(true)
{
arrayList.add(new String("waste"));
}
}
}
class MyType
{
int i;
protected void finalize()
{
System.out.println("bye");
}
}
output:
bye
java.lang.OutOfMemoryError
Exception in thread "main"

explanation:
coz before giving error(OutOfMemoryError) ,jvm runs garbage collecter atleast once.since finalize is called means object is garbage collected.it does not matter whether it is static.


2. String str1="garbage"
String str2 = new String("garbage");
str1=null;
str2=null;
after the execution of above code the string literal "garbage" is not elligible for garbage collection coz the String class maintains the static pool to store the referneces of literals.

String referenced by str2 will elligible for garbage collection.Since str2 in not pointing to string literal.

if i misunderstood the problem please let me know.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rajan singh:
1
2. String str1="garbage"
String str2 = new String("garbage");
str1=null;
str2=null;
after the execution of above code the string literal "garbage" is not elligible for garbage collection coz the String class maintains the static pool to store the referneces of literals.

String referenced by str2 will elligible for garbage collection.Since str2 in not pointing to string literal.

if i misunderstood the problem please let me know.



exellent reply for first answer ...i like your code

well i m confused regarding second one ..that if str2 is on heap and is eligible for garbage collection and suppose its collected by gc ..will the str2 will be in string pool

as its written in K&B that when we create string by using new
one copy is placed at pool and another at heap..

so if it removed from heap ...will one copy will be remain at pool ?
just asking for knowledge...as if str2=null then there is no way to point to str2 value in from pool because there is no referense to "garbage"
 
rajan singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii
when we use String s2 = new String("garbage") we actually have two String objects.
one is "garbage" which is local to constructor of String and since this is literal it's reference will be stored in pool(static member of class String).
second is the string which is pointed by s2 and since it is created by new operator.it will not get stored in pool.
so object pointed by s2 will be freed where as string "garbage" will not be freed(reference is already there in pool).
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys.............
I have 1 doubt.In java ,objects are automatically garbage collected.Then why we need to use System.gc() & finalize() methods???

Please help me yarrrrrr...I really need a help.......help me. bye.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi


I have 1 doubt.In java ,objects are automatically garbage collected.Then why we need to use System.gc() & finalize() methods???



--->System.gc()

Calling the System.gc() method suggests that the JVM to expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

--->finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
 
Skool. Stay in. Smartness. Tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic