It's not a secret anymore!*
The moose likes Performance and the fly likes Question on GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Question on GC" Watch "Question on GC" New topic
Author

Question on GC

Xavier George
Ranch Hand

Joined: Jul 29, 2005
Posts: 39
I have a question regarding the GC. When I run Test class, there will be 6 objects created xyz being parent with a b c d e and e (if I am not wrong). when xyz goes out of scope then garbage collector runs. So, when GC runs and when object xyz is garbage collected then at the same time all other objects (a b c d e f) will be gabage collected? Please let me know.

class Test
{
public static void main(String args[])
{
XYZ xyz = new XYZ();
xyz.createMethod();
}
}

class XYZ
{
void createMethod()
{
A a = new A();
B b = new B();
C c = new C();
D d = new D();
E e = new E();
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

a, b, c, d, and e are eligible for garbage collection as soon as the createMethod() method returns. Those variables are all local to that method, and aren't connected to the xyz object at all; in other words, the xyz object could live for a long time, but a, b, c, d, and e could be collected long before xyz.


[Jess in Action][AskingGoodQuestions]
Xavier George
Ranch Hand

Joined: Jul 29, 2005
Posts: 39
Okay, lets put it this way.

class XYZ
{
A a = new A();
B b = new B();
C c = new C();
D d = new D();
E e = new E();

void createMethod()
{
// do something with a b v d e object;
}
}

Now can a b c d e will exist without xyz object? I mean when GC collects xyz so will at the same time abcde must be Garbage Collected.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Not must, but may, yes. In this scenario, a b c d e can't be collected until the xyz instance that refers to them is collected. But garbage collection is a non-deterministic background process -- objects don't necessarily get collected the instant they become eligible. They might hang around for a long time, until your program really needs to reuse the memory.
Virag Saksena
Ranch Hand

Joined: Nov 27, 2005
Posts: 71
Yes, since the instances a, b, c, d, e are children of instance xyz, they'll be eligible for garbage collection when xyz is orphaned by going out of scope (if they have no external references like your example).

However they might not get collected until a full GC happens if ...

1. they land up in the tenured/old generation. In this case if xyz is in Young generation, it'll get collected, but the other instances (even though they are garbage) will not be collected till a full GC

2. they are in the Young generation, but xyz is in tenured/old gen. xyz will not get collected till a full GC (because it is in tenured/old gen). Since a,b,... have a reference from old/tenured generation they'll also not get collected till a full GC happens


<a href="http://www.auptyma.com" target="_blank" rel="nofollow">The Peak of Performance</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on GC