Digital Media Web Blogs > Web

A heap-estimating utility for better desktop Java on Windows


Objectively, Java is now looking pretty good for use in desktop applications: the speed for tasks that don't involve much object creation is excellent, Swing is stable though in need of scintillation with some fresh JComponents, SWT is available for people who want something else, Linux's new threads may improve responsiveness there, JRE 1.4.2 is available pretty much everywhere, and we have a zillion open source libraries—my current favourite is SwiXML.

But buzzing amongst our sunbeams are a couple of flies: too-slow loading times, the pathological space-behaviour of some large text classes, and—the topic of this blog—memory allocation geared to suit servers or applets but rather constipated for desktop applications.

Sun's JRE for Windows has a single command-line parameter for setting the maximum heap size of Java applications. Make it too small and you risk perpetual garbage collection, and out-of-memory errors if you open a large file. Make it too large and you risk bumping other applications out to the swapspace (frustrating with slow disks) and, worse,if your memory setting exceeds the physical RAM, you risk the disk thrashing that can occur from the unfortunate interaction of garbage collection and virtual memory.

I don't know what the best answer is. For desktop applications we want to use as much RAM as the user has available, but still co-exist with other applications. But here is one approach that may be some use for people in the same boat.

Javamem

Javamem is an MVC++ program, a little command-line utility for Windows for estimating the optimal maximum heap size for a Java application. You use the result in the -Xmx parameter for the JVM. Javamem came out of frustration with trying to tailor memory setting with some common Windows installer programs. (Click here for the source code javamem.zip.)

Basically, Javamem allocates the amount of heapspace you specify, range-checked by a minimum and maximum threshold, but if there is more available memory, not used by any other program, it will allocate that too. So if your user has a large amount of RAM, your application can make more use of it.

There are a couple of whistles: Javamem reserves some memory for the operating system (and your application's non-heap use) and it can detect if you are running XP and reserve even more; the intent being to allow you to go up to maximum without taking you further. If there is less memory available than the preferred size, it allocates as much as is available.

The command line is

javamem min preferred max? reserved?

where min is the minimum -Xmx in megabytes, preferred is the heap size you want, max is the opportunistic maximum, and reserved allows your own bias for how much memory the OS and so on requires.

Of course, virtual memory behaviour is very complex: this is just a quick hack for a particular problem I had. My feel is that the problem is not so important under Linux and Mac OS X, I presume because of their different virtual memory algorithms.

Anyway, enjoy! Here are a couple of example settings.

  • A medium program with small documents which you always want to have a 64 meg heap if it is available, but 96 if possible: javamem 32 64 96
  • A large program which you want to allow up to a gig of heap memory, if available, but at least 256 megabytes: javamem 64 256 1024.

Note: this is not code I will be supporting or maintaining. Please feel free to use or improve it.

Any better ideas?

Categories





AddThis Social Bookmark Button




Read More Entries by Rick Jelliffe.

Topics of Interest

Related Books

Recommended for You

Archives


 
 


Or, visit our complete archive.  

Stay Connected