Week 125 — How can one set the heap size of a Java application?

Question of the Week #125
How can one set the heap size of a Java application?
2 Replies
lars
lars2w ago
By default, the JVM detects a suitable heap size given system and environment constraints but sometimes, developers may want to set it to a different value. For configuring the heap site, the most important option is the maximum heap size which can be set with the -Xmx argument. This argument must come before the main class or the -jar argument when calling the java command:
# run a JAR file with a maximum heap size of 1GB
java -Xmx1G -jar yourjar.jar

# run a class with a maximum heap size of 1GB
java -Xmx1G your.fully.qualified.MainClass
# run a JAR file with a maximum heap size of 1GB
java -Xmx1G -jar yourjar.jar

# run a class with a maximum heap size of 1GB
java -Xmx1G your.fully.qualified.MainClass
Similar to the maximum heap size, it is also possible to set an initial heap size. When the application starts, it will start with that heap size and allocate more memory if necessary until it reaches the maximum heap size.
# set minimum heap size to 100MB and run a JAR
java -Xms100M -jar yourjar.jar

# set both the initial heap size (100MB) and maximum heap size (1GB) when running a JAR
java -Xms100M -Xmx1G -jar yourjar.jar

# set both the initial and maximum heap size to the same value (1GB) to force it allocating that memory at the beginning
java -Xms1G -Xmx1G -jar yourjar.jar
# set minimum heap size to 100MB and run a JAR
java -Xms100M -jar yourjar.jar

# set both the initial heap size (100MB) and maximum heap size (1GB) when running a JAR
java -Xms100M -Xmx1G -jar yourjar.jar

# set both the initial and maximum heap size to the same value (1GB) to force it allocating that memory at the beginning
java -Xms1G -Xmx1G -jar yourjar.jar
📖 Sample answer from dan1st
lars
lars2w ago
Why the need? We get OutOfMemoryError when the heap memory runs out. When this happens the JVM throws the OutOfMemoryError and if it is uncaught the program crashes. Check JVM heap settings To see how your JVM chooses defaults:
$ java -XX:+PrintFlagsFinal -version | grep HeapSize
$ java -XX:+PrintFlagsFinal -version | grep HeapSize
You’ll see values like:
InitialHeapSize = 268435456 # 256 MB (-Xms)
MaxHeapSize = 4294967296 # 4 GB (-Xmx)
InitialHeapSize = 268435456 # 256 MB (-Xms)
MaxHeapSize = 4294967296 # 4 GB (-Xmx)
These are set ergonomically based on your machine’s specs. Example
import java.util.ArrayList;
import java.util.List;

public class HeapStressTest {
public static void main(String[] args) {
List<byte[]> memoryHog = new ArrayList<>();
int count = 0;

try {
while (true) {
// Allocate 10 MB chunks
memoryHog.add(new byte[10 * 1024 * 1024]);
count++;
System.out.println("Allocated " + count * 10 + " MB");
Thread.sleep(100); // Slow it down a bit
}
} catch (OutOfMemoryError e) {
System.err.println("💥 OutOfMemoryError after allocating " + count * 10 + " MB");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.List;

public class HeapStressTest {
public static void main(String[] args) {
List<byte[]> memoryHog = new ArrayList<>();
int count = 0;

try {
while (true) {
// Allocate 10 MB chunks
memoryHog.add(new byte[10 * 1024 * 1024]);
count++;
System.out.println("Allocated " + count * 10 + " MB");
Thread.sleep(100); // Slow it down a bit
}
} catch (OutOfMemoryError e) {
System.err.println("💥 OutOfMemoryError after allocating " + count * 10 + " MB");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
In this Example we keep allocating 10MB chunks of memory until we get the OutOfMemoryError. Solution We can set the initial as well as the max heap size by passing some VM options when we run the program. - -Xms : initial heap size - -Xmx : max heap size We can pass these options like this:
$ java -Xmx2g -Xms512m HeapStressTest
$ java -Xmx2g -Xms512m HeapStressTest
Or for more verbose GC info we can add this options as well
$ java -Xmx2g -Xms512m -XX:+PrintGCDetails HeapStressTest
$ java -Xmx2g -Xms512m -XX:+PrintGCDetails HeapStressTest
Submission from oneplusiota

Did you find this page helpful?