locked
Java heap space OutOfMemoryError when binding a big .jar RRS feed

  • Question

  • User4675 posted

    Hi, I'm trying to bind BurstlySDK to my mono for Android project.

    According to the steps described here http://docs.xamarin.com/guides/android/advancedtopics/javaintegrationoverview/bindingajavalibrary_%28.jar%29

    I did the following:

    created Android Java Bindings library project called "BurstlyBindingLibrary"

    created Jars filter there and a corresponding folder, copied BurstlySDK.jar, added to project, set EmbeddedJar in properties.

    added 2 other jars on which Burstly was dependant (admobfix.jar and joda-time-2.1.jar) and marked them as EmbeddedReferenceJar in properties.

    tweaked the Metadata.xml to remove and rename some nodes in API.xml so that BurstlyBindingLibrary finally compiles.

    to the main project added the same 3 jars and marked them as AndroidJavaLibrary in properties

    right-clicked on the main project, Add reference, selected project BurstlyBindingLibrary ("BurstlyBindingLibary" now shows in References of the main project)

    Now, when trying to build the main project, I get the following error:

    COMPILETODALVIK : UNEXPECTED TOP-LEVEL error : 
      java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2882)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
    at java.lang.StringBuilder.append(StringBuilder.java:119)
    at com.android.dx.rop.type.Prototype.withFirstParameter(Prototype.java:370)
    at com.android.dx.rop.type.Prototype.intern(Prototype.java:180)
    at com.android.dx.cf.iface.StdMethod.<init>(StdMethod.java:45)
    at com.android.dx.cf.direct.MethodListParser.set(MethodListParser.java:80)
    at com.android.dx.cf.direct.MemberListParser.parse(MemberListParser.java:217)
    at com.android.dx.cf.direct.MemberListParser.parseIfNecessary(MemberListParser.java:108)
    at com.android.dx.cf.direct.MethodListParser.getList(MethodListParser.java:54)
    at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:532)
    at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:396)
    at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:378)
    at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:241)
    at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:112)
    at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:87)
    at com.android.dx.command.dexer.Main.processClass(Main.java:487)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
    at com.android.dx.command.dexer.Main.access$400(Main.java:67)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
    at com.android.dx.command.dexer.Main.processOne(Main.java:422)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
    at com.android.dx.command.dexer.Main.run(Main.java:209)
    at com.android.dx.command.dexer.Main.main(Main.java:174)
    at com.android.dx.command.Main.main(Main.java:91)
    

    Googling only finds this: https://code.google.com/p/android/issues/detail?id=20814 which says the jar has >65k methods. Is this the case here, too(?)

    BurstlySDK.jar is pretty big (2.8MB) and contains lots of stuff inside, probably indeed >65k methods.

    I tried making a usual android project in Eclipse and added the BurstlySDK.jar there, it builds fine and runs on the device, though it seems to strip the jar (BurstlySDK....jar inside bin/dexedLibs/ is only 1MB instead of 2.8MB) since the test project in Eclipse does nothing except BurstlySDK.init(..).

    If the huge amount of methods is the problem, then I guess the solution would be to remove more nodes from API.xml, maybe entire packages. Or is this something else.

    Any suggestions? Thanks!

    Monday, March 11, 2013 11:13 AM

