6

I am trying to run a java program through MATLAB. When my input file is a small size, it works fine. But when I increase the file size, and in turn increase my heap size I get this error message:

Error occurred during initialization of VM

Could not reserve enough space for object heap

Could not create the Java virtual machine.

When I run my java command with -Xmx1500m it works fine, but when I increase it to -Xmx2000m it gives the error message. The computer I'm on has 12 GB of memory, so I don't see why increasing my heap size to 2000 should be a problem. I don't have any memory clogging programs running.

trincot
  • 317,000
  • 35
  • 244
  • 286
thepro22
  • 169
  • 1
  • 4
  • 10

1 Answers1

5

It appears you are using a 32-bit JVM. Java requires a single block of continuous memory for its heap and depending on the OS this is limited to 1.2 to 1.6 GB depending on your OS regardless of the amount of memory you have.

If you use a 64-bit JVM you don't have this problem.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I think I am using 64 bit java. If I type 'java -D64 -version' into command prompt, I get this: "java version "1.6.0_22" / "Java(TM) SE Runtime Environment (build 1.6.0_22-b04)" / "Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)" – thepro22 Aug 29 '11 at 19:38
  • The `-D64` sets a property called `64` to value `null`. Do you mean `-d64` which only works on Solaris AFAIK. It must be a restriction due to the way MATLAB forks the Java process. I assume you can create a JVM of this size form the command line? – Peter Lawrey Aug 29 '11 at 19:46
  • No, if I put in the same command into Command Prompt, it gives me the same message. – thepro22 Aug 29 '11 at 19:57
  • Can you tell me how much free memory you have? Which OS do you have? You many have plenty of memory free but if its fragmented you may not get the memory you want. (A reboot can help) – Peter Lawrey Aug 29 '11 at 20:40
  • 1
    I am running 64-bit Windows 7 Enterprise. Task Manager => Performance says that I have total 12285 MB Physical Memory, of which 7082 MB is Free. I've tried rebooting, but it did not solve the problem. Thanks for your help. – thepro22 Aug 29 '11 at 20:54
  • 1
    It really does sound like you're getting a 32-bit JVM like Peter says. I can repro on my x64 XP running the demo/jfc/fileChooserDemo in the JDK distro. How exactly are you invoking your Java program from within Matlab? If it's with `system`, what's the exact command line? Matlab can change the environment, including the path, or pulling in WoW64 if you're running 32-bit Matlab on Windows x64, which will change which programs you pull in when shelling out. – Andrew Janke Aug 30 '11 at 15:22
  • 1
    Peter - fragmentation and free system memory probably don't matter in this case. The JVM heap only needs to be contiguous within the JVM process's virtual memory address space, and the OS can map pages to physical memory or swap space as and where needed. E.g. on my Win x64 machine with 12 GB of RAM, I can do a `java -Xmx40G -jar FileChooserDemo.jar` with a 64-bit JVM and it works, showing up with 40 GB Virtual Size in procexp, even though that's bigger than my total physical memory, much less available RAM. Same if I have another process that's already filled 7 GB. (Heck, `-Xmx1T` works.) – Andrew Janke Aug 30 '11 at 15:29