- Native Memory Allocation in Java
- How to Allocate the Native Memory
- Go Straight to the Example
- Java Stack and Heap: Java Memory Allocation Tutorial
- What is Heap Memory?
- Memory Allocation in Java
- Difference between Local and Instance Variable
- Difference between Stack and Heap
- Memory Allocation in Java
- How Does Memory Allocation work in java?
- 1. Heap Memory
- 2. Stack Memory
- Example of Memory Allocation in Java
- Recommended Articles
Native Memory Allocation in Java
Join the DZone community and get the full member experience.
From time to time, we run into a memory problem that is not related to Java Heap but Native Memory. Let’s imagine the situation where we have a running container that is restarted once per day. We look at Prometheus/Grafana to check the reason behind this issue. However, we don’t spot any problem with Java Heap size (exported via JMX) and start blaming our container scheduler to do any inconvenient operations with our containers :). The real problem can be hidden a bit deeper — in Native Memory.
Do you use any native memory allocator in your code or do you use any library that stores its internal state outside the Java Heap? It could be whatever third-party code that tries to minimize GC, such as I/O libraries or databases/caches keeping data in memory inside your process.
In this article, I will go through some basics: how the native-memory is allocated in those libraries and mainly how to notice that something in your code started allocating native memory that could lead to killing your container.
How to Allocate the Native Memory
For a long time, the only efficient way to allocate memory from Java was using sun.misc.Unsafe . From the very beginning, Java architects warn that this library is only for internal use-cases, and there is no intention to keep it backward compatible as an official API. However, developers wanted the best for their libraries and kept using Unsafe in their code. The big bang came with Java 9 and an introduction to the Java Module System. Unsafe was automatically put inside the jdk.unsupported module, and it was announced that the module will become visible for external code depending on it for some time. But, the intention is to provide an official API, and the libraries will be forced to migrate to it.
Another way to allocate native memory is by ByteBuffer. There are two implementations: HeapByteBuffer and DirectByteBuffer . While HeapByteBuffer keeps data inside a byte array allocated on the heap, DirectByteBuffer is backed by native memory and is primarily used for transferring data between JVM and Kernel. However, some libraries doing I/O implemented their own libraries for dealing with the native memory, such as Netty (ByteBuf — maven) or Aeron (subproject Agrona), especially because of two reasons: a custom collecting of unused memory (Netty uses an implementation of manual reference-counting collector) and a more convenient API.
You can imagine, there was no reason for the library’s owners to migrate from Unsafe. Therefore, Project Panama, dealing with an Interconnection of JVM and Native Code, came with something new and shiny. The first output of this project was JEP 370: Foreign Memory Access API, which provided an alternative way to access native memory that was supported by a JIT Compiler to optimize it as much as possible to be close to the efficiency of Unsafe. As an addition, it also brought a new API for designing a native memory layout to avoid «counting» addresses and offsets manually and provide an explicit way of deallocating the memory in the source code (e.g. using try-with-resources). It’s available as an incubator in Java 14. Feel free to try it out and give feedback!
Go Straight to the Example
After a short introduction, let’s use the simplest way to allocate the native memory, and let’s monitor what it actually does.
Java Stack and Heap: Java Memory Allocation Tutorial
Stack in java is a section of memory which contains methods, local variables, and reference variables. Stack memory is always referenced in Last-In-First-Out order. Local variables are created in the stack.
What is Heap Memory?
Heap is a section of memory which contains Objects and may also contain reference variables. Instance variables are created in the heap
Memory Allocation in Java
Memory Allocation in Java is the process in which the virtual memory sections are set aside in a program for storing the variables and instances of structures and classes. However, the memory isn’t allocated to an object at declaration but only a reference is created. For the memory allocation of the object, new() method is used, so the object is always allocated memory on the heap.
The Java Memory Allocation is divided into following sections :
This division of memory is required for its effective management.
- The code section contains your bytecode.
- The Stack section of memory contains methods, local variables, and reference variables.
- The Heap section contains Objects (may also contain reference variables).
- The Static section contains Static data/methods.
Difference between Local and Instance Variable
Instance variable is declared inside a class but not inside a method
Local variable are declared inside a method including method arguments.
Difference between Stack and Heap
Click here if the video is not accessible
Let’s take an example to understand this better.
Consider that your main method calling method m1
In the stack java, a frame will be created from method m1.
The variable X in m1 will also be created in the frame for m1 in the stack. (See image below).
Method m1 is calling method m2. In the stack java, a new frame is created for m2 on top of the frame m1.
Variable b and c will also be created in a frame m2 in a stack.
Same method m2 is calling method m3. Again a frame m3 is created on the top of the stack (see image below).
Now let say our method m3 is creating an object for class “Account,” which has two instances variable int p and int q.
Here is the code for method m3
The statement new Account() will create an object of account in heap.
The reference variable “ref” will be created in a stack java.
The assignment “=” operator will make a reference variable to point to the object in the Heap.
Once the method has completed its execution. The flow of control will go back to the calling method. Which in this case is method m2.
The stack from method m3 will be flushed out.
Since the reference variable will no longer be pointing to the object in the heap, it would be eligible for garbage collection.
Once method m2 has completed its execution. It will be popped out of the stack, and all its variable will be flushed and no longer be available for use.
Eventually, the flow of control will return to the start point of the program. Which usually, is the “main” method.
What if Object has a reference as its instance variable?
public static void main(String args[]) < A parent = new A(); //more code >class A < B child = new B(); int e; //more code >class B < int c; int d; //more code >
In this case , the reference variable “child” will be created in heap ,which in turn will be pointing to its object, something like the diagram shown below.
- When a method is called, a frame is created on the top of the stack.
- Once a method has completed execution, the flow of control returns to the calling method and its corresponding stack frame is flushed.
- Local variables are created in the stack
- Instance variables are created in the heap & are part of the object they belong to.
- Reference variables are created in the stack.
Memory Allocation in Java
Memory allocation in java can be defined as a process of assigning storage to java programs or services. Memory allocation in java is done in JVM (Java Virtual Machine) memory, broadly divided into a heap and non-heap memory. This article will detail how heap memory and stack memory, equivalent to non-heap memory, are allocated to Java programs.
How Does Memory Allocation work in java?
As we know, that java is an object-oriented language; therefore, all objects created in java are stored in JVM (Java virtual machine). JVM memory is divided into the following parts:
1. Heap Memory
The java runtime uses Heap Memory to allocate memory to objects and classes while executing a java program. Whenever an object is created in java, it gets stored in heap memory. In addition, a garbage collection process runs on heap memory to free up unnecessary space; garbage collection removes those objects from the heap area that does not have any references. Heap memory in java is divided into the following parts:
- Young Generation: This is the part where all newly created objects are placed. When this part of the java heap gets filled up, a minor garbage collection occurs to free up space.
- Old Generation: All objects left in memory after minor garbage collection are moved into the old generation. Therefore this is the part of heap memory where long-living objects are present.
- Permanent Generation: This part of JVM contains native and static methods that provide metadata for running java applications.
Here are some important points regarding java heap memory:
- If Heap space gets full, the OutOfMemory error is thrown by java.
- Access to Heap memory is slow as compared to stack memory.
- Heap memory is much more in size as compared to stack memory.
- Heap memory is not thread-safe as all objects share it.
- Automatic deallocation is not present in heap memory as it needs a garbage collector to free up space.
2. Stack Memory
As the name signifies, stack memory is based on LIFO (last in, first out) principle. Stack memory is used for static memory allocation, and each executing thread in a java program has its stack memory. Whenever a Java method is called, a new block is created in java stack memory to hold local or intermediate variables and references to other objects in the method. As soon as the execution of the method is completed, the block of memory in the stack becomes empty and used by the next method. Therefore, the Stack memory size is less than heap memory. Here are some of the important features of stack memory.
- Stack Memory grows and shrinks as new methods are added and removed to stack memory.
- Stack memory gets automatically allocated and deallocated after the method completes its execution.
- Access to stack memory is fast as compared to heap memory.
- Whenever stack memory gets full, an exception called stack overflow exception is thrown by java.
- Stack memory is thread-safe as each thread has its stack memory.
Here is a small comparison of stack and heap memory in java:
Heap Memory | Stack Memory |
The entire application uses heap memory during its runtime. | The application in parts uses stack memory. That means it is used one at a time during thread execution. |
Heap memory is larger than stack memory. | Stack memory is small as compared to heap memory. |
All objects created during the application are stored in heap memory. | Stack memory only stores local variables and references to objects. |
Access to heap memory is slow. | Access to stack memory is fast as compared to heap memory. |
Heap memory is allocated by creating new objects and gets deallocated by a garbage collector. | Stack memory is automatically allocated and deallocated with the end in method execution. |
Heap memory stays as long as the application is running. | Stack memory stays only until a method is executing. |
Example of Memory Allocation in Java
Now we will see a java example showing how memory is allocated
package com.edubca.javademo; class StudentData < int rollNumber; String name; public StudentData(int rollNumber, String name) < super(); this.rollNumber = rollNumber; this.name = name; >public int getRollNumber() < return rollNumber; >public void setRollNumber(int rollNumber) < this.rollNumber = rollNumber; >public String getName() < return name; >public void setName(String name) < this.name = name; >> public class Main < public static void main(String[] args) < int name = "Yash"; StudentData s = null; s = new StudentData(id, name); System.out.println("Student Id is " + s.getRollNumber()); System.out.println("Student Name is " + s.getName()); >>
Now we will see how memory is allocated in the above program:
1. In the Main class, after entering the main method, since id, the name is local variables, a space in stack memory is created in the following way:
- Integer id having primitive value will be stored in stack memory.
- Reference of StudentData object s is stored in stack memory pointing to the original Student object, which is stored in heap memory.
2. Call to StudentData class constructor will be added to the top of stack memory. As a result, the following will be stored:
- Reference to calling object.
- Integer variable id having value 11.
- Reference of String type variable name will point to the object stored in the string pool in heap memory.
3. Two instance variables declared in the StudentData class will be stored in heap memory.
Recommended Articles
This is a guide to Memory Allocation in Java. Here we discuss how memory allocation is achieved in java with the help of stack and heap memory. You may also have a look at the following articles to learn more –
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8