Friday, April 30, 2010

java.lang.OutOfMemoryError: PermGen space

First, http://blogs.sun.com/fkieviet/entry/classloader_leaks_the_dreaded_java
Then, http://blogs.sun.com/fkieviet/entry/how_to_fix_the_dreaded

http://community.eapps.com/showthread.php?153-Eliminate-PermGen-out-of-memory-errors-for-good! : It says use -XX:+CMSPermGenSweepingEnabled, -XX:+CMSClassUnloadingEnabled. But what if the classes are still being referred as discussed in the first two SUN blog posts.


http://java.dzone.com/articles/java-performance-tuning?page=0,1 : A generic article


http://stackoverflow.com/questions/88235/how-to-deal-with-java-lang-outofmemoryerror-permgen-space-error

http://forum.springsource.org/showthread.php?t=21383&highlight=cglib+cache : heated up arguments

http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/memleaks.html#gbyuu: Permgen issue with interned Strings

http://rajakannappan.blogspot.com/2010/01/outofmemoryerror-on-heapspace-permgen.html : Suggests using XX:+TraceClassLoading and -XX:+TraceClassUnloading , XX:+PrintGCDetails  , JVisualVM


http://www.samaxes.com/2007/10/classloader-leaks-and-permgen-space/ : Basically analyses the first two SUN blog posts and directs to following  useful posts.
1. http://blogs.sun.com/sundararajan/entry/jhat_s_javascript_interface : contains the oql function for histograms
3. http://mediacast.sun.com/users/Frank.Kieviet/media/JavaOne07-BOF9982-PermGen.pdf : awesome


JACKPOT- what worked for me
http://little418.com/2009/08/grails-configslurper-and-its-permgen-apatite.html : check the email chain too.
Basically I was parsing a config (thus creating new() ConfigSlurper ) inside a method of my service that's called multiple fixed no. of times (say X ) from my cron job. At each cron job run some fixed number of dynamically created classes were loaded X times(each time with some different ID that's used in the class name). When cron job finishes the objects are freed but the classes remian loaded.
Solution: Moved config parsing to the constructor of my service and made ConfigSlurper object as a class parameter(earlier it was a local method variable)

Wednesday, April 28, 2010

Fighting java.lang.NoClassDefFoundError: org/cyberneko/html/parsers/SAXParser on Grails-1.2.1

This was a problem that occured while using HttpBuilder for making HTTP requests.

1. When I say httpBuilder.request(GET, HTML){}, it throws
java.lang.NoClassDefFoundError: org/cyberneko/html/parsers/SAXParser

        at groovyx.net.http.ParserRegistry.parseHTML(ParserRegistry.java:157)

ParserRegistry source:

public GPathResult parseHTML( HttpResponse resp ) throws IOException, SAXException {
return new XmlSlurper( new org.cyberneko.html.parsers.SAXParser() )
.parse( parseText( resp ) );
}

My BuildConfig contained:

build ('org.codehaus.groovy:http-builder:0.5.0-SNAPSHOT'){
excludes "junit", "xml-apis", "xercesImpl"
}



Diagnosis: The class "org/cyberneko/html/parsers/SAXParser" belongs to nekohtml.jar. HttpBuilder has a dependency on nekohtml.jar. This dependency gets resolved in the Grails dependency resolution step. However I still get the above exception. Stupid class loading issue.


2. So added to my BuildConfig:
 runtime ('net.sourceforge.nekohtml:nekohtml:1.9.9'){
}
This lead to:

Error executing script RunApp: loader constraint violation: when resolving overridden method "org.apache.xerces.jaxp.SAXParserImpl.getParser()Lorg/xml/sax/Parser;" the class loader (instance of org/codehaus/groovy/grails/cli/support/GrailsRootLoader) of the current class, org/apache/xerces/jaxp/SAXParserImpl, and its superclass loader (instance of ), have different Class objects for the type org/xml/sax/Parser used in the signature

        at org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParser(Unknown Sour
ce)

Diagnosis: It seems that nekohtml has a dependency on xercesImpl that in turn has a dependency on xml-apis.jar that contains the class org/xml/sax/Parser. However this class had already been loaded by some parent class loader from somewhere else.

3. So I modified my BuildConfig:
runtime ('xerces:xercesImpl:2.8.1'){
excludes "xml-apis"
}
 runtime ('net.sourceforge.nekohtml:nekohtml:1.9.9'){
excludes "xercesImpl"
}
SUCCESS :D


Thursday, April 22, 2010

Deploying grails war to tomcat- servlet-api conflict

http://jira.codehaus.org/browse/GRAILS-2053

http://jira.codehaus.org/browse/GRAILS-5174

Wednesday, April 21, 2010

The Grails framework Reference Documentation

http://www.grails.org/doc/latest/guide/single.html#3.1.3 GORM

Grails save() dynamic method requires exception throwing to be enabled explicitly

http://www.grails.org/doc/latest/ref/Domain%20Classes/save.html:
Check out the failOnError option.


if( !b.save() ) {
   b.errors.each {
        println it
   }
}
OR
try{
b.save(failOnError :true)
}catch(ValidationException e){
//handle exception
}
However I could't find any ValidationException class in the package grails.validation of grails-1.2.1.
So went for the b.errors.each approach.