2

How can I calculate the maximum "maximum heap size" avalable for java application

  • in Windows using cmd commands?
  • in Linux using sh commands?

I want to set -Xmx value as large as possible.

I want to be sure that java will not complain that -Xmx value is too large.


From a linked answer to another question I learned the following values for 32-bit Java:

OS: Windows XP SP2, JVM: Sun 1.6.0_02, Max heap size: 1470 MB
OS: Windows XP SP2, JVM: IBM 1.5, Max heap size: 1810 MB
OS: Windows Server 2003 SE, JVM: IBM 1.5, Max heap size: 1850 MB
OS: Linux 2.6, JVM: IBM 1.5, Max heap size: 2750 MB 

How do I automatically detect which OS and which Java is installed? OK, I can default to the minimum value - 1470 MB. But how can I be sure that in newer versions of Java this value will not shrink?

The question also remains open for 64-bit Java.

Community
  • 1
  • 1
utapyngo
  • 6,946
  • 3
  • 44
  • 65

3 Answers3

1

Try to run java with 2 gigabytes or memory, decrease the amount, repeat until java runs.

Linux:

#!/bin/sh
let mem=2000
while [ 1 ]
do
    java "-Xmx"$mem"m" -version > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
        break
    fi
    let mem=$mem-100
    if [ $mem -lt 100 ]
    then
        #// something went wrong with our test
        let mem=700
        break
    fi
done
echo $mem 

Windows:

@echo off
setlocal enabledelayedexpansion
set mem=2000
:decmem
set /a mem=mem-100
if !mem! == 0 (
    rem // something went wrong with our test
    set mem=700
    goto start
)
java -Xmx!mem!m -version > nul 2> nul
if errorlevel 1 goto decmem
:start
echo !mem!
utapyngo
  • 6,946
  • 3
  • 44
  • 65
0

This question will never get a direct answer. There are a lot of variables in deciding maximum heap size. And of the top of my head, i can think of:

  • OS you are using
  • Ram size of your machine
  • applications that are running. or other applications RAM usage

So what i can suggest is try to find the max. heap size your java application needs for running at most of the time.

Still if you need to find the max size, here are the facts i have heard or read about

  • XP uses 1 GB RAM
  • Vista/Seven uses at least 2-3 GB for smooth running
  • I think Linux ram size was arround 1-2 GB

Now we can hope that your JAVA VM wont run the full time. So we can ignore the other machines running in your system for the time being. So your RAM size - OS RAM size would give you your required heap size.

Still i would suggest you decide your heap size depending on your java application requirement.

Naveen Babu
  • 1,584
  • 1
  • 14
  • 35
  • My application itself requires not much memory, but the files that the users open with it must be processed in memory, and the size of that files varies from 1 kilobyte to hundreds of megabytes. – utapyngo Sep 25 '11 at 05:07
  • @eye one of my friend had done logic for reading data from text file and loading it to the Oracle DB. there also he had data in Gbs. And as i remember he had used a similar logic as in [link](http://www.daniweb.com/software-development/java/threads/30336). The program can be configured using changing the new byte[1024] array size. It improved his memory management for program and also the speed of reading. Hope this helps – Naveen Babu Sep 25 '11 at 10:17
  • @eye also 64bit OS require more heap size to run. But there is some jvm setting to have the heap memory usage advantage of 32-bit OS. please search for this also if you are using a 64bit windows. It can be found in IBM site also apart from oracle or sun site. – Naveen Babu Sep 25 '11 at 10:21
0

There are a number of things to consider. There are two real limits for memory, first is the limit of maximum addressible memory and second is the total available virtual memory in your environment.

1) The virtual memory is (usually) made up of RAM plus swap space (disk-backed virtual memory). This memory pool is used by all processes running on your machine (including the OS). The total memory used must be less than the total available virtual memory (attempts to exceed this limit will result in errors in one or more processes).

2) The maximum addressible memory is a per-process limit on the amount of memory that can be mapped to that process. Some of the per-process memory map may be reserved for the OS/kernel. The main factor for what will be available is whether the process is 32- or 64-bit. Typically 64-bit processes don't need to worry about the limit (yet ;)) as it is very high (although this does depend on architecture).

Threoretically, 32-bit processes can address up to 4GigaBytes and 64-bit processes can address up to 16ExaBytes (in practice most 64-bit processors do not support the full amount).

Let's talk 32-bit from now on...

The OS/kernel will usually reserve a chunk of address space for its own use. For example, by default Windows reserves 2GB of address space (although there is a switch that will reduce this to 1GB). The Linux kernel will usually reserve 1GB (although this can be reduced to almost nothing with a kernel change).

So, this leaves you with 2GB on Windows 32-bit and 3GB on Linux 32-bit for your Java VM. Consider that there is a bunch of memory the JVM needs that is NOT part of the Java Heap, so your heap will have to be somewhat smaller than these amounts - how much will vary depending on your application and the JVM implementation.

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
  • Also, a server running on a JVM whose current in-use Java heap is not entirely in RAM (ie not paged to disk) will not perform very well. – Mike Tunnicliffe Sep 24 '11 at 20:05