How to Increase Virtual Memory in Java

Sheeraz Gul Feb 02, 2024
  1. Use CMD to Increase Virtual Memory in Java
  2. Use Eclipse to Increase Virtual Memory in Java
How to Increase Virtual Memory in Java

The Java virtual machine allocates memory to each application. You must know how to manage the virtual memory when working with Java so your applications can function in a scalable manner.

This tutorial demonstrates how to increase the virtual memory in Java.

Use CMD to Increase Virtual Memory in Java

If your application requires a lot of memory, you can increase the memory to give more room to the garbage collector. There are two parameters which are needed to be adjusted when starting the JVM.

-Xms<size>  The initial Java heap size

The xms is the minimum Java heap size, and the maximum heap is:

-Xmx<size> the maximum Java heap size.

We can set the VM memory by using these parameters. Let’s try to increase the VM memory using the command line.

The default minimum memory is 2MB, and the maximum is 64MB. We will increase the minimum to 32MB and the maximum to 128MB.

Run the following command in cmd.

java Main.java -Xms32m -Xmx128m ...

The Main.java program is:

package delftstack;

public class Main {
  public static void main(String[] args) {
    System.out.println("Test Program");
  }
}

The output for the cmd command is:

C:\>cd C:\Users\Sheeraz\eclipse-workspace\Delftstack\src\delftstack
C:\Users\Sheeraz\eclipse-workspace\Delftstack\src\delftstack>java Main.java -Xms32m -Xmx128m ...
Test Program

We run the program with memory parameters. It will increase the memory to a minimum of 32MB and a maximum of 128MB.

Use Eclipse to Increase Virtual Memory in Java

If you are using Eclipse IDE, it is easier to increase the VM memory in Java. Follow the steps below.

  1. Open the Eclipse installation folder.

    Eclipse

  2. Open the eclipse.ini file and search the Xms and Xmx options.

    Eclipse INI

  3. Set the minimum memory by changing Xms and maximum memory using the Xmx option.

    Eclipse INI 1

We set the minimum memory to 256m and maximum memory to 2048m.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java JVM