A Day In The Life Of Java Code In JVM | CodersTea
Home Java A Day in the Life of Java Code in JVM

A Day in the Life of Java Code in JVM

by Mahesh More
Published: Last Updated on 4667 views
A Day in the Life of Java Code in JVM

Hey, Tea Lovers! Today we will talk about Java Memory Management with the example. We will look at the code and then how it is affecting the JVM. So the relationship between these 2. I will try to explain it line by line. So let us see how a code lives its life.

You can follow me on social media via @coderstea on TwitterLinkedinFacebook, or Instagram. We also share high-quality videos about programming on our Youtube channel. You can also publish your post on CodersTea, just share your thought on Contact Us or let us know in the comments.

It is part of the JVM post series “Get Ready to Deep Dive Java Memory Management”.
Before we explain examples first we will understand the java rules for java memory allocation.

Rules for Java memory

  • Object are stored on the heap
  • Variables are a reference to the object
  • Local Variables are stored on the stack

Below example, we initialize String & list & manipulate the values of variables, for better understanding discuss the below example line by line.

The Code

import java.util.*;
public class Main
	public static void main(String[] args) {
		String name = "mahesh";
		final List<String> listOfString = new ArrayList<>();
		System.out.println("name :: "+name);
		System.out.println("listOfString :: "+listOfString);
	public static void modified(String str){
	    System.out.println("exiting name :: "+str);
	    str = "Imran";
	    System.out.println("modified name :: "+str);
	public static void modified(List<String> listStr){
	    System.out.println("exiting listStr :: "+listStr);
	    System.out.println("modified listStr :: "+listStr);


exiting name :: mahesh
modified name :: Imran
exiting listStr :: []
modified listStr :: [Imran]
name :: mahesh
listOfString :: [Imran]

What a Life the Code have

I will start from line 7 because calling the main method with a string array(args) is common in all.

For better understanding, we will use the below diagram to the java memory allocation on stack & heap.


Step – 1)

  • On code line 7 we have an initialized String name variable with the “mahesh” value.
  • On the stack, it will store the variable identifier as the name & make String object on heap assigned value as the “mahesh” & point the stack variable to the heap.
  • It will represent in the diagram.

Step – 2)

  • On code line 8, It will call modified(String str) method & control pass to modified(String str) method, with String str variable with the “mahesh” value.

Step – 3)

  • On code line 19, It will display the str value on the console.
  • On code line 20, str assigned string value as “Imran”.
  • There It will create a new string with value “Imran” & then it will change an str reference to that “Imran”.

Step – 4)

  • On code line 21, It will display the str value on the console & control again pass to the main method on code line 9 & same time str is gone out of scope. it will garbage collect str variable & java memory structure looks like as below.

Step – 5)

  • On code line 10, here create a list of ArrayList type with identifier listOfString.
  • listOfString is an of final.
  • On java memory, the structure looks as below.

Step – 6)

  • On code line 11, It will call modified(List<String> listStr) method & control pass to the method, Initialized listStr variable with the initial value. (null)

Step – 7)

  • On code line 25, It will display the value on the console, & the next line.
  • On code line 26, It will insert/add the string value “Imran” in List<String> listStr. This will create a string with “Imran” & than str reference point to that listStr index 0 from the list, same show below.

Wait, It’s possible to modify the value in the final list.

  • To understand this lets first understand the final keyword
    Once a final a variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
  • This will applies to the List because List is objects. if a final the variable holds a reference to a List, then the components of the List may be changed by operations on the List, but the variable will always refer to the same List on the heap.
  • If you find in the above picture in listOfString is has reference on list object & from inside this, it has an index which pointed to the object in the current case it String.

for more info check out : Interview Question: Final vs Finally vs Finalize

Step – 8)

  • Then On code line 27, It will display the value on the console & after control again pass to the main method on code line 13 & meanwhile remove reference to List<String> listStr which look like as below.
  • Finally, On code lines 13 & 14, It will display the value in name & listOfString on the console & program exit.


I hope you liked the post. I tried to simplify things. If I had missed something please feel free to suggest to me in the comment section. If you are still confused about the JVM, I would say please see my post series on JVM “Get Ready to Deep Dive Java Memory Management”. Please see them in the following order.

I hope you liked the post. See you in the next post. HAKUNA MATATA!!!

Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ashish Singh
Ashish Singh
January 22, 2022 9:07 am

I am not sure, but don’t you think that a copy of the list will be created when it is passed into the method?
According to the 7th stach diagram, you are pointing two references to the same list. Is that correct?

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More