All replies

  • User4675 posted

    Ok, so even after I remove all nodes with

    <remove-node path="/api" />
    

    and don't access any code from the jar in the main project, the same error still happens. Btw the resulting BurstlyBindingLibrary.dll file is still 3MB, I wonder why since there are absolutely no .cs files in obj/Debug/generated/

    Monday, March 11, 2013 12:16 PM
  • User4675 posted

    Another solution could be to increase Java heap space with

    java -Xmx2g 
    

    but where can one edit this for Mono?

    Monday, March 11, 2013 1:59 PM
  • User4675 posted

    In some places including here http://stackoverflow.com/questions/1434779/maximum-java-heap-size-of-a-32-bit-jvm-on-a-64-bit-os I've seen that the OutOfMemory error can be solved if one uses 64-bit version of Java.

    I installed both 1.7 and 1.6 64bit versions of JDK but can't find where to tell Mono which JDK to use.

    Here http://forum.xda-developers.com/showthread.php?t=761183 they say to edit the file find_java.bat in the SDK's installation.

    Seems like Mono plugin for Visual Studio installs (on Windows 7 64 bit) it's own copy of Android SDK into

    c:\Users\USER_NAME\Local\Android\android-sdk\
    

    the file findjava.bat should contain %PROGRAMFILES% or %PROGRAMFILES(X86)% which could be changed to take the 64 bit version of Java. But apparently Mono has them tweaked and they just call findjava.exe.

    I don't remember exactly but there seem to be a mentioning in the official Android SDK docus that it won't compile on Java 64 bit.

    Seems like I've run out of ideas how this error could be solved.

    Monday, March 11, 2013 2:23 PM
  • User4675 posted

    Just another comment: I've tried binding a very small jar file (with just 1 class TestMath which contains 1 tiny method sum(a,b) ) instead of BurstlySDK.jar and as expected both the binding library project and the main project compile OK. So the reason of the error is apparently that with my current machine setup Mono can't handle a big .jar file.

    Sadly, I have no influence of the .jar file: I get it from a different department and in fact don't even know which parts of it can be stipped out and which not, so ideally everything should stay included and as jew nodes removed from API.xml as possible.

    I'd appreciate any help.

    Monday, March 11, 2013 2:40 PM
  • User4675 posted

    Apparently find_java.bat was changed by Google, there is no %PROGRAMFILES% in it also when it is inside the ADT bundle from the official Android developer page.

    I have a JAVAHOME environment variable set to C:\Program Files\Java\jdk1.7.017 which is the 64bit java but this seems to have no influence on anything.

    Monday, March 11, 2013 3:12 PM
  • User730 posted

    I don't know if this will fix your issue, but to change the heap size you can use JAVAOPTIONS

    Not well documented and not standard, I guess, but it seems to work.

    The JVM picks up the contents of both JAVATOOLOPTIONS and JAVAOPTIONS

    https://forums.oracle.com/forums/thread.jspa?messageID=5203488 http://stackoverflow.com/questions/9677346/java-system-environment-variable http://docs.oracle.com/javase/1.5.0/docs/guide/2d/flags.html

    You can see (and change) the JDK Xamarin uses, in Mono or XS from: Tools>Options>Projects:SDK Locations. I don't think Java 7 is supposed to work.

    Monday, March 11, 2013 5:03 PM
  • User48 posted

    As of Xamarin.Android 4.6, you can provide the $(JavaMaximunHeapSize) and $(JavaOptions) MSBuild properties to let Java allocate enough memory to complete your build.

    You can do this by hand-editing your .csproj and adding the following section:

    <PropertyGroup>
        <JavaMaximumHeapSize>1G</JavaMaximumHeapSize>
    </PropertyGroup>
    
    Tuesday, March 12, 2013 3:25 AM
  • User4675 posted

    Thanks! The solution to add _JAVA_OPTIONS environment variable with value -Xmx1g worked.

    Hover, adding the JavaMaximumHeapSizetag into the .proj file didn't help.

    I added the property right in the beginning of the file. Is this the right way to do it?

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <JavaMaximunHeapSize>1G</JavaMaximunHeapSize>
      </PropertyGroup>
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProductVersion>8.0.30703</ProductVersion>
    

    It would or course be preferable to have this setting right in the proj file so that anyone else working on the project would have this working out of the box.

    Tuesday, March 12, 2013 9:26 AM
  • User48 posted

    <JavaMaximunHeapSize/> is mis-spelled; it should be <JavaMaximumHeapSize/> (note n vs. m). (Because I mis-spelled it originally. Oops. Corrected.)

    Tuesday, March 12, 2013 7:13 PM
  • User4675 posted

    Sorry, I didn't notice that, too. Thx

    Wednesday, March 13, 2013 12:24 PM
  • User11034 posted

    Nice. What's the default value? Getting a heap memory exception on a 64 bit OSX with plenty of memory is just wrong.

    Thursday, May 9, 2013 1:28 PM
  • User11987 posted

    Is there maximum value that JavaMaximunHeapSize>1G

    My project compiles ok with JavaMaximunHeapSize>1G

    but if I put it up to JavaMaximunHeapSize>2G

    I get: "error MSB6006: 'java' exited with code 1."

    when I compile

    C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1018,3): error MSB6006: "java" exited with code 1.

    Thursday, May 23, 2013 4:03 PM
  • User48 posted

    Is there maximum value

    I don't know, but it certainly looks like it.

    Friday, May 24, 2013 3:13 AM
  • User9453 posted

    Has the spelling mistake been fixed now? It seems to have been. If so can you reply to this thread :-)

    Thursday, June 6, 2013 11:20 AM
  • User15691 posted

    Hello @jonp, I have the same problem. I have been changed all you write, but the problem persist. Only I have a listview with 20 row and the row have 1 icon and 1 edittext.

    Monday, August 5, 2013 5:45 PM
  • User29061 posted

    dear Jonp It worked for me.you saved my time.thank you...

    Tuesday, December 24, 2013 9:07 PM
  • User12927 posted

    Changing the Heapsize in the .csproj file did it for me aswell, thanks alot!

    Tuesday, July 29, 2014 6:28 AM
  • User21647 posted

    Thanks guys! Personally, I had to do it three times in my .csproj file, once at the top, and once each in the Debug and Release configuration sections further down. After that it was fine.

    Monday, September 15, 2014 9:45 AM
  • User11137 posted

    Keep in mind, depending on the versions of the templates you have it looks like an empty tag is inserted for JavaMaximumHeapSize, if you insert your setting before that one, I think it is replacing your larger value with the default value when it hits that line, so search your csproj for other occurrences of JavaMaximumHeapSize. This has burned me a few times recently.

    Monday, September 15, 2014 6:14 PM
  • User68083 posted

    I am using Xamarin and I pull 1G in my Main.axml file and i still get the

    File not found: C:\Users\blank\Desktop\WPBNoise\WPBNoise\COMPILETODALVIK

    Tuesday, November 11, 2014 2:55 PM
  • User2595 posted

    You can also set the Java heap size / arguments in the build settings section (right click on project in solution explorer, Options/Android Build/Advanced Tab :) Setting the heap to 1G there solved it for me.

    Thursday, December 4, 2014 11:29 AM
  • User33959 posted

    it seems that 1G JavaMaximumHeapSize still not enough to my app building. How can I increase the size more e.g. 2G as I have 8G memory in the Windows

    If I have to install 64 bit Java to get more heap size, is there any settings to configure to tell Xamarin use the 64-bit Java?

    Thursday, March 26, 2015 4:03 AM
  • User160415 posted

    Worked for me. In my case (VS 2013 PCL Project) I did it in the property page of the project. Double click in 'Properties' in the Solution Explorer, then Click on 'Android Options' and click in 'Advanced' tab. There you find 'Java max Heap Size', write 1G and save. It will modify the *.csproj file.

    Thursday, February 4, 2016 1:00 PM
  • User203051 posted

    i have a list with 20 items. each item has a frame with some text. i have changed the to 1g and even to 2g. but nothing helped me.

    plz some one help me.

    Friday, April 15, 2016 11:36 AM
  • User14430 posted

    Same thing here. The same project was working fine before upgrading Xamarin. Now 1G or 2G does not help at all.

    Sunday, June 26, 2016 6:19 AM
  • User14430 posted

    So my research led me to the point that if I upgrade the Facebook SDK component from 4.13.1.0 to the latest version I start getting this error no matter what version of Xamarin.Android I am using.

    Sunday, June 26, 2016 7:53 AM
  • User22454 posted

    @JonathanPryor Brilliant

    
        1G
    
    

    worked perfectly. Thanks!

    Tuesday, June 28, 2016 1:58 PM
  • User202922 posted

    1G worked perfectly for me too!

    Wednesday, July 13, 2016 3:23 PM
  • User236483 posted

    In a new Hello World PCL XAML Blank project, when you just upgrade Xamarin Forms to 2.3.0.107 you will need increase the java heap.. :(

    Thursday, July 14, 2016 8:45 PM
  • User65168 posted

    @ViniciusSchneider to how much you increased it? I put 1G and I'm still getting the error:

    C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets (1853, 3) java.lang.OutOfMemoryError. Consider increasing the value of $(JavaMaximumHeapSize)

    Any suggestions?

    Friday, July 15, 2016 7:23 PM
  • User236483 posted

    Just 1G and worked for me. I tried another values in the past but was not accepted. I did not found documentation about the valid values.

    @VictorHGarcia said: @ViniciusSchneider to how much you increased it? I put 1G and I'm still getting the error:

    C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets (1853, 3) java.lang.OutOfMemoryError. Consider increasing the value of $(JavaMaximumHeapSize)

    Any suggestions?

    Saturday, July 16, 2016 7:04 AM
  • User235635 posted

    yep 1G worked for me to. Going over 1G always failed :(

    Wednesday, July 20, 2016 5:57 PM
  • User248282 posted

    Thanks! It was the correct fix.

    <PropertyGroup>
        <JavaMaximumHeapSize>1G</JavaMaximumHeapSize>
    </PropertyGroup>
    

    Here's the steps I did: 1. Closed the VS2015 2. Changed the file .csproj 3. Re-open VS2015 4. Re-build 5. Done!

    Tuesday, October 4, 2016 5:50 AM
  • User177806 posted

    Thanks the 1G solution worked.

    Tuesday, November 22, 2016 5:35 AM
  • User277421 posted

    For me 1G worked. Thanks.

    Tuesday, November 22, 2016 7:44 PM
  • User280187 posted

    worked for me! Thank you

    Thursday, December 22, 2016 2:38 PM
  • User260659 posted

    Worked for me! Thank you

        <PropertyGroup>
        <JavaMaximumHeapSize>1G</JavaMaximumHeapSize>
        </PropertyGroup>
    
    Wednesday, January 11, 2017 3:06 PM
  • User243044 posted

    Many Thanks to JonathanPryor

    Tuesday, January 17, 2017 7:49 AM
  • User223936 posted

    Followed Mark Cerezo order of operations and all is good now. Thanks!

    Tuesday, February 21, 2017 5:22 AM
  • User208280 posted

    While i was struggling a long time on this, and i also changed the heap size to 1GB, the only simple and stupid solution that helped in this, was "Clean Solution", it looks that moving from "Release" to "Debug" or the other way, affects each other in some way that should be fixing. Again "Clean Solution" command solved my head scratching annoying bug.

    Thursday, May 4, 2017 8:47 PM
  • User320251 posted

    @JonathanPryor @JenniferRostenne

    Neither 1G nor _JAVA_OPTIONS environment variable with value -Xmx1g helped me...

    please help.

    Tuesday, May 16, 2017 4:18 PM
  • User221588 posted

    Its working for me. In Visual Studio, settings available in Properties - > Android Options -> Advanced -> Advanced Properties -> Java Max Heap Size. Set it to 1G.

    Friday, September 1, 2017 10:21 AM
  • User332560 posted

    Setting up Java Max Heap Size to 1G in ProjectName.Android-->Advanced Android Options worked for me

    Tuesday, November 7, 2017 11:35 AM
  • User363983 posted

    @arslanan resolved this problem. After the latest update of Xamarin, you should delete JAVA from Environment Variables. Steps:

    This pc -> Properties -> Advanced System Settings ->Environment Variables -> (you will see java below system variables).

    Friday, December 22, 2017 9:39 AM
  • User319170 posted

    Using Visual Studio

    Thanks!! Using the Visual Studio Project Properties i set the size to 1G (1 Gigabyte) and it worked like a charm.

    Saturday, July 21, 2018 9:33 PM
  • User343509 posted

    I changed heap size(1G) in android property and it's worked for me

    Saturday, July 28, 2018 6:22 AM