Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on GC

 
Xavier George
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Xavier George
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic