2009年7月24日金曜日

[Grails]check list when java.lang.OutOfMemoryError: Java heap space in grails application

check list when java.lang.OutOfMemoryError: Java heap space in grails application

References
Tools
Check list
  • Check the setup for the second level hibernate cache
    Open grails-app/conf/DataSource.groovy and check the setup of the second-level and query cache. If you don't need to use the hibernate query cache, both cache.use_second_level_cache and cache.use_query_cache are set to false. And then, you need to comment out the cache.provider_class settings if you don't need to use second level cache.(In our case, if the cache provider setting is not commented out, grails caches the domain object into the EhCache internally.)
         hibernate {
    cache.use_second_level_cache=false
    cache.use_query_cache=false
    //cache.provider_class='org.hibernate.cache.EhCacheProvider'
    }
    If you use the Multiple Datasources plugin, please check hibernate cache settings for the other database sessions in the datasources file.

  • Check the history of the java heap usage and Garbage Collector
    Check whether the memory setting for the java virtual machine is enough or not.
    1. run the grails application with followng java options.
      export JAVA_OPTS=-verbose:gc -XX:+PrintClassHistogram -XX:+PrintGCDetails -Xloggc:/tmp/jvm_loggc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp
    2. After appearing the OutOfMemory error, view the /tmp/jvm_loggc.log.
      If you use the GCViewer(http://www.tagtraum.com/gcviewer.html), you can check the history of running the garbage collector and usage of the java heap size easily.

  • Check and analyze the heap dump file.
    After occuring the OutOfMemory Exception, Java VM outputs the HeapDump file under the HeapDumpPath directory, the file name is java_pidXXXXX.hprof. You can check and analyze the usage of the java heap for each java objects and view the description when the OutOfMemory error occures.
    If you use the Memory Analyzer(http://www.eclipse.org/mat/) tool, you can view the information about the Heap Memory Dump file visually and easily.
    Memory Analyzer reports the Leak Suspect Report, you can see the output of the "Leak Suspect Report" analyzed by the Memory Analyzer. Memory analyzer reports the overview of the memory usage and problem suspect.

    Check the memory leak in your program based on the output of the Memory Leak Suspect Report. If you cache the objects pointer in your program, garbage collector never clear and garbage the heap memory.
Problems
  • Memory usage of the org.hibernate.engine.StatefulPersistenceContext" object
    If the Problem Suspect in Leak Suspect Report says that the memory usage of the org.hibernate.engine.StatefulPersistenceContext" object is accumulated, this object depends on the Hibernate's First-level cache, and maybe we can't turn off the hibernate first-level cache functionality in grails environment.(In standalone hibernate env, we can find the StatelessSession functionality, but I don't know how to use this in grails.)
    In case of this, I can find the 2 solutions to avoid increasing the memory usage of the hibernate first level cache.
    1. Set the session's AutoClear flag to true.
      When starting the application, this flag is set to true, cached objects in session are cleared after the end of the transaction. But the cached objects aren't always cleard out.
          Session session=sessionFactory.getCurrentSession();
      if((session instanceof SessionImpl)){
      ((SessionImpl)session).setAutoClear(true);
      }
    2. Call the clear() method of the session
      We often call the clear() method of the session object in our program and release the cached objects in first-level cache manually.
           Session session=sessionFactory.getCurrentSession();
      session.clear();

0 件のコメント: