user3787266
user3787266

Reputation: 21

JVM won't use as much memory as I tell it to

I am running a memory intensive application. Some info about the environment:

Here is the exact command I am issuing:

java -Xmx9000m -jar "ale.jar" testconfig

I have run the program with same exact data, config, etc. on several other systems, and I know that the JVM uses (at its peak) 6 GB of memory on those systems. However, I am getting an OutOfMemory error. Furthermore, during the execution of the program, the system never drops below 8.5 GB of free memory.

When I output Runtime.getRuntime().maxMemory() during execution, I get the value 3044540416, i.e. ~ 3 GB.

I don't know whether it is relevant, but this is a Google Compute Engine instance.

The only explanation I can think of is that there may be some sort of system restriction on the maximum amount of memory that a single process may use.

Upvotes: 2

Views: 127

Answers (2)

Stephen C
Stephen C

Reputation: 718758

The only explanation I can think of is that there may be some sort of system restriction on the maximum amount of memory that a single process may use.

That is one possible explanation.

Another one is that you are attempting to allocate a really large array. The largest possible arrays are 2^31 - 1 elements, but the actual size depends on the element size:

  • byte[] or boolean[] ... 2G bytes
  • char[] or short[] ... 4G bytes
  • int[] ... 8 Gbytes
  • long[] or Object[] ... 16 Gbytes

If you allocate a really large array, the GC needs to find a contiguous region of free memory of the required size. Depending on the array size, and how the heap space is split into spaces, it may be able to find considerably less contiguous space than you think.

A third possibility is that you are getting OOMEs because the GC is hitting the GC Overhead limit for time spent running the GC.


Some of these theories could be confirmed or dismissed if you showed us the stacktrace ...

Upvotes: 1

Jeroen Kransen
Jeroen Kransen

Reputation: 1471

-Xmx will only set the maximum assigned memory. Use -Xms to specify the minimum. Setting them to the same value will make the memory footprint static.

Upvotes: 1

Related Questions