Getting Gradle error "Could not reserve enough space for object heap" constantly in Intellij IDEA

39,040

Solution 1

Installing 64 bit Java solved it for me

Solution 2

I got the same error in Visual Studio 2015 with cordova 5.1.1 under Windows 10.

To solve the issue, set this environment variable:

_JAVA_OPTIONS=-Xmx512M
  1. Right click on start-button and open "System"
  2. Search for "Advanced system settings" and open it
  3. Click the button "Environment Variables ..."
  4. In System Variables, click "New..."
  5. New Variable Name: _JAVA_OPTIONS
  6. New Variable Value: -Xmx512M
  7. Click OK
  8. Restart Visual Studio, so the variable is picked up

This Article point me in the right direction. I did change the instructions a littel bit, so they fits to Windows 10.

Hope it helps someone!

Solution 3

Although this question is a bit older I would like to come up with the (possible) rootcause of the problem: the JVM requests a whole block of memory on startup. If it couldn't find it, the "Could not reserve..." error occurs. John Pape wrote an enlighting article in IBM's devblog about this issue: https://www.ibm.com/developerworks/community/blogs/aimsupport/entry/why_wont_my_jvm_start_with_this_heap_size?lang=en

This is solving the problem for me:

  • variant a: using a 64 bit version of Java, so to give Java a bigger area of possible connected free memory
  • variant b: With 32 bit Java using smaller XMX amount. In the gradle.properties file I just use org.gradle.jvmargs=-Xmx150m and Gradle has no problems.

Solution 4

This happens to me too and it drives me nuts. It could be that the memory (ram) available is fragmented. Although you have enough free memory, there is no contiguous 512MB blocks available, it's all fragmented into smaller blocks! This is why you only see it "sometimes".

Some things worth trying:

  • check if you're low on memory, try to close other apps. This happens to me sometimes even if I still have a good 3GB free out of my 8GB! That 3GB can be fragmented in blocks which are all smaller than 512MB [ex: if you have -Xmx512m]... Rebooting is a way to defrag your memory. Or close firefox, that'll free up a gig ;-)

  • also, surprisingly, check your swap space [free disk space]! Depends of machine/JVM/etc implementation. But if you're low on swap space and your memory is fragmented, that could be another cause.

  • lower your -Xmx to maybe -Xms128m -Xmx512m to make it easier to find a contiguous block of free memory [128m]. This might not be a good idea if gradle truly needs 512m minimum. Given that we're talking about gradle, ignore this bullet point (could be a useful tip for other cases/worth noting).

  • double check your Xms/Xmx in ~/.gradle/gradle.properties attribute: org.gradle.jvmargs

  • use idea64.exe [64bit] and if you think IDEA is using the wrong JDK [32bit], in latest Intellij IDEA [14.1.3 as of this writting], go to "Settings"->"Build,Execution,Deployment" -> "Gradle": "use the default wrapper" and select the Gradle JVM you'd like (if you think it uses the wrong one, you'll see it here".

  • finally, this is just a guess but: in the previous bullet, I wonder if you select "use project JDK", if that makes it easier to find contiguous memory. I'm not sure if that means that Gradle will use intellij IDEA's already reserved memory block. So, instead of having two processes, you only have one and you could reserve a bigger memory block when IDEA starts up. But I'm not sure if that's accurate of if that's an option to suggest to Intellij...

I agree with you, I've never had any memory issues with IDEA [well no issues AT ALL] until gradle integration came along.

Solution 5

In your android folder there is a gradel.properties file and add the following lines:

org.gradle.jvmargs=-XX\:MaxHeapSize\=256m -Xmx256m

I ran into this while using Intellij and Flutter. Also another property you may find useful to add is declaring where the JDK is.

org.gradle.java.home=C:\\Your\\JDK\\path
Share:
39,040
Nilzor
Author by

Nilzor

I am a software engineer developing for Android, Windows, Windows Phone and any other platform I find interesting. Currently working for Schibsted with Android phones, but I have also years of experience in the consulting industry on the Microsoft platform. I have an MA in Computer Science from NTNU, Trondheim.

Updated on June 21, 2021

Comments

  • Nilzor
    Nilzor almost 3 years

    So I have a problem with memory allocation. Sometimes it works, and sometimes it doesn't. I've read this thread and tried the advice there multiple times. Sometimes -Xms512m -Xmx768m works, sometimes -Xms256m -Xmx512m. I'm sick and tired of having to tweak this setting in IntelliJ under the Build, Execution, Deployment > Build Tools > Gradle > Gradle VM options setting. Is there no setting that solves this once and for all?

    I have 16GB of RAM on my Windows 7 computer, I'm running IntelliJ IDEA EAP 14 build 138.2210.3, 64 bit version. Android Gradle build plugin v0.12.

    I'm suspecting gradle is not running in 64 bit mode, or else it would have ~7 GB of free memory to eat from. Why is it not utilizing this?

    EDIT:

    I got the same error in Visual Studio 2015 with cordova 5.1.1 under Windows 10. See my solution below.

  • racs
    racs over 8 years
    Thanks! This answer just helped me with the new version of Android Studio.
  • Sabuncu
    Sabuncu over 7 years
    Thank you, +1. This answer solved similar problem when using Apache Cordova on Windows.
  • suraj mahajan
    suraj mahajan over 7 years
    Thanks Bjoerg, +1,
  • Hamed
    Hamed almost 7 years
    This solution worked for me with Ionic 3.5.0 and Windows10
  • Krummelz
    Krummelz almost 7 years
    @nilzor, JDK or JRE?
  • VahidNaderi
    VahidNaderi almost 6 years
    I had both 32 and 64 bit installed and the 32bit version was in the path.
  • Donald Shahini
    Donald Shahini over 4 years
    Great. Solved my problem when using Cordova on Windows. +1
  • Hanlet Escaño
    Hanlet Escaño over 2 years
    Dec 2021 this is what worked for me. Changing the gradle.properties file's option directly kept on getting overridden. Thanks.