Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Life cycle of local Java objects created during a method call

In a method call, if I create an object during that call. When are those objects garbage collected?

Are they placed on the heap and then garbage collected along with other objects on the heap. Or are they garbage collected earlier because they are not needed. The execution of that method has completed.

like image 267
Berlin Brown Avatar asked Dec 29 '10 19:12

Berlin Brown


People also ask

What is the life cycle of an object in Java?

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 an object is created in Java?

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.

What is object life cycle?

An object's life cycle—that is, its runtime life from its creation to its destruction—is marked or determined by various messages it receives. An object comes into being when a program explicitly allocates and initializes it or when it makes a copy of another object.

When the new objects are created by JVM?

The JVM creates a new object when the clone() method is invoked. It copies all the content of the previously created object into new one object. Note that it does not call any constructor. We must implement the Cloneable interface while using the clone() method.


3 Answers

Objects created within method scope are eligible for garbage collection when the method is closed - unless that reference is passed back as the return value. In that case, the caller may or may not hang onto that reference and prevent it from being gc'd.

Since the garbage collector runs on its own thread according to its own lights, you don't necessarily know when an object is cleaned up, or whether or not objects allocated elsewhere are eligible as well.

like image 132
duffymo Avatar answered Nov 03 '22 00:11

duffymo


This is not so easy - in the very end every object is created in some method.

The VM / compiler needs to make escape path analysis to detect if a reference of this object in some way can escape - imagine calling newObject.toString(). You and me know (hope) that this will do no harm and the object is still not referenced, as it will not link itself to a global variable. But the VM dows not.

While a modern VM will do such analysis and treat real short lived objects special in garbage collection, from a "high level" point of view they are just objects. Everything else is sophisticated low level optimization.

And anyway, as duffymo says - when exactly this objects are freed is uncertain.

like image 30
mtraut Avatar answered Nov 03 '22 00:11

mtraut


The fact that the execution of the method has ended and now the object is out of scope is irrelevant.
Garbage collection is an implicit operation of the runtime system running in a separate thread in parallel with your code, implementing a specific garbage collection algorithm.
The garbage collection thread runs at unpredictable times-but very often, every second or so according to java docs and when the memory is almost run out, evaluating which objects are eligible to be garbage collected i.e. there are no references to them from root pointers e.g. static variables.
So every object accessible by a root pointer is marked and then recursively the objects referenced by these objects etc.
This can mean scanning the entire process space. Once done, all the objects not "marked" from previous scan go to the free list (are GC'd).
This is a heavy operation as you can see.

The execution of that method has completed.

So the fact that you are out of the scope of a method you called because it has completed, is irrelevant. It does not mean that the runtime knows at that point that the object is done (since GC is run in parallel).
It is not like in C++ that in the end of the method the programmer would call delete on the object since it is not needed. No "delete" is automatically called at the end of the method in Java.
The GC thread will eventually realize that there are no more references to the object allocated on the heap by the method and CG it.
You can call the GC at any point if you want by:

System.gc();

But the GC will run sooner or later anyway.
One thing to note is as long as there is a reference to a method by a root pointer it can not be GC's.
So if in your method you create via new an object on the heap and you store the reference in a container that is static or return it to the caller, the object outlives the method.

like image 44
Cratylus Avatar answered Nov 03 '22 01:11

Cratylus