Assignments/Article By Silvia Scano

Stack Memory In Java

Student Assignment

Flowchart

Published on 22, 2022

Every application that runs on a Java virtual machine (JVM) is allocated its share of memory before its startup. The JVM uses this memory and divides it into two parts: Stack memory and heap memory. In this blog we are trying to illustrate both.

Java Memory Area Parts

Stack memory

Stack memory management depends on the references used in the code and is maintained by the JVM only until the execution of that code.

It is the temporary memory allocation where local variables, reference variables are allocated memory when their methods are called. It contains references to the object are stored in a heap. After the execution of the method, the memory containing those variables is cleared. We can access this memory in Last In First Out Order. Allocation and deallocation is faster than heap memory. It is safer as data can only be accessed by the thread owner. If stack memory is full, then StackOverflowException is thrown by the JVM.

Heap Memory

When it comes to heap memory, the JVM manages it using garbage collection (GC). An appropriate garbage collector and application design are crucial to manage the heap memory. Memory allocations are

Stack operations

As explained earlier .Stack follows LIFO(Last in, First out) algorithm. The following three operatios are the mostly performed in the stack.
  • Push: Add element to the top of the stack
  • Pop: Remove an element from the top of the stack.
  • Top: Return the top element of the stack
  • isEmpty: If stack is empty it returns true ,else False

Student Assignment

The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites

Our Latest Blog

Why Retrieval-Augmented Generation Is the Next Big Step in Artificial Intelligence

Why Retrieval-Augmented Generation Is the Next Big Step in Artificial Intelligence

Retrieval-Augmented Generation (RAG) is an advanced AI technique that improves the accuracy and relevance of...
Read More
Building Smarter AI with the Model Context Protocol A New Standard for Contextual Intelligence

Building Smarter AI with the Model Context Protocol: A New Standard for Contextual Intelligence

The Model Context Protocol (MCP) is transforming how artificial intelligence interacts with data and tools...
Read More
Beyond the Buzz: How Prompt Engineering is Quietly Reshaping the Modern Workplace

Beyond the Buzz: How Prompt Engineering is Quietly Reshaping the Modern Workplace

The future of work isn’t about being replaced by AI, it’s about learning how to...
Read More