Mark Eschbach

Software Developer && System Analyst

Increasing NetBean’s available memory

Weather you are encountering a java.lang.OutOfMemoryError or you would like to increase NetBeans CPU efficiency, its easy to increase the memory available to NetBeans. If you consider the variable NB_HOME pointing to the location in your file system NetBeans is installed into, you will find a file at NB_HOME/etc/netbeans.conf. This file controls the launching of the Java Virtual Machine (JVM), including the memory specific options. The variable inside the java.util.Properties-like file you should tune is netbeans_default_options. Each JVM option requires a -J prefix. For example if you were looking to set the minimum heap size of 32MiB (-Xms32m) would be -J-Xms32m.

Many of the options I will discus here are Sun's (now Oracle's) HotSpot VM specific options, however the options are typically ported to other implementations. You should check the documentation for your JVM for any option starting with -X or -XX.

A quick reference of memory options:

-XmxsizeSets the minimum heap size to the specified size
-XmssizeSets the maximum heap size to the specified size
-serverThe server option changes a number of variables optimizing the JVM for long running processes which execute the same code repeatedly, like well, a server process. Normally I would not advocate using this option on a desktop application, however I've typically experienced better subjective results with this option. You probably shouldn't use this on a memory constrained system.
-XX:NewSize=sizeThis option modifies the minimum size of the new generation of the JVM application heap. Making this larger may reduce the number of collections and tenuring objects which should otherwise be collected earlier. This option must be less than the minimum heap size. The best value for this option is highly dependent on all other options.
-XX:MaxNewSize=sizeThis sets the maximum size of the new generation.

Details on tuning the JVM from Sun (now Oracle)

My laptop contains 3GB of RAM, after Windows Vista loads with all the standard trimmings, I'm still only using approximately 30% of the available RAM (~1GB). This leaves a lot of room for NetBeans and the applications I'm developing. To round things out, I give NetBeans a max of 1GB, leaving 1GB for my applications when needed (rare). My options look something like the following (okay they are the following):

netbeans_default_options="-J-server -J-Xverify:none -J-Xss2m -J-Xms256m -J-Xmx1g -J-XX:NewSize=16m -J-XX:MaxNewSize=64m -J-XX:PermSize=32m -J-XX:MaxPermSize=200m"

I removed the -J-Dapple.laf.useScreenMenuBar=true because I'm not running under OSX or an Apple JVM. I also removed the -J-Dsun.java2d.noddraw=true because this was a work around to JVM 1.5 J2D DirectDraw issue, which was apparently fixed in JVM 1.6. If you are having trouble tuning the JVM, I suggest you monitor JVM process with a tool like JConsole, which will let you study the GC behavior.