Restricting max heap size of application running under Intellij IDEA

18 views Asked by At

Problem

I am trying to restrict max heap size of the application running under Intellij IDEA - in order to detect memory problems as early as possible.

So far I have tried to pass custom JVM option in Run/Debug Configurations > CLI arguments to your application.

-Xmx128M

This resulted in Intellij producing the following command:

/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/bin/java \
  -Dfile.encoding=UTF-8 \
  -classpath /home/username/ws/projectname/target/classes:/home/username/.m2/repository/org/jetbrains/annotations/23.0.0/annotations-23.0.0.jar:/home/username/.m2/repository/org/reflections/reflections/0.9.10/reflections-0.9.10.jar:/home/username/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/home/username/.m2/repository/com/google/code/findbugs/annotations/2.0.1/annotations-2.0.1.jar:/home/username/.m2/repository/com/github/matejtymes/javafixes/1.3.2/javafixes-1.3.2.jar:...:/home/username/.m2/repository/org/bgee/log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1/1.16/log4jdbc-log4j2-jdbc4.1-1.16.jar com.companyname.rootpackage.MainApplicationClass \
  -Xmx128M

Which does not restrict max heap size to 128MB. But this would have:

/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/bin/java \
  -Xmx128M \
  -Dfile.encoding=UTF-8 \
  -classpath /home/username/ws/projectname/target/classes:/home/username/.m2/repository/org/jetbrains/annotations/23.0.0/annotations-23.0.0.jar:/home/username/.m2/repository/org/reflections/reflections/0.9.10/reflections-0.9.10.jar:/home/username/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/home/username/.m2/repository/com/google/code/findbugs/annotations/2.0.1/annotations-2.0.1.jar:/home/username/.m2/repository/com/github/matejtymes/javafixes/1.3.2/javafixes-1.3.2.jar:...:/home/username/.m2/repository/org/bgee/log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1/1.16/log4jdbc-log4j2-jdbc4.1-1.16.jar com.companyname.rootpackage.MainApplicationClass

I have changed paths and project names, and cut-out large portion of the jars added to classpath.

I know that the first command does not restrict maximum heap size because I have seen the output of Runtime.getRuntime().maxMemory() - which returned ~5GB (1/4th of total RAM available on my development machine - that's the default JDK setting).

I know that the second command does restrict maximum heap size because I have tried it outside of Intellij. The output of Runtime.getRuntime().maxMemory() had changed accordingly.

Questions

  1. Is it normal for -Xmx option to work only it it's defined before the classpath?
  2. Is it possible to restrict max heap size of the application in other way than by using CLI arguments to your application.?

Versions used

Java

openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)

Intellij IDEA

IntelliJ IDEA 2023.2.2 (Community Edition)
Build #IC-232.9921.47, built on September 12, 2023

OS

PRETTY_NAME="Debian GNU/Linux bookworm/sid"
0

There are 0 answers