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

 ̄綄美尐妖づ 提交于 2019-12-05 01:19:37

问题


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?


回答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.




回答2:


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

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




回答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.




回答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




回答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.


来源:https://stackoverflow.com/questions/11824134/caused-by-java-lang-outofmemoryerror-java-heap-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!