3
#!/bin/bash
#
module add apps/java/1.6 
java -Xmx1024m HelloWorld

I need to set -XmxYm where Y should be the 95% of available memory on the system in Mb.

siamii
  • 23,374
  • 28
  • 93
  • 143
  • 1
    What is the definition of "available memory"? Do you mean RAM available to the operating system? Do you mean virtual memory available to the process? Or what? – David Schwartz Mar 27 '12 at 10:44
  • @DavidSchwartz mean all the memory that Java could use and not make the system crash. I think that's RAM available to OS. – siamii Mar 27 '12 at 10:50
  • 1
    Note that you can't always allocate as much memory for a JVM as possible. Some hints: http://stackoverflow.com/questions/171205/java-maximum-memory-on-windows-xp, http://stackoverflow.com/questions/1190837/java-xmx-max-memory-on-system, http://stackoverflow.com/questions/3030263/increasing-the-jvm-maximum-heap-size-for-memory-intensive-applications – Thomas Mar 27 '12 at 10:58
  • The heap is just a portion of the memory Java uses. In any case you want it to be the memory not used by the OS and reasonably sized disk cache. If you leave the default option it will use 1/4 of memory (with the server JVM) – Peter Lawrey Mar 27 '12 at 11:33
  • @bizso09: No, because a process can use more memory than available RAM if the system has available swap. If the issue is not making the system crash, you need to take swap/paging into account as well. – David Schwartz Mar 27 '12 at 18:08

2 Answers2

3

Here is a script that calls Java with the required heap size.

#!/bin/bash

# Total memory in KB
totalMemKB=$(awk '/MemTotal:/ { print $2 }' /proc/meminfo)

# Percentage of memory to use for Java heap
usagePercent=95

# heap size in KB
let heapKB=$totalMemKB*$usagePercent/100

# heap size in MB
let heapMB=$heapKB/1024

module add apps/java/1.6 
java -Xmx${heapMB}m HelloWorld

I strongly advise you to use a lower usagePercent, since a Java application uses more memory than the heap size (for eg. for the PermGen).

Filip
  • 1,451
  • 1
  • 11
  • 19
0

As Peter Lawrey said, allocating 95% of memory to the heap is probably not wise.

You can, however, determine memory size by reading the "/proc/meminfo" file. Try

cat /proc/meminfo
MemTotal:     32958996 kB
MemFree:      23461744 kB
Buffers:        133772 kB
Cached:        1651888 kB
SwapCached:          0 kB
Active:        8460504 kB
Inactive:       740048 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:     32958996 kB
LowFree:      23461744 kB
SwapTotal:    34996216 kB
SwapFree:     34996216 kB
Dirty:             372 kB
Writeback:           0 kB
AnonPages:     7415344 kB
Mapped:          61260 kB
Slab:           206896 kB
PageTables:      24320 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:  51475712 kB
Committed_AS: 18514524 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    282388 kB
VmallocChunk: 34359454135 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB
Paul Cager
  • 1,910
  • 14
  • 21