Netbeans on Mac Tip

As some of you may know I'm a big Mac person. I split my time equally between my iBook (now heading back to the shop, alas) and my WinXP desktop. Being a dyed in the wool plain jane text editor type of programmer I've used jEdit for the past five years and have only recently started using an IDE like Netbeans. I've found Netbeans to a great productivity booster but on my iBook it seems to generate garbage at a mad pace. This is okay, it's just garbage, not a memory leak; but it's a pain when the editor locks up for five seconds to do garbage collection. Fortunately there is a solution.

I recently gave a talk on application optimization at JavaOne Tokyo. The last part of the presentation talks about how choose different garbage collectors to improve perceived performance. In particular the Mark and Sweep garbage collector will operate incrementally, meaning it won't lock your application while done one big GC. Instead it will do lots of tiny GCs which take a bit longer but improve the perceived performance of your app. And after all, what the user perceives is the most important thing. The cool part is you don't need to do any code changes to use this garbage collector. Just a commandline switch.

So, back to Netbeans. Yesterday I'd had it with the long GCs, remembered the commandline switch from my talk, and decided to do something about it.

Here's how to make Netbeans on a Mac use the Mark and Sweep GC.

  1. Fire up a text editor (maybe even vi from the command line) that can open application folders. On the Mac applications are really folders with a special structure that Finder makes look like single application files, so be sure to use a text editor which will show you the real folders and open them. I used JEdit.
  2. Open the folder, navigate to the Contents/MacOS/ directory, and open the netbeans file. This is the script that actually starts netbeans. I'd back file up before editing just in case you break something.
  3. Find section of the script that actually calls the Netbeans executable when running under Darwin (the unix name for MacOSX). In my copy of Netbeans (a recent beta of 5.0) this is line 74 of the script.
  4. Add this argument to the netbeans commandline (line 83 in my copy).
    -J-XX:+UseConcMarkSweepGC \

    Note the space and the trailing '\'. This is because the 10 lines or so right there are actually part of a single commandline and the '\'s make it all be one line.

  5. Save the file then launch Netbeans from Finder. It should come up as normal.
  6. Use Netbeans for a while and watch the memory. It should almost never get up to the max memory limit. Instead it will GC periodically and keep memory usage down to what's actually being used. And you should never experience a GC pause.
  7. Get back to coding. There's apps to be written!

For my Mac brethern out there I hope you find this useful. For those of you not using Netbeans 5 I urge you to try it out. It doesn't suck! (and coming from a lifetime text editor guy that's saying a lot :)

- Josh

Talk to me about it on Twitter

Posted November 23rd, 2005