Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Life cycle of a new object without reference

My question is related to:

  • Java: What is the purpose of creating an object in the heap with no reference
  • Java - Can objects which are executing methods be garbage-collected?

What actually happens when we have something like this in our code:

 (new SomeClass()).longMethod();

Is there still some sort of unnamed (strong ?) reference pointing to the newly created object on Heap put on Stack?

If nothing is on Stack, then how does Garbage Collector know to preserve the object for the duration of the method?

Is it possibly the same as

{ 
  // very local scope
  SomeClass throwAwayRef = new SomeClass();
  throwAwayRef.longMethod();
}
like image 684
PM 77-1 Avatar asked Jun 17 '15 17:06

PM 77-1


People also ask

What happens to an object when there are no references to it?

As far as your code is concerned, the instance is gone as soon as you no longer have a reference to it.

Can object be created without reference?

no. When you later "initialize" the variable, you are assigning a reference to the variable. You are not copying the fields of the object.

What is the life cycle of an object?

We can break the life of an object into three phases: creation and initialization, use, and destruction. Object lifecycle routines allow the creation and destruction of object references; lifecycle methods associated with an object allow you to control what happens when an object is created or destroyed.

What happens when a new object is created?

When an object is created, memory is allocated to hold the object properties. An object reference pointing to that memory location is also created. To use the object in the future, that object reference has to be stored as a local variable or as an object member variable. Code section 4.30: Object creation.


1 Answers

You can look at the bytecode for insight:

   0: new           #16                 // class SomeClass
   3: dup
   4: invokespecial #18                 // Method SomeClass."<init>":()V
   7: invokevirtual #19                 // Method SomeClass.longMethod:()V
  • new actually allocates the object, a reference to which is pushed on the stack.
  • dup duplicates the top-of-stack; now the top two stack items are references to the newly created object.
  • invokespecial here calls the constructor of SomeClass, popping the stack; now the stack consists of just a single reference to our SomeClass instance. The instance is not GCed because a reference to it exists on the stack.
  • invokevirtual here calls longMethod. Again, the instance is not GCed because a reference to it still exists on the stack (and is popped after the method completes, after which it is eligible for GC).

(new SomeClass()).longMethod();

is not the same as

{ 
  // very local scope
  SomeClass throwAwayRef = new SomeClass();
  throwAwayRef.longMethod();
}

at the bytecode level, since the latter involves an astore and an aload. However, the two are certainly functionally equivalent. The SomeClass instance still becomes eligible for GC after longMethod completes (the stacks for the two snippets will look identical when invokevirtual is executed).


Reference:

  • Java bytecode instruction listings
like image 82
arshajii Avatar answered Sep 21 '22 14:09

arshajii