-1

Possible Duplicate:
How to set the maximum memory usage for JVM?

I am running a java program but when the memory usage reaches 26.1%, it does not increase any more and the program gets very slow.

What could be the problem? Does java have a limit of memory usage? or is there a limit of memory per app on linux?

java version "1.7.0_11" Java(TM) SE Runtime Environment (build 1.7.0_11-b21) Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)


Tasks: 126 total,   1 running, 125 sleeping,   0 stopped,   0 zombie
Cpu(s): 97.5%us,  0.4%sy,  0.0%ni,  2.1%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  33018800k total,  9522548k used, 23496252k free,    12100k buffers
Swap: 32764528k total,        0k used, 32764528k free,   391812k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3463 wonn24    20   0 8769m 8.2g 9.8m S 391.3 26.1 257:09.24 java
 3725 hong8e    20   0 14876 1148  872 R  0.3  0.0   0:01.81 top
    1 root      20   0  4088  972  720 S  0.0  0.0   0:00.71 init
    2 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT  -5     0    0    0 S  0.0  0.0   0:00.17 migration/0
    4 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    5 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    6 root      RT  -5     0    0    0 S  0.0  0.0   0:00.15 migration/1
    7 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/1
Community
  • 1
  • 1

2 Answers2

3

By default, the server version of Java has a maximum heap size of 1/4 of the main memory. With a little overhead of libraries and thread stacks this could easily be 26 or 27% of main memory.

When your JVM starts to fill up, it takes lower to perform a GC, and the lower free memory means it also occurs more often (so really bad) Ideally, you actual usage might as low as 40% of your maximum size. In your case you actual usage could be at least 7 GB.

I suggest you try increasing your maximum heap size to at least 16 GB, possibly 24 GB if you can spare the memory. i.e. -Xmx16g or -Xmx24g

Jesper
  • 202,709
  • 46
  • 318
  • 350
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • The [official page on java](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html) doesn't document the `mx` option. – Marko Topolnik Jan 18 '13 at 09:32
  • @MarkoTopolnik But it does say that `-X` options are "non standard". The `-mx` option is retained from Java 1.1 where it was a documented option. It was notionally dropped so the option could be non-standard, and yet the "non-standand" `-Xmx` option is the standard and the "standard" `-mx` option is non-standard. :P – Peter Lawrey Jan 18 '13 at 09:34
  • In the end, I use the simplest option as `-mx` like `-ms` is supported in Java 8 and is not going away any time soon. – Peter Lawrey Jan 18 '13 at 09:36
  • @PeterLawrey thanks for your answer. But when i run another program(The name of program is CRF++ that is a machine learning program written by c++ language) that program memory usage was also 26.1% and it dosen't increase anymore. c++ has also same memory limitation? or linux system has a limitation of main memory usage? – Hong-seok Kwon Jan 18 '13 at 09:44
  • @Hong-seokKwon The OS has a limit, but this is a hard limit and usually very high ie. the application will fail if it tries to exceed it, not just slow down. run `ulimit -a` to see what the limits are set to. I don't know of any reason a C++ program would use about the same amount of memory unless it runs a JVM to do most of it's work. – Peter Lawrey Jan 18 '13 at 09:47
  • @PeterLawrey hm.. I have to try googling more. I am really thankful your answer! :) – Hong-seok Kwon Jan 18 '13 at 09:56
1

By default, Java has a heap size limit of 64Mb (if memory servers...) You can increase it with command line arguments, but before you do, make sure you need the extra memory and don't have a leak.

Warren R.
  • 497
  • 3
  • 12
  • While it is true that the maximum memory of 32-bit windows defaults to 64 MB, he is already using 8 GB on UNIX so it doesn't apply here. Never the less your solution is appropriate so +1 – Peter Lawrey Jan 18 '13 at 09:23