Skip to content
Advertisement

How do I allow the user to easily choose how much memory to allocate in a Java Swing app?

We have a Swing app that processes relatively large amounts of data. For instance we currently process CSV files with millions of rows of data. For the reasons of performance and simplicity we just keep all of the data in memory. However different users will have different amounts of data they need to process as well as different amounts of RAM. When creating the installer, of course we need to specify the heap size. Is there any easy way to allow the user to specify the heap without them needing to hand edit a configuration or .bat file? I’m guessing not all users would be comfortable with this.

I’ve seen one example where an app specified three different shortcuts each with a different amount of memory specified. This could work but I would like an option that is more flexible. The user could then just choose the one that would work best for them.

Advertisement

Answer

I recommend having something similar to IntelliJ. It brings up a configuration dialog when an OutOfMemoryException occurs. This dialog allows the user to configure the heap size and stores it to idea.exe.vmoptions. You would need to sub the contents of the file into the java/javaw launch command or have one Java program bootstrap and launch the real one.

A really cool variant on this is to suggest a memory size based on the data size. The user really has no way of knowing how much memory your program needs so any guidance you can provide in selecting the value will help them immensely.

It is extremely important in support of this solution that you expect and handle OutOfMemoryExceptions in a way that avoids data corruption!

Advertisement