5

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?

2
  • It seems ur using more memory than you have Commented Aug 6, 2012 at 7:32
  • In my case my RAM is used 100% because too much applications running. After closing few apps error is gone. Commented May 3, 2020 at 6:18

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

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

3 Comments

I agree with you but I have to note that 100+ threads is not that ridiculous, in fact, if you examine the threads in a running Tomcat/jetty with Spring and Hibernate , it is possible you already have about 50-60 framework-related threads. Think about it, in tomcat pool default configuration of threadPool size is 200, which means 100+ threads is definitely not that ridiculous. With that said, the producer/consumer pattern with worker threads is indeed the proper solution besides making each process require less than 1 hour and require less heap or increasing the maxHeapSize itself.
@baba Sorry, but 100+ threads running 100% CPU for a minimum of 1 hour each is ridiculous and not productive. You want roughly no more threads than CPUs for CPU-bound processing. Those many threads in J2EE containers are practically all asleep and are kept there only to preserve state so when they (occasionally) awake to do work they can immediately respond.
Yes, 100 threads running simultaneously for more than 1-2 seconds is already crazy. I was merely pointing out that 100+ threads are no problem if a large portion of them is sleeping, like in the frameworks I described above.
2

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.

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.