Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java -Xmx, Max memory on system

Tags:

My Java application runs another Java application, by running the process "java -jar j.jar". J.jar is known to use a LOT of memory depending on the dataset it is given, and often gets an OutOfMemoryError heap. So I want to use -Xmx on it, so that I can allocate as much memory as possible (or close to). I was thinking of getting the total memory on the system, then specifying 80-90% of that in -Xmx.

Is there any solution to my problem? And, how does my solution sound?

Edit: I cant reduce the memory consumption as the memory being used is by Java's built-in pack200 compression, which I am using to pack some JAR files.

like image 632
Amandeep Grewal Avatar asked Jul 27 '09 21:07

Amandeep Grewal


People also ask

How much memory can Java handle?

From the Java Tuning white paper: For a 32-bit process model, the maximum virtual address size of the process is typically 4 GB, though some operating systems limit this to 2 GB or 3 GB.

What is the maximum Java heap size for Windows?

On most modern 32-bit Windows systems the maximum heap size will range from 1.4G to 1.6G.

How much memory should I allocate to Java?

Therefore we recommended that physical memory availability for each JVM be 4096 MB;0.5 GB is for JVM allocation and 512 MB is for overhead. This simplified calculation means that a 64-bit system with 16 GB physical memory can run 3 JVMs.


2 Answers

The limit for -XmX is -Xmx1500m on 32 bit windows. Shared libraries get in the way of a bigger heap. You'll need about 2Gb of RAM to do this.

On non-windows OSes you can go bigger, and 64Bit JVM's are capable of a LOT more.

Windows XP will not let you have more than 3Gb of RAM ( doesn't care if you have 4Gb physical, ever since XP SP3) Vista may be different YMMV.

I've tried -Xmx4000M on a 64 bit JVM on 64 bit Linux and it was fine. considering I had 6Gb of physical ram, it was not a big request.

Your 80% idea is interesting, but my test systems run higher percentages than that without ill effect. (As long as you don't try doing anything else.)

And the other commenter is right, paging out your JVM's in-memory image is not quick. Later JVM's are better at doing this less messily ( but they have better garbage collectors too)

If you can't reduce your memory consumption – and I know how hard that is – then have lots of physical ram and allocate most of it.

like image 140
Tim Williscroft Avatar answered Sep 20 '22 08:09

Tim Williscroft


Depending on your OS, this might work for getting the free and available memory size:

java.lang.management.OperatingSystemMXBean mxbean = java.lang.management.ManagementFactory.getOperatingSystemMXBean(); com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean; long freeMemory = sunmxbean.getFreePhysicalMemorySize(); long availableMemory = sunmxbean.getTotalPhysicalMemorySize(); 

From there, you can figure out 80-90% and launch your jar with the max memory size you want.

I don't know that this works with all OS's (i.e. Windows), but it worked when I tested it with both OSX and Linux.

like image 33
joe p Avatar answered Sep 23 '22 08:09

joe p