0

I'm working on a Java (Hibernate+Spring+JavaFX) application. To successfully run this application, I have to set VM : "-Xms512m " otherwise it is failing with below mentioned error.

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [*path/*]: 
    Constructor threw exception; nested exception is java.lang.OutOfMemoryError: Java heap space.

I have observed that the session factory is consuming 250+ MB to initiate. There are some hbm (hbm POJO's) files which are consuming 180 MB

I also tried the Netbeans Profiler session to reduce memory leaks.

Could you please suggest few steps to reduce VM consumption in the start up of application.

What is the best possible approach to be followed to reduce VM consumption?

JavaAppUser
  • 300
  • 1
  • 5
  • 18
  • Configure java to dump heap on OOM. Analyse it , change code to reduce memory footprint. – Jayan Aug 12 '15 at 10:01
  • btw : actually worrying about memory consumption in 2015 is kinda ... pointless. Even the most low-end computers have at least 4 GB of RAM nowadays, in fact 6-8 GB are becoming standard and you can expect AT LEAST 8 GB in gaming rigs, my personal machine even has 16 and it still isnt enough for simultaneous VMs and my private J2EE projects ... as a future developer, you will need A LOT of RAM on a daily basis. – specializt Aug 12 '15 at 10:12
  • @specializt : This is not a gaming application. Every single byte of RAM cost us in hardware. – JavaAppUser Aug 12 '15 at 10:32
  • possible duplicate of [What is an OutOfMemoryError and how do I debug and fix it](http://stackoverflow.com/questions/24510188/what-is-an-outofmemoryerror-and-how-do-i-debug-and-fix-it) – Raedwald Aug 12 '15 at 11:16
  • thats hilariously and utterly false. "Bytes of RAM" cost NOTHING after the hardware has been bought - the mere illusion is kinda ..... "naive". Plus : RAM is extremely cheap, if a few hundred bucks one single time is too much the entire problem attack vector is a wrong one and the whole concept should be re-done ... thats a beginners' mistake. You should re-think your entire concept, everything else might lead into something horrible. – specializt Aug 12 '15 at 12:21
  • @JavaAppUser: If memory is an expensive resource, Java is not your friend. – TMN Aug 12 '15 at 17:05

3 Answers3

0

Not Sure about the correct reason for insufficient heap spcae. To find the correct reason you need to debug it.

Steps to Debug

  • Use JVM arg for memory dump to a given location in the case of Out Of Memory. It will create a hevy file (upto few GBs)

    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=path_to_file

  • Load the dump file using any profiler. Find the culprit. Below are the links to use heap dump

heap dump using visualvm

heap dump tricks

ravi ranjan
  • 5,920
  • 2
  • 19
  • 18
0

Inorder Application to come up, JVM would need minimum java heap space for initialization once it is up due to lack of memory space the application may throw OutOfMemory. There is no option to reduce the memory consumption by VM. JVM will allocate memory if application request for object allocation. It is duty of Garbage Collector to clean up the dead objects.

Following Option could help in identify the leak

1) -verbose:gc This argument will record the GC occupancy behaviour. Once System throws OOM, You could load the logs in Garbage Collector and Memory vzisualizer tool to see the pattern of allocation and tuning recommendations provided by the tool.

2) Collect Heap dump on OOM and Load it in MemoryAnalyzerTool(MAT) and check for Leak suspects.

Mohan Raj
  • 1,104
  • 9
  • 17
0

Solution:

  1. Earlier I was mapping 3 schema in database with 3 session factories. Now mapping with one. Saved 110 MB there.

  2. To further improve the performance, Integerated Ehcache.

Pang
  • 9,564
  • 146
  • 81
  • 122
JavaAppUser
  • 300
  • 1
  • 5
  • 18