Java heap space Xmx Xms parameters ignored

2019-04-06 12:35发布

问题:

I have a .JAR that apparently uses up too much memory, and throws an exception "Java heap space" (or something similar).

So I tried running the .JAR via the CMD like this:

C:\MyFolder>javaw -jar MyJar.jar -Xms64m -Xmx128m

That did not solve the problem. Same error. Now, when I checked the Processes tab in the windows task manager, I noticed that the process of my jar has a lot less memory than what i asked for (same as running it without the parameters).

Why is it ignoring the parameters?

Also, I think the exception is thrown around the time the process reaches 100mb of memory usage. Is it possible that the GC is trying to free up the memory and that's what causes the problem? Is there any parameter I can set for the GC to prevent that?

Thanks, Malki :)

回答1:

The Command

javaw -jar MyJar.jar -Xms64m -Xmx128m

will use -Xms... and -Xmx... as parameters to the "main(String[] args)" method.
Parameters to the JVM must be passed before the -jar part:

javaw -Xms64m -Xmx128m -jar MyJar.jar

The reason for that can be seen when we execute "java -version" :

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

Where your parameters -Xms... and -Xmx... are options to the JVM.