JAVA MEMORY MANAGEMENT
In this blog, you will know everything about memory management in Java.
But, before starting, what is actually memory management?
Well, it literally means managing the memory. Allocating and delocating the memory in RAM is called memory management. Basically, the JVM takes care of all the allocations and dealocations.
The operating system allocates some amount of memory in RAM to the JVM, and the amount typically is based on several factors, such as the system's available RAM and the JVM’s configuration settings, which we will talk about a bit later.
To perform all tasks efficiently, JVM uses 3 different runtime data areas.
1. Heap Area
2. Stack Area
3. Perm Gen/Meta Space
These areas perform different functionalities of memory management.
For understanding memory management in a better way, let us take a code snippet and map what comes under what.
package MemoryManagement;
public class Employee {
int organizationId = 101;
String organizationName = "ABC";
String ceoName = "John";
int departments = 2;
static double netWorth = 500.00;
public static void main(String[] args) {
int employeeId = 9164;
String employeeName = "Stuart";
double salary = 5000.00;
Employee employee1 = new Employee();
employee1.getOrganizationId();
System.out.println(Employee.getNetWorth());
Employee employee2 = new Employee();
employee2.deleteEmployee(employeeId);
}
public int getOrganizationId() {
return organizationId;
}
public void deleteEmployee(int employeeId) {
// delete employee code
}
public static double getNetWorth() {
return netWorth;
}
}HEAP AREA
It is responsible for dynamically allocating and managing objects during program execution. JVM allocates a fixed amount of memory to the heap, which is typically around 768 MB, which can be adjusted using configuration settings.
It is used to store all kinds of objects that we use in our programme, including arrays, collections, and instances of the class.
Heap Area also stores String pool where all the String literals are maintained.
The performance of the Heap area is less. Sometimes, it is also termed the ROM of JVM.
When a new keyword is used, an object is assigned a space in the heap, but the reference to the same exists on the stack.
Usually, the kind of exceptions that we face when heap memory is full is
java.lang.OutOfMemoryErrorGarbage collection in the heap area is mandatory.
In our example code, let's look at how the data is stored in the heap area.
STACK AREA
JVM allocates a fixed amount of memory to the heap, which is typically around 1 MB, which can be adjusted using configuration settings.
Get Perla Pawan Phanieswar’s stories in your inbox
Join Medium for free to get updates from this writer.
Method call frames and local variables are managed in stack memory. Every time a method is called, a new frame with method-specific information such as parameters, return addresses, and local variables is pushed onto the stack.
Primitive data types and object references—not the actual objects themselves—are stored in the stack.
Data on the stack has a brief lifespan that is dependent on how the method is executed. The frame of the method is removed from the stack upon completion, and the associated data is automatically deallocated.
Usually, the kind of exceptions that we face when Stack memory is full is
java.lang.StackOverFlowErrorThe number of stacks formed in the Stack Area depends on the number of threads running in the programme at the given point in time.
The number of stacks = The number of threads running.
In our case, only one thread is running, as at the start of execution of the programme, only one thread is created.
The performance of the Stack area is high when compared to the Heap area. Sometimes, it is also termed RAM of JVM.
In our example code, let’s look at how the data is stored in the stack area.
PERMGEN/METASPACE
Before Java 1.8, this PERMGEN used to be present in the heap area as a subpart. After 1.8, it was separated as a different area and was named METASPACE.
It is used to store all the static methods, static variables, and static blocks, along with method areas and class structures. If we use inner classes or declare any other class for usage in the main programme, all the structures of the class will be stored in the METASPACE.
CONFIGURATION SETTINGS
To increase the heap memory size, you can go to configurations of the program if you are using Eclipse.
Go to the arguments and add -Xmx800m in the VM arguments section, and click run. This increases the heap area size to 800 MB. If you want even more, add the required number in the command followed by m.
To increase the stack memory size, you can go to configurations and in Arguments, add -Xss2m in the VM aruments, and click run. This increases the stack area size to 2 MB. If you want more stack space for your application or you feel there is a lot of recursion in your code, you can increase the size up to how much you might need.
You can use the following command: -Xss5m
If you want even more, add the required number in the command followed by m.
Thanks for reading this blog. I hope you have understood the memory management in Java.
References :
https://sematext.com/glossary/jvm-heap/
https://www.geeksforgeeks.org/java-virtual-machine-jvm-stack-area/
https://www.javatpoint.com/memory-management-in-java









