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
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.
Installing 64 bit Java solved it for me
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
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
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!
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:
org.gradle.jvmargs=-Xmx150m
and Gradle has no problems.In my case I had too much things opened. Freeing memory made it work.