Caused by: java.lang.OutOfMemoryError: Java heap space

29,740

Solution 1

This error means that your program needs more memory than your JVM allowed it to use!

Therefore you pretty much have two options:

  1. Increase the default memory your program is allowed to use using the -Xmx option (for instance for 1024 MB: -Xmx1024m)
  2. Modify your program so that it needs less memory, using less big data structures and getting rid of objects that are not any more used at some point in your program

As Peter Lawrey pointed out, using a profiler to see what your program is doing in such situations is generally a good idea.

Solution 2

Use a producer/consumer pattern with a limited number of worker threads.

100+ threads is ridiculous - no wonder your application is exploding.

Solution 3

You haven't provided any information which indicates the problem is very different to all the answers given in StackOverflow regarding this error either;

  • You are using too much memory and you need to use a memory profiler to reduce it.
  • You are setting the maximum memory too low and you need to increase the maximum memory with -mx or -Xmx

I suspect that since you want 1000 users to run processes which take an hour each you may need more resources than you have. e.g. 1000 cores perhaps? I suggest you look at how much hardware you need based on the CPU, memory, disk IO and network IO that is required to run the users at an acceptible level e.g. 20 users and multiple that by 50.

Solution 4

You can try increasing the JVM heap space when you launch your application. You can try setting it to 2GB with -Xmx2g. If you're running 32-bit Java I think 2GB is as high as you can go, but if you have a 64-bit JVM you should be able to go higher.

Edit:

Example: java -Xmx2g MyApp

Solution 5

I will check 2 areas when there is out of memory error

  1. Is the allocated memory to the JVM sufficient, if not increase it using -Xmx
  2. Check the code thoroughly, more than 90% of the time I found the error with some loop going recursive under some border condition.
Share:
29,740
Ganesa Vijayakumar
Author by

Ganesa Vijayakumar

LinkedIn | Github | Github/gv-classroom | Github/gv-lab | Medium | StackShare | Pocket | Hackerrank | JSfiddle | Stackblitz | Sourcerer | CodeMentor | Udemy | CodePen | Castbox | gitconnected | ProductHunt | Stepik | hyperskill

Updated on June 19, 2020

Comments

  • Ganesa Vijayakumar
    Ganesa Vijayakumar almost 4 years

    MY GOAL:

    I want run my application for 1000 users.

    NOW

    I am trying to run for 100 user. During application run, I'd like to do some process for each user that will take a minimum of one hour per user, so I'm using one thread per user.

    ERROR

    Caused by: java.lang.OutOfMemoryError: Java heap space

    I've tried to figure out what this means, but I'm not really sure how to resolve it.

    Can anybody help me?