1

In my Tomcat application I am eventually getting "Out of memory" and "Cannot allocate memory" errors. I suppose it is nothing to do with the heap as it completely fulls up the system memory and I am hardly able to run bash commands.

How this problem is connected to the heap? How can I correctly set up heap size so that the application has enough memory and so it does not consume too much of the system resources?

Strange thing is that "top" command keeps saying that tomcat consumes only 20% of mem and there is still free memory, once the problem happens.

Thanks.

EDIT:

Follow-up: BufferedImage leaks - are there any alternatives?

trincot
  • 317,000
  • 35
  • 244
  • 286
Vojtěch
  • 11,312
  • 31
  • 103
  • 173
  • Are you getting permSize Exception or Out of Memory? – JHS Jun 05 '12 at 08:19
  • 1
    few things: `jmap -histo` is your 1st clue, read docs of jmap. 2nd if you are unable to run bash commands usually your disk are 100% used, check that w/ atop. @everyone, 'cannot allocate memory is not perm gen' – bestsss Jun 05 '12 at 08:25
  • I have 3 GB of memory and I am not using Swap, so it should not be about disks I suppose, however I have 5 GB free. Will try jmap, thanks! – Vojtěch Jun 05 '12 at 08:32
  • Would it help, if I somehow limitted the size of memory java uses? The strange thing is that even when I set Xmx to 1GB, it still consumed all the memory :/ – Vojtěch Jun 05 '12 at 08:35
  • you need a crash course of java memory management/usage. java consumes more than the max. heap size. stats from top are close to useless unless you monitor for native jvm leaks (but you'd not ask anything about on SO anyways then). and yes, all servers should have swapoff -a. disk issues do not come from swap only but faulty hardware too. if you have a memory leak, 1GB/3GB won't help much, just possibly delay the inevitable... – bestsss Jun 05 '12 at 10:51

4 Answers4

2

Problems with running bash scripts may indicate I/O issues, and this might be the case if your JVM is doing Full GCs all the time (which is the case, if your heap is almost-full).

The first thing to do, is to increase the heap with -Xmx. This may solve the problem, or - if you have a memory leak, it won't, and you will eventually get OutOfMemoryError again.

In this case, you need to analyze memory dumps. See my answer in this thread for some instructions.

Also, it might be useful to enable Garbage Collection Logs (using -Xloggc:/path/to/log.file -XX:+PrintGCDetails) and then analyzing them with GCViewer or HPJmeter.

Community
  • 1
  • 1
npe
  • 15,395
  • 1
  • 56
  • 55
  • The thing is, I am not even sure, whether the heap is getting full. I am not getting any permgen, nor heap exceptions. – Vojtěch Jun 05 '12 at 09:20
  • Do you execute external programs from your app? Like using [Process](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html) or [ProcessBuilder](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html)? If so, the OOME is referring to the system memory, not the JVM heap - it probably cannot allocate any more memory for the new process. This would also explain high HDD activity - the system is swapping a lot. – npe Jun 05 '12 at 09:49
  • Well I did not say, there is HDD activity. The SWAP is turned off actually. – Vojtěch Jun 05 '12 at 10:56
  • in linux there is no system oom, when the memory runs low an arbitrary application is killed to release some memory. look up *oom killer* for details – bestsss Jun 05 '12 at 22:02
0

You can set JVM Heap size by specifying the options

-Xmx1024m  //for 1024 MB

Refer this for setting the option forTomcat

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

If you have 4 GB ram then can allocate 3GB to HEAP -

-Xmx3GB

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
0

you can also change the available perm gen size by using the following commands:

-XX:PermSize=128m -XX:MaxPermSize=256m

mooonli
  • 2,355
  • 4
  • 23
  • 32