Java heap space issue

1
Hi Team, I am getting an java heap space error while running the application in local.  
asked
5 answers
11

Hi Kajal,

 

This is an error you run into when the JVM Heap tells you “Enough is enough. I can’t fit all of this into my memory.” Which usually means that the application has become unstable and should be restarted before it crashes and that you also have a real problem to solve.

The following things can cause this error:

  • Memory leak
    • Introduced by developer, custom code
    • A bug in Mendix Runtime
    • A bug in a Java library used by custom code of the developer or by the Mendix Runtime
    • A bug in Java Runtime
  • Massive creation of objects (for example, by retrieving 1 trillion entities in a single microflow at once)
  • Configuration issue or sizing issue.

https://docs.mendix.com/refguide/runtime-java-errors/

You can refer to this doc.

 

you can correct this by

Settings→ config.→ Size

Hope it helps!

answered
4

Hi Kajal,

There are too many objects in memory. This can be, for example, due to a memory leak. Or just a big application and too little memory.

 

You can up the standard java heap size in the settings. 

 

After that, check the JVM analyses tool from the JDK. Which after connecting to your running Mendix app, gives you insight in the usage:

answered
0

Hi there, well both the answers above were very insightfull but if those dont work I suggest you simpy delete all the data in your local and start fresh as this error could also be a result of a corrupt data file. 

It worked for me may be it will for you too.

Have  a nice day

answered
0

In general, this “Java Heap Space” error will be caused by insufficient Java heap memory allocation.  The java.lang.OutOfMemoryError: Java heap space error that you are facing actually indicates that your JVM is running out of memory to allocate for objects in the heap. Suppose, if the issue continues even after increasing the heap size then it should be mainly because of the objects that will be retained unnecessarily in memory. This is actually an indication or a suggestion for a possible memory leak. In such scenarios, it is advisable to enable heap dump generation on OOM (XX:+HeapDumpOnOutOfMemoryError) and analyze the dump using tools like HeapHero to identify which objects are consuming the most memory.

 

The next possibility could be when the application attempts to load very large files or collections into memory all at once. Yes, this action will actually overwhelm the available heap. This should be the logic behind any application. If any application tries to load large files all at once, definitely the available heap will overwhelm resulting in the “Java Heap Space” error .

 

If you notice that the garbage collection is running frequently and not reclaiming space then it is a sign that the objects are still strongly referenced. So you can do an analysis for both heap and thread dumps together and get an understanding.

 

In short, with the recommended steps you can first check your heap size configuration and increase the -Xmx value if needed. If the problem remains the same, then definitely you will have to analyze heap dumps to understand memory usage, with which  you can change your code to process large data sets more efficiently.

 

You can check out this blog How to Solve OutOfMemoryError: Java heap space to understand more about this Java Heap Space error.

answered
0
  • Normally this happens when you are loading lot of records using database retrieve
    • For example, lets assume, you are loading 1million records from any table
    • It will be loaded in memory, which will inturn consume memory allocated for JVM
  • In general, garbage collector will clear the space consumed by unused objects
  • But, within a microflow, if you did a DB retrieve loading lot of objects, then it wont be garbage collected anytime sooner than expected
    • And, if you keep loading more objects, it will stress the JVM and it complains that, it cannot create more objects as no there is no heap space
    • java.lang.OutOfMemoryError: Java heap space:
      • Causes: Memory leaks (objects are held onto unnecessarily, preventing garbage collection), processing large datasets, inefficient data structures or algorithms, or an insufficient maximum heap size configured for the application.
      • This is the most common type, occurring when the Java heap, where objects are allocated, runs out of space.
  • Ideally, you should find which scenario is causing this and fix it instead of increasing memory, as increasing the memory is only temporary solution.
    • The increased memory allocation might suite a particular dataset, but not a bigger data set
  • Alternative approach to load large dataset
    • If you are processing large dataset, then batch them
    • OR, you could deviate from the best practice, which is mostly needed in real time situations handling large datasets, to retrieve the object from database using DB retrieve within loop
    • Ofcourse, this would increase the number of DB Connections, but, you should be able to judge which approach is best for your scenario
answered