Member-only story

Unlock Java Memory: The Role of Stack and Heap

Namrata
4 min readDec 1, 2024

--

When working with Java, it’s crucial to understand how memory is managed. Understanding the basics of the Java Stack and Heap is essential for grasping concepts like variable scope, object creation, memory management, threads, and exception handling.

When a JVM launches, it allocates a portion of memory from the underlying operating system to execute your Java program. The amount of memory and whether you can adjust it depend on the JVM version and the platform you’re using. In most cases, you won’t have control over this.

The Java Virtual Machine (JVM) allocates memory in two main areas: the area of memory where objects live (the heap) and the one where method invocations and local variables live (the stack).

Local variables

Local variables, including method parameters, are declared within a method. They are temporary and exist only for the duration of the method’s execution on the stack.

Consider below example

--

--

Namrata
Namrata

Written by Namrata

Engineering @Microsoft A software developer writing her daily bits . https://www.linkedin.com/in/namrataagarwal5/

No responses yet