开发者

Run java application with maximum "maximum heap size" available

开发者 https://www.devze.com 2023-04-07 12:32 出处:网络
How can I calculate the maximum \"maximum heap size\" avalable for java application in Windows using cmd commands?

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 min开发者_如何转开发imum 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.


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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消