Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime.maxMemory() and -Xmx

Tags:

java

jvm

I was expecting Runtime.maxMemory() to return exactly the -Xmx but it returns a lower value. So what does it return?

like image 832
LostInComputer Avatar asked Dec 05 '12 18:12

LostInComputer


People also ask

How to get Runtime object in Java?

getRuntime() method returns the runtime object associated with the current Java application. Most of the methods of class Runtime are instance methods and must be invoked with respect to the current runtime object.

What are Runtime classes in Java?

Runtime class is a subclass of Object class, can provide access to various information about the environment in which a program is running. The Java run-time environment creates a single instance of this class that is associated with a program.

What is Runtime getRuntime exec in Java?

getRuntime() Returns the instance or Runtime object associated with the current Java application. halt(int status) Forcibly terminates the currently running Java virtual machine. This method never returns normally.


3 Answers

The interpretation of the -Xmx flag is VM-dependent. Some VMs, including HotSpot, enforce a lower bound on the effective value of this option. The CCC proposal should not have mentioned the -Xmx flag in this way.

Reference

like image 168
invariant Avatar answered Oct 02 '22 05:10

invariant


Returns the maximum amount of memory that the Java virtual machine will attempt to use, jvm may not use all the memory you have defined as -Xmx parameter for just heap.

like image 22
kosa Avatar answered Oct 02 '22 07:10

kosa


-Xmxn

Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. The upper limit for this value will be approximately 4000m on Solaris 7 and Solaris 8 SPARC platforms and 2000m on Solaris 2.6 and x86 platforms, minus overhead amounts. Examples:

       -Xmx83886080
       -Xmx81920k
       -Xmx80m

maxMemory()

Returns the maximum amount of memory that the Java virtual machine will attempt to use. If there is no inherent limit then the value Long.MAX_VALUE will be returned.

like image 41
Terry Li Avatar answered Oct 02 '22 07:10

Terry Li