rajvineet
rajvineet

Reputation: 319

Memory Management in Java - Metaspace/native memory

Is native memory (Metaspace) for a java application gets space from heap memory or there is completely a different set of memory dedicated for it?

I think it uses the memory which is used by OS to manage all their applications, but not clear.

Upvotes: 1

Views: 963

Answers (1)

Wasi Ahmad
Wasi Ahmad

Reputation: 37721

Java Heap Space

Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create any object, it’s always created in the Heap space. Garbage Collection runs on the heap memory to free the memory used by objects that doesn’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application.

Java Stack Memory

Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as method ends, the block becomes unused and become available for next method. Stack memory size is very less compared to Heap memory.

Difference between Java Heap Space and Stack Memory

  • Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution.

  • Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.

  • Objects stored in the heap are globally accessible whereas stack memory can’t be accessed by other threads.

  • Memory management in stack is done in LIFO manner whereas it’s more complex in Heap memory because it’s used globally. Heap memory is divided into Young-Generation, Old-Generation etc, more details at Java Garbage Collection.

  • Stack memory is short-lived whereas heap memory lives from the start till the end of application execution.

  • We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory. We can use -Xss to define the stack memory size.

  • When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error.

  • Stack memory size is very less when compared to Heap memory. Because of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory.

Reference:

This is the basic of Java Memory Management but going through the reference material should give you comprehensive idea.

Edit

Thanks to @rajvineet to pointing out to this great article on how the JVM uses native memory on Windows and Linux. Specially the section, how the Java runtime uses native memory describes everything clearly.

Upvotes: 1

Related Questions