0

I used Tomcat 7 for deploy the applications. I execute below code for get the OutOfMemory error.

Long maxMemorySize = Runtime.getRuntime().maxMemory();
model.addAttribute("memorySize",maxMemorySize);
int[] matrixArr = new int[(int) (maxMemorySize + 1)];
for(int i = 0; i < matrixArr.length; ++i)
    matrixArr[i] = i+1;

After error is appear, I create a setenv.sh file in tomcat/bin and add below code in it.

export CATALINA_OPTS="-Xms512M -Xmx1024M"

Then restart the Tomcat server and re-execute the programme. But again OutOfMemory error appear. I change the parameters of CATALINA_OPTS, but it doesn't work. And also add below code in setenv.sh file, but it also doesn't work.

export JAVA_OPTS="-Xms512M -Xmx1024M"

Please help me to solve this issue.

abaghel
  • 14,783
  • 2
  • 50
  • 66
Lakshitha Gihan
  • 303
  • 1
  • 19
  • Possible duplicate of [How to increase Java heap space for a tomcat app](http://stackoverflow.com/questions/2718786/how-to-increase-java-heap-space-for-a-tomcat-app) – rkosegi Feb 05 '17 at 13:16
  • I think you're using the wrong parameter to be designated as the **max allocatable** heap size. see http://stackoverflow.com/questions/3571203/what-are-runtime-getruntime-totalmemory-and-freememory – Jerry Chin Feb 05 '17 at 13:16

3 Answers3

0

Runtime.getRuntime().maxMemory() return the maximum amount of memory that JVM will attempt to use.
It return 1024M when -Xmx1024M. The array matrixArr need to allocate 1024M*4 of memory. That's why you still got java.lang.OutOfMemory exception after increasing JVM heap size.

Beck Yang
  • 3,004
  • 2
  • 21
  • 26
0

You can't allocate all the memory in a Java VM. It needs room for the program code, class definitions and static fields in many classes that get loaded.

Furthermore, you're allocating an array of ints (which are 4 bytes per piece) while your maxMemory returns a value in bytes.

So if maxMemory returns 100, then you try to allocate an array of 100 ints, which at the very minimum requires 400 bytes of space.

What you can do is use IntBuffer.allocateDirect to allocate space outside the Java heap.

john16384
  • 7,800
  • 2
  • 30
  • 44
0

if your tomcat server is running on a linux machine and installed from repository the do this .. edit /etc/default/tomcat7. in the line JAVA_OPTS replace the value for -Xmx with (2048m). leave everything else the same. restart tomcat

D.jackson
  • 11
  • 2