11

I have written a java program that tests the speed of a couple of multi-threading algorithms on different machines with various numbers of processors.

On some machines, merge sort* fails because it requires a sizable heap space to work on very large arrays. I can easily change the java heap space myself before running the program, but I feel like a more robust and easy approach would be to do this task from within the program itself.

Is there a way to request/achieve more heap space from the virtual machine during the course of a java program?

Note: I do understand that I could execute the program with a script like java -Xmx1g Program; my curiosity on this subject is in part academic.

*My implementation does NOT merge in-line. It requires O(n) extra memory.

vatbub
  • 2,713
  • 18
  • 41
Robz
  • 1,747
  • 5
  • 21
  • 35

3 Answers3

8

As far as I know, there is no way of controlling the heap size at runtime.

It may not be necessary though: you can provide a minimum and maximum heap size with the -Xms and -Xmx switches respectively. (eg -Xms128m -Xmx512m) The jvm will manage the actual heap size within these bounds.

jvdneste
  • 1,677
  • 1
  • 12
  • 14
3

Java was not design to be able to dynamically manage memory, in this case "java heap space", all the opposite, it was designed in order to relieve the programmer from having to worry about that.

In short, I'm afraid to say that there is nothing like a "malloc()"or "setHeapSize(int byes)" in Java.

On Java you're constraint to the amout of memory available to the JVM when your program starts. In terms of memory management this is both a blessing and a curse.

For that kind of dynamic memory allocation you should try to use implement your algorithm using a language like C and/or C++ instead.

Jose Diaz
  • 5,353
  • 1
  • 31
  • 29
  • Erm, the Sun JVM for windows does resize the heap (i.e. requests or returns memory from/to the operating system) within the limits given by -Xms and -Xmx. You can see this for instance in visualvm. – meriton Aug 04 '10 at 19:33
  • Well the keyword *new* in java is practically equivalent to malloc() in c as it is in c++. But thanks; this clears things up. – Robz Aug 04 '10 at 21:34
  • 1
    you are mixing concepts. The fact that somebody might want to dynamically change the maximum available heap size has nothing to do with the fact that the VM is designed to free the programmer to do memory management. I, as a sysadmin might have misjudged the heap space requirement of my app, and I want to increase it without restarting the application. It's technically feasible to resize the heap space, since the JVM itself does it (from min to max). – mkm Dec 17 '10 at 19:35
2

The maximum is not the size of memory used, this is dynamic based on usage.

The maximum heap size should be the point at which you would rather the program fail, than use more memory. It makes little sense to change this dynamically, even academicly.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130