5

Possible Duplicate:
Is it possible to dynamically change maximum java heap size?

I know there is an XMX option for the Java launcher, but is there some kind of preprocessing directive that does this instead (so that all computers that run my code will have their heap increased).

As it is right now, java.exe reaches ~280MB max only (before it crashes) - is this normal?

Community
  • 1
  • 1
  • Related: http://stackoverflow.com/questions/193483/how-to-specify-jvm-argument-for-maven-built-executable-jar – Captain Giraffe Dec 08 '12 at 23:40
  • 3
    *"Is there a way to increase Java heap space in the code itself?"* No - once the code is loaded, the available memory has already been set (in stone). One solution is to check the memory in the current VM, and if not enough 1) launch a new `Process` with more memory 2) exit. – Andrew Thompson Dec 08 '12 at 23:41
  • @Andrew Thompson: "launch a new Process with more memory" can this be done in the code? – Wuschelbeutel Kartoffelhuhn Dec 08 '12 at 23:46

3 Answers3

4

The answer is "no".

The memory size is passed to the JVM on start up. The code running inside the JVM can not change it.

IF you think about it, it makes sense, because the JVM is a program not written in java that can execute java byte code. IT wouldn't be safe to allow the java code to control its container.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • If I want to go the XMX route, which java executable needs to have this startup property added?...i just did a search for java.exe and i get about two dozen results.. – Wuschelbeutel Kartoffelhuhn Dec 08 '12 at 23:47
  • or do i have to enter this in the cmd.exe? – Wuschelbeutel Kartoffelhuhn Dec 08 '12 at 23:48
  • It has to be a command line option. It's not something you can set globally. It is usually a sensible value which suggests it either a) your machine doesn't have much memory or more likely b) the problem isn't what you think as you haven't been very specific about what the "crash" is. IMHO a crash means you have an unhandled signal and you will have a dump file logged, not an error or exception. – Peter Lawrey Dec 09 '12 at 00:07
1

It wouldn't be a maximum if you could increase it.

The reason it cannot be increased is that it allocated on start up. It can only get set before the JVM starts.

What you can do is relauch the application with a higher maximum. e.g. on start you check the maximum and if it not high enough you run the same program except with a higher maximum.

Note: if you use off heap memory, it can be GB or even TBs more as it is not part of the maximum.

As it is right now, java.exe reaches ~280MB max only (before it crashes) - is this normal?

Crashes can mean just about anything and the reason could be just about anything. I suggest you investigate in more detail exactly why it is not working and I suspect you will find that the maximum memory is not the issue. (Unless you have a small 1 GB machine in which case the default maximum memory would be about 250 MB)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Where am I supposed to enter the XMX attribute? I can't find the JVM command line. You're probably right that something other than the maximum heap size is at fault here. – Wuschelbeutel Kartoffelhuhn Dec 09 '12 at 00:03
  • It can only be set on the command line. How are you running the Java program? How much main memory do you have? – Peter Lawrey Dec 09 '12 at 00:04
  • 16GB, when i enter it into the cmd.exe, I get java is not recognized as an internal or external command – Wuschelbeutel Kartoffelhuhn Dec 09 '12 at 00:06
  • also, im running it in jgrasp – Wuschelbeutel Kartoffelhuhn Dec 09 '12 at 00:07
  • If you have 16 GB and you have the server jvm (any 64-bit version or unix) then your maximum memory will be 1/4 or 4 GB. As your program is having problems long before it reaches this point I suspect the maximum memory is not the problem. You need to clarify what you mean by "crash" – Peter Lawrey Dec 09 '12 at 00:09
  • I might have the 32bit jvm installed. edit: indeed: its in my x86 program files folder. how much is the max in that case? – Wuschelbeutel Kartoffelhuhn Dec 09 '12 at 00:11
  • "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.HashMap.createEntry(Unknown Source)" – Wuschelbeutel Kartoffelhuhn Dec 09 '12 at 00:13
  • If you are also running windows then it runs a 32-bit virtual machine which limits your memory to about 1.2 GB The default maximum memory size for the client version is 64 MB, so I don't think you are using that) – Peter Lawrey Dec 09 '12 at 00:14
  • I think you need to investigate exactly what is happening before the application dies because it doesn't make much sense to me. I suggest you watch the process in VisualVM to trace it's memory usage. – Peter Lawrey Dec 09 '12 at 00:16
  • Ok i downloaded it and it says the heap size is 200MB and after a while it increases to 268MB (after my HashMap grows), and after that it doesnt increase the heap any more :( Here is a screenshot: http://i.imgur.com/ICVQT.jpg – Wuschelbeutel Kartoffelhuhn Dec 09 '12 at 00:33
  • That confirms what you are saying. My only guess is that you are running the 32-bit server version which is taking a default maximum heap size of ~268 MB. Are you sure your latest version is 64-bit? Another possibility is that you are launching your application from an environment which is setting it incorrectly because the default on your system should be 4 GB (for 64-bit) – Peter Lawrey Dec 09 '12 at 15:23
1

"launch a new Process with more memory" can this be done in the code?

Yes. See this simplistic example.

import java.awt.EventQueue;
import javax.swing.JOptionPane;
import java.io.File;

class BigMemory {

    public static void main(String[] args) throws Exception {
        if (args.length==0) {
            ProcessBuilder pb = new ProcessBuilder(
                "java",
                "-Xmx1024m",
                "BigMemory",
                "anArgument"
                );
            pb.directory(new File("."));
            Process process = pb.start();
            process.waitFor();
            System.out.println("Exit value: " + process.exitValue());
        } else {
            Runnable r = new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(
                        null,
                        "Max Memory: " +
                        Runtime.getRuntime().maxMemory() +
                        " bytes.");
                }
            };
            EventQueue.invokeLater(r);
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433