Archive for March, 2006

Improve the performance of Netbeans

Written by coregps on Tuesday, March 21st, 2006 in Java.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I’m running Netbeans 5.0 with JDK 1.5.0_06 on Windows XP. It is getting more and more slow, especially during the garbage collection. I found some tunning tips on the NetBeans site.

1. Disable the modules that you don’t use. With NetBeans 5.x use Tools | Module Manager to display a dialog allowing you to configure modules.

2. Try switching to a different garbage collection algorithm and tunning JVM switches. This can be done by setting the following options in the configuration file $NB_HOME/etc/netbeans.conf.

  • -J-Xverify:none - this switch turns off Java bytecode verification, making classloading faster, and eliminating the need for classes to be loaded during startup solely for the purposes of verification. This switch improves startup time, and there is no reason not to use it.
  • -J-Xms32m - this setting tells the Java virtual machine to set its initial heap size to 32 megabytes. By telling the JVM how much memory it should initially allocate for the heap, we save it growing the heap as NetBeans consumes more memory. This switch improves startup time. It is used by default in NetBeans, so you do not need to specify it.
  • -J-Xmx256m - this settings tells the Java virtual machine the maximum amount of memory it should use for the heap. Placing a hard upper limit on this number means that the Java process cannot consume more memory than physical RAM available. This limit can be raised on systems with more memory. Current default value is 128MB. Note: Do not set this value to near or greater than the amount of physical RAM in your system or it will cause severe swapping during runtime. On machines with limited memory (less than 384MB), it is recommended that you set the initial heap size lower than the default. Open the configuration file etc/netbeans.conf in a text editor and modify the options in netbeans_default_options setting. Decrease the option -J-Xmx128m to -J-Xmx96m and the option -J-XX:MaxPermSize=96m to -J-XX:MaxPermSize=64m.
  • -J-XX:+UseConcMarkSweepGC or -J-XX:+UseParNewGC - try these switches if you are having problems with intrusive garbage collection pauses. This switch causes the JVM to use different algorithms for major garbage collection events (also for minor collections, if run on a multiprocessor workstation), ones which do not “stop the world” for the entire garbage collection process. You should also add the line -J-XX:+CMSClassUnloadingEnabled and -J-XX:+CMSPermGenSweepingEnabled to your netbeans.conf file so that class unloading is enabled (it isn’t by default when using this collector).
  • -XX:+UseAdaptiveSizePolicy - this switch may help improve garbage collector throughput and memory footprint. It is part of garbage collector ergonomics implemented in JDK5.0.
  • -J-XX:+UseParallelGC - some tests have shown that, at least on systems fairly well equipped with memory, the durations of minor garbage collections is halved when using this collection algorithm, on uniprocessor systems. Note that this is paradoxical - this collector is designed to work best on multiprocessor systems with gigabyte heaps. No data is available on its effect on major garbage collections. Note: this collector is mutually exclusive with -J-XX:+UseConcMarkSweepGC.
  • -J-XX:CompileThreshold=100 - this switch will make startup time slower, by HotSpot to compile many more methods down to native code sooner than it otherwise would. The reported result is snappier performance once the IDE is running, since more of the UI code will be compiled rather than interpreted. This value represents the number of times a method must be called before it will be compiled.

After modifying some options above, my Netbeans IDE works faster than before :) The following is my netbeans.conf file.

# ${HOME} will be replaced by JVM user.home system property
netbeans_default_userdir="${HOME}/.netbeans/5.0"
# options used by netbeans launcher by default, can be overridden by explicit
# command line switches
#netbeans_default_options="-J-Xms32m -J-Xmx128m -J-XX:PermSize=32m -J-XX:MaxPermSize=96m -J-Xverify:none -J-Dapple.laf.useScreenMenuBar=true"
# --- tips for more advanced options you could use ---
# for JDKs where it is supported (for example Sun's JDK 5.0 and newer),
# you can use the following set of options which enable Concurrent Mark and
# Sweep garbage collection algorithm and improve UI responsiveness
# (see FAQ item http://www.netbeans.org/kb/faqs/performance.html#FaqGCPauses)
netbeans_default_options="-J-Xms32m -J-Xmx128m -J-XX:PermSize=32m -J-XX:MaxPermSize=96m -J-Xverify:none -J-Dapple.laf.useScreenMenuBar=true -J-XX:+UseConcMarkSweepGC -J-XX:+CMSClassUnloadingEnabled -J-XX:+CMSPermGenSweepingEnabled -XX:+UseAdaptiveSizePolicy -J-XX:CompileThreshold=100"
# ---
# default location of J2SE JDK, can be overridden by using --jdkhome <DIR> switch
netbeans_jdkhome="C:Program FilesJavajdk1.5.0_06"
# clusters' paths separated by path.separator (semicolon on Windows, colon on Unices)
#netbeans_extraclusters="/absolute/path/to/cluster1:/absolute/path/to/cluster2"

For more information about Netbeans performance tuning, visit:
http://performance.netbeans.org/howto/jvmswitches/index.html
http://www.netbeans.org/kb/faqs/performance.html#FaqGCPauses

Precompile JSP files in Netbeans

Written by coregps on Wednesday, March 15th, 2006 in Java.

Quite often we may need to precompile jsp files to get better performance without waiting the first time hit, or protect our jsp code by only shipping the corresponding CLASS files.

