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();

2009年7月3日金曜日

[Java]how to use the gzipped http response by using the apache http-client ver.3.x

how to use the gzipped http response by using the apache http-client ver.3.x

  • References
  • Example
    //set url
    String url="http://hc.apache.org/httpclient-3.x/";

    //initialize apache HttpClient object and HttpMethod
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    //set followRedirects function to true if you need.
    httpMethod.setFollowRedirects(true);
    //set Accept-Encoding request header
    httpMethod.setRequestHeader("Accept-Encoding","gzip");

    try{
    //access to the url and get response status
    int status = httpClient.executeMethod(http_method);

    //check response status. if the value of response status is set to 200, get body stream
    if(status == 200){
    //check the response header "Content-Encoding". the value of Content-Encoding header contains the
    //"gzip" value, this means the response stream is gzipped.
    Header contentEncodingHeader=httpMethod.getResponseHeader("Content-Encoding");
    String contentEncoding = contentEncodingHeader!=null ? contentEncodingHeader.getValue() : "";
    String contentEncodingLowerCase=contentEncoding.toLowerCase();
    boolean isGzipped=(contentEncodingLowerCase.indexOf("gzip")>=0);

    //get response stream
    InputStream stream=httpMethod.getResponseBodyAsStream();

    InputStream bodyStream=null;
    ByteArrayOutputStream outStream=null;
    try{
    //if the response stream is gzipped, derived stream is converted into the GZIPInputStream.
    //the response stream is not gzipped, set the stream without conversion
    bodyStream = isGzipped ? new GZIPInputStream(stream) : stream;

    //change the response from InputStream to Byte Array.
    outStream=new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];
    int length;
    while((length=bodyStream.read(buffer))>0){
    outStream.write(buffer,0,length);
    }

    //get the response charset.
    String charset=httpMethod.getResponseCharSet();
    //convert the response byte array to the String object.
    String body=new String(outStream.toByteArray(),charset);

    //Instead of converting the InputStream to Byte array,
    //we can convert the inputstream to String by using the InputStreamReader and BufferedReader directly.
    //But we can't read the responsed InputStream twice.
    //for example.
    //InputStreamReader bodyReader=new InputStreamReader(bodyStream,charset);
    //BufferedReader bodyBufferedReader=new BufferedReader(bodyReader);
    //String line=bodyBufferedReader.readLine();
    //while(line!=null){
    // bodyBuffer.append(line);
    // line=bodyBufferedReader.readLine();
    //}
    //String body=bodyBuffer.toString();
    }catch(Exception e1){
    throw e1;
    }finally{
    //close ByteArrayOutputStream
    if(outStream!=null){
    try{
    outStream.close();
    }catch(Exception ignore){}
    }

    //close InputStream
    if(bodyStream!=null){
    try{
    bodyStream.close();
    }catch(Exception ignore){}
    }
    if(stream!=null){
    try{
    stream.close();
    }catch(Exception ignore){}
    }
    }
    }
    }catch(Exception e0){
    System.out.println("Error, "+e0);
    }finally{
    httpMethod.releaseConnection();
    }