-2

enter image description here

class Robot
{
long memory[] = new long[9923372];

private String name;

Robot(String nm) throws Exception
{
name = nm;
System.out.println("name = " + name);
}

protected void finalize()
{
System.out.println("\nBye - Bye ---eeeee " + name+"\n");
}

}


class Test
{

public static void main(String sdf[]) throws Exception
{
int i=1;
Robot robot1;


while(true)
{
//finalizer runs before the dereference of rajni - 1
robot1= new Robot("Rajni - "+i++);
Thread.sleep(1000);
}

}
}

how can finalizer run before derefernce of robot1 from rajni1 first object ... the loop runs in the infinite loop...

i know that garbage collector runs whenever heap space is low and more memory is required for the object allocation...but the condition is that there must be some dereferenced object residing in memory .....

--->run in jre 32 bit to get the given output ... you better know why??

Anonymous
  • 67
  • 6
  • Please show the program output. – BetaRide Jul 14 '14 at 13:28
  • What do you mean "before dereference"? You *never* reference `robot1`, so it's probably not even generated, and the `new Robot` is simply thrown on the floor -- instantly collectable. – Hot Licks Jul 15 '14 at 02:59
  • i couldn't explain well ...just see the output... you will see that before the dereference of first object(rajni-1).. gc runs...and removes first object.. even robot1 is referring to object rajni1 – Anonymous Jul 15 '14 at 03:01
  • (By the way, in computing "dereference" means to "follow" a pointer or reference to its "target".) – Hot Licks Jul 15 '14 at 03:01
  • You're right -- you haven't explained anything. Dump the bytecodes. – Hot Licks Jul 15 '14 at 03:02
  • By the way, when a GC cycle starts it has no guarantee that there is any unreachable object anywhere in the JVM. – Hot Licks Jul 15 '14 at 03:10
  • just tell me ..u got the question or not??reply – Anonymous Jul 15 '14 at 03:14
  • 1
    Your output shows a sequence of construction of an object followed by finalization of the same object. There is no evidence here of when the dereference took place. You also need to be aware that finalizers run on a separate thread, and that different threads writing to the same stream can appear to interleave in unexpected ways due to buffering. There is no case to answer. – user207421 Jul 15 '14 at 03:20
  • @EJP - am i right? the dereference of first object takes place when loop iterates second time and constructor is called which returns the address implicitly to only reference variable robot1 ?? – Anonymous Jul 15 '14 at 03:26

1 Answers1

2

It is clearer now you have provided the output.

You can look at the compiled byte code to see why.

  public static main([Ljava/lang/String;)V throws java/lang/Exception 
   L0
    LINENUMBER 48 L0
    ICONST_1
    ISTORE 1
   L1
    LINENUMBER 54 L1
   FRAME APPEND [I]
    NEW Main$Robot
    DUP
    NEW java/lang/StringBuilder
    DUP
    INVOKESPECIAL java/lang/StringBuilder.<init> ()V
    LDC "Rajni - "
    INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
    ILOAD 1
    IINC 1 1
    INVOKEVIRTUAL java/lang/StringBuilder.append (I)Ljava/lang/StringBuilder;
    INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
    INVOKESPECIAL Main$Robot.<init> (Ljava/lang/String;)V
    ASTORE 2
   L2
    LINENUMBER 55 L2
    LDC 1000
    INVOKESTATIC java/lang/Thread.sleep (J)V
    GOTO L1
   L3
    LOCALVARIABLE sdf [Ljava/lang/String; L0 L3 0
    LOCALVARIABLE i I L1 L3 1
    LOCALVARIABLE robot1 LMain$Robot; L2 L3 2
    MAXSTACK = 4
    MAXLOCALS = 3

The important line to note is the scope of the robot1 variable which is between L2 and L3 i.e. only during the Thread.sleep(1000); and the local variable is out of scope as soon as the loop jumps back to L1

This means the variable in reality is available to be GC-ed at the top of the loop, not after the new Robot has been created as you might imagine.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • can you please tell me how to diassemble the bytecode as you did above to see the scopes of vars... – Anonymous Jul 15 '14 at 06:38
  • @nihal You can do `javap -c -v -p -cp {classpath} mypackage.MyClass` or you can add a Byte Code viewer to your IDE. The ASM Byte Code Viewer is pretty good. – Peter Lawrey Jul 15 '14 at 06:54
  • -> one interesting thing if i assign robot1=null; at the time of declaration then comes - OutOfMemoryError that is garbage collector DONOT run... It's a bit confusing.... Does doing so increase its scope... What u think??? – Anonymous Jul 15 '14 at 09:44