Some JSP containers support the capability of precompiling a JSP page by accessing the jsp page with a query string of ?jsp_precompile:

http://hostname.com/myjsppage.jsp?jsp_precompile

Netbeans IDE also implements the jsp compilation. Just right click a single jsp file and choose compile file or select the “Test compile all JSP files during builds” checkbox (in the Compiling panel of the Project Properties dialog box).

However, we must define explicit servlet mappings for each servlet in our Web application manually. It is a hard work. Fortunately, the Netbeans-generated build file provides several targets which are by default empty and can be used for execution of customized tasks. They are:

    -pre-init:                 called before initialization of project properties
    -post-init:                called after initialization of project properties
    -pre-compile:              called before javac compilation
    -post-compile:             called after javac compilation
    -pre-compile-single:       called before javac compilation of single file
    -post-compile-single:      called after javac compilation of single file
    -pre-compile-test:         called before javac compilation of JUnit tests
    -post-compile-test:        called after javac compilation of JUnit tests
    -pre-compile-test-single:  called before javac compilation of single JUnit test
    -post-compile-test-single: called after javac compilation of single JUunit test
    -pre-dist:                 called before jar building
    -post-dist:                called after jar building
    -post-clean:               called after cleaning build products

I’ve created one build script used to precompile jsp files by using Tomcat JspC task. It is something like this:

<?xml version=“1.0″ encoding=“UTF-8″?>
<project name=“MyAPP-JSP” basedir=“.”>
    <description>Precompile JSP files</description>

    <target name=“jsp2java”>
        <taskdef classname=“org.apache.jasper.JspC” name=“jsp2java”>
            <classpath id=“jsp2java.classpath”>
                <fileset dir=“${tomcat.home}/bin”>
                    <include name=“*.jar”/>
                </fileset>
                <fileset dir=“${tomcat.home}/server/lib”>
                    <include name=“*.jar”/>
                </fileset>
                <fileset dir=“${tomcat.home}/common/lib”>
                    <include name=“*.jar”/>
                </fileset>
            </classpath>
        </taskdef>
        <jsp2java classpath=“jsp2java.classpath” javaEncoding=“UTF-8″
validateXml=“false” uriroot=“${basedir}/${build.web.dir.real}”
webXmlFragment=“${basedir}/${build.web.dir.real}/WEB-INF/webJSP.xml”
outputDir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC/src”/>
    </target>

    <target name=“java2class”>
        <mkdir dir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC/classes”/>
        <javac srcdir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC/src”
destdir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC/classes”
encoding=“UTF-8″ optimize=“off” debug=“on” failonerror=“false” excludes=“**/*.smap”>
            <classpath id=“java2class.classpath”>
                <pathelement location=“${basedir}/${build.web.dir.real}/WEB-INF/classes”/>
                <fileset dir=“${basedir}/${build.web.dir.real}/WEB-INF/lib”>
                    <include name=“*.jar”/>
                </fileset>
                <pathelement location=“${tomcat.home}/common/classes”/>
                <fileset dir=“${tomcat.home}/common/lib”>
                    <include name=“*.jar”/>
                </fileset>
                <pathelement location=“${tomcat.home}/shared/classes”/>
                <fileset dir=“${tomcat.home}/shared/lib”>
                    <include name=“*.jar”/>
                </fileset>
                <fileset dir=“${tomcat.home}/bin”>
                    <include name=“*.jar”/>
                </fileset>
            </classpath>
            <include name=“**”/>
            <exclude name=“tags/**” />
        </javac>
    </target>

    <target name=“class2jar”>
        <jar basedir=“${basedir}/${build.web.dir.real}/WEB-INF/classes”
jarfile=“${basedir}/${build.web.dir.real}/WEB-INF/lib/${project.name}Bean.jar”/>
        <jar basedir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC/classes”
jarfile=“${basedir}/${build.web.dir.real}/WEB-INF/lib/${project.name}JSP.jar”/>
    </target>

    <target name=“cleanup”>
        <delete dir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC/classes”/>
        <delete dir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC/src”/>
        <delete dir=“${basedir}/${build.web.dir.real}/WEB-INF/JspC”/>
    </target>

    <target name=“precompile-jsp”  depends=“jsp2java, java2class, class2jar, cleanup”/>
</project>

To be able to use the precompile jsp target, just put the build-jsp.xml file into the project folder (same as build.xml) and follow the steps below:

1. Add the following properties in the nbproject/project.properties file:

project.name=MyAppName
tomcat.home=D:/Tomcat

2. Import build-jsp.xml into the project’s build.xml file and override an appropriate target mentioned above:

<import file=”build-jsp.xml”/>
<target name=”-post-dist”  depends=”precompile-jsp”/>

3. Right-click the project node and choose Build Project to build the project. The precompile jsp target will compile all the jsp files and create a jar file named JSP.jar (for example, OAJSP.jar) in the build/web/WEB-INF/lib directory, it also create a webJSP.xml file in the build/web/WEB-INF directory, which contains the servlet mapping. Just copy these mappings to the web.xml file.

What should be noticed is that the included jsp fragment should be named with the .jspf extension. After building the project, you can delete all the jsp files and deploy the JSP.jar file only.

That’s all. Any good ideas or improvement suggestions will be appreciated!



Site Navigation