ArticleGeneralJava

JVM Structure Overview. Memory Distribution

The JVM greatly simplifies and automates memory handling in Java programs. The programmer simply declares variables without even worrying about how the occupied memory will be freed.

Although in some cases a random memory leak can occur, where the program will just start devouring memory in large amounts, in most cases everything happens automatically and without problems.

Memory Types Allocation

Generally speaking, the JVM allocates memory into a stack and a heap. Every Java program has at least one parent process and every process has one main thread. In turn, each thread has its own stack. This stack often contains all local variables of primitive types and data references. The stack is filled and released automatically.

Why do We Need a Stack?

As we described above, the stack is mainly needed for two data types. All the data in the stack has certain variable visibility, also called scope.

If the stack overflows, a java.lang.StackOverFlowError occurs.

Memory Types in the Stack

Primitive types. These types usually store the data itself, not references.
Heap references. References themselves occupy a minimal amount of memory, although they can refer to large blocks of memory.

What is a Heap

The heap is the main partition of the JVM in terms of memory size. This section stores the data of classes and objects.

As already mentioned all references to objects and classes are stored in a stack. Any thread can access the data, so you must make sure that the data is thread-safe.

The heap itself is divided into several partitions, which help process the data more efficiently.

When the heap overflows, a java.lang.OutOfMemoryError occurs. Unlike the stack, data is not automatically freed, so if the code is written with errors, you can accidentally get a memory leak when the program starts consuming memory in significant amounts.

Heap generations

Young generation – all the newly created objects will go to this section.
Permanent generation – this section is responsible for service information about classes and objects.
Old generation – all the data that has existed for a long period of time gets here from the new generation.

Hi, I’m Vlad

Leave a Reply

Your email address will not be published. Required fields are marked *