Life and Death of an Object in Java

Pasan Kamburugamuwa
5 min readOct 18, 2021

Objects are born and objects die. we are in charge of an object’s lifecycle and decide when and how to construct them. We can destroy the objects after using it. If we couldn’t delete the object and simply abandon it, the heartless Garbage Collector (GC) can vaporize it, reclaiming the memory that object was using. If we write java programs, then we need to create java objects. Sooner or later, you’re gonna create objects. This could led to risk of running out of RAM. So in this tutorial, we are going to look how we can create objects in java and keep or abandon them efficiently.

The Stack and the Heap, Where things alive

Let’s focus on where the objects alive in java. The heap and the stack takes an important place when considering where the objects alive in java. When JVM starts up, it gets a chunk of memory from the underlying OS, and uses it to run the java program. How much memory, and whether or not you can tweak it, is dependent on which version of JVM you are running.

All the objects live on the garbage-collectible heap, but we haven’t yet looked at where variables live. And where a variable lives depends on what kind of variable it is. There are two kinds of variables whose lives we care about now are,

1. Instance Variables

2. Local Variables

Stack and Heap — Image taken from Head First Java — Kathy Sierra

So let’s have a quick look at the instance variables and local variables.

Instance Variables

Instance variables are declared inside a class but not inside a method. Instance variables live inside the object they belong to.

Instance variable s— Image taken from the Head First Java — Kathy Sierra

Local Variables

Local variables are declared inside a method, including method parameters. They’re temporary and live only as long as the method is on stack (in other words, as long as the method has not reached the closing curly braces)

Local Variables — Image taken from the Head First Java — Kathy Sierra

Methods are stacked

When you call a method, the method lands on the top of a call stack. That new thing that’s actually pushed onto the stack is the stack frame, and it holds the state of the method including which line of code is executing, and the values of local variables.

Methods are stacked — Image taken from the Head First Java — Kathy Sierra

What about local variables that are objects?

Non-primitive variable holds a reference to an object, not the object itself. As the objects live on the heap and the local variable is a reference to an object, only the variable (the reference/remote control) goes on the stack.

How the non-primitive objects declared — Image taken from the Head First Java — Kathy Sierra

Where do instance variables live?

Values of an object’s instance variables live inside the object. If the instance variables are all primitives, java makes space for the instance variables based on the primitive type. An int needs 32 bits, a long 64 bits etc. Java doesn’t care about the value inside primitive variables. the bit-size of an int variable is the same (32 bits) whether the value of the int is 32000000 or 32.

How to create an object?

To create an object, we can use easy method like having a constructor. The constructor does look and feel a lot like a method. but it’s not a method. It’s got the code that runs when you say new. In other words, the code that runs when you instantiate an object.

A constructor has the code that runs when you instantiate an object. In other words, the code that runs when you say new on a class type. Every class you create has a constructor, even if you don’t write it yourself.

Four things to remember about constructors.

  1. A constructor is the code that runs when somebody says new on a class type.
  2. A constructor must have the same name as the class and no return type.
  3. If you don’t put a constructor in your class, the compiler puts in a default constructor. The default constructor is always a non-arg constructor.
  4. You can have more than one constructor in your class, as long as the argument lists are different. Having more than one constructor in a class means you have overloaded constructors.

So far, we have discussed how the object is born. But we didn’t talk about how long does an object alive?

How long does an object alive?

An object’s life depends entirely on the life of references referring to it. It the reference is considered “alive”, the object is still alive on the Heap. If the reference dies, the object will die.

The difference between life and scope for local variables.

Life — A local variable is alive as long as its stack frame is on the stack. In other words, until the method completes.

Scope — A local variable is in scope only within the method in which the variable was declared. When its own method calls another, the variable is alive, but not in scope until its method resumes. You can use a variable only when it is in scope.

This is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer being used by a Java application and to recycle this memory for other uses.

Summary

Garbage collection is good. Because you can make sure Java heap memory is properly configured and managed so GC activity is optimized.

Garbage collection is needed when you have unreferenced objects to be cleared out. Since it is not a manual activity, the JVM will automatically take care of this for you. From all the information above, you would have understood why GC is needed and when.

How to tune garbage collection? There are two common ways,

  1. Keep the number of objects passed to the old generation area to a minimum
  2. Configure the major (or full) GC time to be low.

How to know when GC is not operating as expected? JVM monitoring is key. Make sure to track vital JVM metrics and be alerted when GC activity is deviating from the norm.

So in this tutorial, we have discussed the life and death of objects in java. Hope you find this tutorial more useful as developers need to know how things going with java before hands on it. And I should mention that, I followed the book Head First Java — By Kathey Sierra to write this article. Thanks all.

--

--