File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection

 
Rukky A
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me with this question

class Test {
2. public static void main(String [] args) {
3. Test2 t2 = new Test2();
4. Test2 t3;
5. t3 = t2.m1();
6. t2 = t2.linker(t2,t3);
7. t2 = null;
8. t3 = null;
9. }
10. }
11.
12. class Test2 {
13. Test2 link;
14. Test2 m1() {
15. Test2 x = new Test2();
16. return x;
17. }
18. Test2 linker(Test2 z1, Test2 z2) {
19. z1.link = z2;
20. z2.link = z1;
21. return z1;
22. }
23. }


Which statement is true?





A After line 8 no objects can be garbage collected.
B After line 7 one object can be garbage collected.
C After line 7 no objects can be garbage collected.
D After line 6 at least one object has more than two references
E The z1 and z2 references must be nulled to make any objects eligible for garbage collection.

The answer was C, can someone please explain why this is so.

Thanks for you help.
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are only two objects and four references to them.
After Line 7, only two references are nullified one is t2 and the other is t2.link which is pointing to the object refered by t3.

Now T3 has reference to one object. T3.link has reference to T2 object.

After line 7 we have two objects. One is T3 and the other is T2 which is refered by T3.link.

if we make T3 = null, both objects will be garbage collected.
 
Rukky A
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.
Can you please explain what line 12-21 does.

Thanks
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method m1 is nothing but a kind of constructor.
Method linker just creates a internal reference to the other object.

Try the below...

import java.io.*;

class TestIt {
public static void main(String [] args) throws Exception {
Test2 t2 = new Test2();
Test2 t3;
t3 = t2.m1();
t2 = t2.linker(t2,t3);
GCThread gc = new GCThread();
gc.start();
t2 = null;
char c = (char)System.in.read(); // A break point 1
System.out.println(c);
t3 = null;
char c1 = 'z';
while((c1 = (char)System.in.read()) != 'z')
{
continue;
}; // A final break point
System.out.println(c1);
}
}

class Test2 {
static int i = 0;
Test2 link;
Test2 m1() {
Test2 x = new Test2();
return x;
}
Test2 linker(Test2 z1, Test2 z2) {
z1.link = z2;
z2.link = z1;
return z1;
}
public void finalize()throws Throwable
{
System.out.println("Garbage Collecotr: Am Collecting the object");
Test2.i++;
super.finalize();
}
}
class GCThread extends Thread
{
boolean stopFlag = false;
public void run()
{
while (Test2.i < 2)
{
System.gc();
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
 
Kevin Lam
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why D isn't correct too?

after line 6, we have


t2->objA objA.link -> objB objB.link -> objA t3 -> objB

so object A has t2 & objB.link pointing towards it, and it is pointing towards objB... which means there are 3 references attached to it??

Kev
 
Leoo yu
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"After line 6 at least one object has more than two references "
Because t3 just have one reference t2.link.
That's be explained by Lakshmanan Arunachalam kindlly.

Leoo.
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D is not correct.
There were only two objects ObjA, ObjB.
ObjA was referred by t2 and t3.link.
ObjB was referred by t3 and t2.link.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic