Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Could not reserve enough space for object heap [RESOLVED]

Discussion in 'Editor & General Support' started by Elecman, May 5, 2011.

  1. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,369
    I kept on getting this error when building for Android (shadow demo):
    Code (csharp):
    1.  
    2. Building DEX Failed!
    3. G:\Unity\JavaPluginSample\Temp/StagingArea> java -Xmx1024M
    4. -Djava.ext.dirs="G:/AndroidSDK/android-sdk_r09-windows\platform-tools/lib/"
    5. -jar "G:/AndroidSDK/android-sdk_r09-windows\platform-tools/lib/dx.jar"
    6. --dex --verbose --output=bin/classes.dex bin/classes.jar plugins
    7. Error occurred during initialization of VM
    8. Could not reserve enough space for object heap
    9. Could not create the Java virtual machine.
    10.  
    The troubleshooting guide shows this solution:
    http://unity3d.com/support/documentation/Manual/TroubleShooting.html
    Well, I tried uninstalling and updating both the JDK and JRE. I still kept on getting the error. I finally fixed it by modifying the Xmx variable of java. I noticed that Unity3d uses a value of 1024Mb and this is the cause of the problem, at least on my PC (4gb, winXP32) because on my laptop (1gb, winXP32) it works fine without modifying anything.

    Anyway, here is how to fix it:
    Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System Variables->New:
    Variable name: _JAVA_OPTIONS
    Variable value: -Xmx512M

    If 512Mb doesn't work, try reducing it from 1024 to lower until it works. Also, when googling around I noticed that some users reported that the order in which the path variables occur makes a difference too. The JRE must be the first one. I am not sure if this is true, but here it is anyway:
    Variable name: Path
    Variable value: ;C:\Program Files\Java\jre6\bin;F:\JDK\bin;
    Change this to your appropriate path.
     
    AnastasiiaIT likes this.
  2. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,369
    I tried unity on a new laptop now. It has Windows 7 and 2gb memory. It has a fresh install (format before) of all the required components yet the same problem exists. I now use unity 3.3 instead of 3.2 but it makes no difference. The fix above solves the problem.

    The interesting thing is that the fix above breaks java on my XP 4gb machine. If I open my browser (any), it crashes if I open a webpage which uses java. It works fine on my windows 7 laptop. Weird...

    Unity needs to address this issue. The excuse that the problem is caused by a bad installation of java is just plain nonsense. At least in my case. How can this be, on two completely different systems, one with a fresh install of everything?
     
    Last edited: Jun 16, 2011
  3. AbgaryanFX

    AbgaryanFX

    Joined:
    Jan 9, 2010
    Posts:
    167
    thanks, thanks a lot !
     
    Ghos7 likes this.
  4. psrussell

    psrussell

    Joined:
    Feb 4, 2012
    Posts:
    8
    I just purchased and imported GyroDroid into a new Unity 3.5.6f4 project on a WinXP SP3 machine. I have the latest JRE6 installed. When I try to build the project for Android, I get the error indicated above. When I try the fix listed above by creating a _JAVA_OPTIONS env var using a value of -Xmx512m the compile appears to work, but then errors when trying to sign the package, even though the correct passwords in the publishing settings.

    arsigner: unable to read password: The handle is invalid
    Enter Passphrase for keystore: Picked up _JAVA_OPTIONS: -Xmx512m

    UnityEngine.Debug:LogError(Object)
    PostProcessAndroidPlayer:SignAndroidPackage(String) (at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/BuildPipeline/PostProcessAndroidPlayer.cs:949)
    PostProcessAndroidPlayer:postProcess(BuildTarget, String, String, String, String, String, String, BuildOptions) (at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/BuildPipeline/PostProcessAndroidPlayer.cs:460)
    UnityEditor.HostView:OnGUI()

    If I use the _JAVA_OPTIONS, do all parameters have to be picked up from the env var?
    Is there another way of setting this java compiler setting in a Unity config file or something?

    Thanks
     
  5. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,369
    Roughly 2 years later, Unity finally fixes this problem.

    Try version 4.1
     
  6. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,551
    Hi. I experienced this in 3.5.7f6 and I can verify that the "_JAVA_OPTIONS: -Xmx512m" trick works. I have JRE 7 installed (no JDK installed).
     
  7. alanic

    alanic

    Guest

    I just had this issue with 4.1.0 and upgrading to 4.1.2 solved it.

    Edit: well, a little later I had it in 4.1.2 again. I guess I'll try the jvm command line option suggested above.
     
    Last edited by a moderator: Apr 29, 2013
  8. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,369
    Bummer. So it is still not fixed.
     
  9. brigosx

    brigosx

    Joined:
    May 21, 2013
    Posts:
    6
    Hi to all, I am new in Unity and I am trying to export a very simple test project to Android. I have done everything mentioned in this thread but I still get the invokation error (it says something about Java language Null exception or something) when Unity tries to communicate with Android SDK. My OS is Win64 but the JRE and JDK is for Win32 like the latest Unity version I have installed in my PC. Any help will be much appreciated. Thanks.
     
  10. Moraleidahgo

    Moraleidahgo

    Joined:
    Mar 3, 2012
    Posts:
    107
    Any ideas on how to do that on a Mac?
     
  11. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    Just had this bug.

    Going to Edit > Preferences > External Tools and choosing the "Android SDK Location" fixes it.

    I already had it set, since I had done 10+ exports to Android before, but it seems like Unity automatically removed the location at some point.
     
  12. Mikael-Madrid

    Mikael-Madrid

    Joined:
    Nov 2, 2012
    Posts:
    26
    [27-06-2013]Just here to add: Using latest unity version 4.1X,was getting the error and the variable trick worked.
     
  13. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,369
    I upgraded to Win 7 64 bit and at first everything worked smoothly. Then for no particular reason this error started to show up again. Interestingly Unity had lost the Android SDK folder as previously mentioned. However, setting the Android SDK folder again didn't work as Unity lost it every time.

    Restarted computer, started Unity, set path, closed Unity, started Unity again and now it works.

    There is something seriously wrong with either Java or Unity.
     
  14. gdg

    gdg

    Joined:
    Jul 8, 2013
    Posts:
    18
    I did the above steps and I still get the error. At the end I get:

    Error: Could not create the Java Virtual Machine.
    Error: A fatal exception has occurred. Program will exit.
    Picked up _JAVA_OPTIONS: -Xmx1024M

    I am using Unity 4.1
     
  15. gdg

    gdg

    Joined:
    Jul 8, 2013
    Posts:
    18
    After that I changed it back to 512 and somehow it works!
     
  16. hnoor0044

    hnoor0044

    Joined:
    Mar 5, 2016
    Posts:
    1
    I now use unity 3.3 instead of 3.2 but it makes no difference. The fix above solves the problem.