Memory Areas

This lesson explains the various areas of memory for a Java application.

We'll cover the following...

Question # 1

What kinds of memory does the JVM manage?

The JVM manages two kinds of memory: heap and non-heap memory, both created when it starts.

Non-heap memory is all the memory the JVM allocated for purposes other than the heap. The only information JVM provides on non-heap memory is its overall size. No detailed information on non-heap memory content is available.

Question # 2

What are Java Runtime Data areas?

The Java Virtual Machine defines various run-time data areas that are used during execution of a program. Some of these data areas are created on Java Virtual Machine start-up and are destroyed only when the Java Virtual Machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.

  1. (JVM) Stack: Stack memory is responsible for holding references to heap objects and for storing value types (primitive types), which hold the value itself rather than a reference to an object from the heap. Stack memory in Java is allocated per Thread. Therefore, each time a Thread is created and started, it has its own stack memory and cannot access another thread’s stack memory.

  2. Heap: The heap part of memory stores the actual object in memory. There exists only one heap memory (heap) for each running JVM process. Therefore, this is a shared part of memory regardless of how many threads are running. The heap memory is periodically garbage collected by the framework.

  3. Program Counter (PC) Register: The PC register holds the address of the current instruction. If the current method is native then the PC is undefined. All CPUs have a PC, typically the PC is incremented after each instruction and holds the address of the next instruction to be executed. The JVM uses ...