12

I have a java desktop application for searching files and it is usually reaching the default heap limit pretty soon. I wont have access to all the systems it will be installed in so I want to increase the JVM heap size in the application itself. Can anybody help me how can I do that programmatically in my application

trincot
  • 317,000
  • 35
  • 244
  • 286
hrl
  • 131
  • 1
  • 1
  • 4
  • 2
    Why don't you choose adding some starting script (bat or sh) with defined starting parameters for jvm (like Xmx) and deploy it together with application itself. – Mirek Pluta Jan 15 '10 at 18:36
  • 3
    What would be the point of setting a limit if any app could just raise it at will? – Mirko N. Jan 15 '10 at 18:36
  • you can't programmatically change the memory constraints in a running application –  Jan 15 '10 at 19:01
  • For reference, here is the RFE for hotspot: bugs.sun.com/bugdatabase/view_bug.do?bug_id=4408373 – Luke Quinane May 21 '12 at 01:56
  • possible duplicate of [Can the JVM max heap size be dynamic?](http://stackoverflow.com/questions/3472593/can-the-jvm-max-heap-size-be-dynamic) – Raedwald Nov 22 '13 at 08:27
  • Although this is earlier than http://stackoverflow.com/questions/3472593/can-the-jvm-max-heap-size-be-dynamic that question has better answers. – Raedwald Nov 22 '13 at 08:28

5 Answers5

7

Setting -Xmx to one gig doesn't mean that the JVM will allocate that much memory upon start-up. The JVM will allocate only -Xms (plus overhead) until more heap space is needed. Do you need to protect your users from thrashing virtual memory or a failed memory allocation from the OS? If not, just set Xmx to a large value. Note that Windows 32bit JVMs will often ignore Xmx settings greater than 1.2Gig, so it's best to not request more than a gig or so to be safe.

ShabbyDoo
  • 1,256
  • 1
  • 11
  • 20
  • 1
    32-bit JVMs won't ignore large maximum heap sizes, instead they won't start. Just set -mx1200m or similar. (-mx works just as well as -Xmx) – Peter Lawrey Jan 15 '10 at 23:51
5

There is no such standard API to do so.

I would suggest you use Java Web Start to invoke your application (can be used for local applications to in the latest Sun Java 6) as it allows you to specify values for this.

You can then have three or four links each pointing to exactly the same files but with "Tiny", "Medium", "Large", "Gigantic" heap sizes.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
2

See this post about increase java heap size :

increase the java heap size permanently?

Maybe also you could clean some part of your code to reduce memory usage.

Best Regards

Community
  • 1
  • 1
madshiva
  • 31
  • 1
2

Take a look at something like launch4j and consider deploying executables where you can control the limit when someone begins the run of your application.

You won't be able to do this programmatically.

Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
2

For desktop applications, I suggest providing a launcher which can then specify your desired memory size. In addition to configuration of your JVM, you can also provide an icon, file associations, etc.

See this SO question for deployment alternatives.

Community
  • 1
  • 1
Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49