Siamak SiaSoft Siamak SiaSoft - 3 months ago 23
Java Question

Error:java.lang.OutOfMemoryError: GC overhead limit exceeded error on Telegram

I'm newbie to Android and trying to run Telegram source code provided on GitHub for Android but I faced with some terrible errors and after days of googling i found nothing at all!

I just followed instructions but it's not working. error say's

Error:java.lang.OutOfMemoryError: GC overhead limit exceeded

but even after increasing max heap size it's throwing the same error but more memory.

BuildVars.java:

public class BuildVars {
public static boolean DEBUG_VERSION = false;
public static int BUILD_VERSION = 821;
public static String BUILD_VERSION_STRING = "3.10";
public static String APP_HASH = "94706fdc9f5d86e2c3749bd8d0a2559e"; //obtain your own APP_HASH at https://core.telegram.org/api/obtaining_api_id
public static String HOCKEY_APP_HASH = "f7a81fd2b9cd40d3a8687311defcfe88";
public static String HOCKEY_APP_HASH_DEBUG = "f7a81fd2b9cd40d3a8687311defcfe88";
public static String GCM_SENDER_ID = "760348033672";
public static String SEND_LOGS_EMAIL = "ssiasoft@gmail.com";
public static String BING_SEARCH_KEY = "afb6312cb4ed4e78b8dfd177c6d174af"; //obtain your own KEY at https://www.bing.com/dev/en-us/dev-center
public static String FOURSQUARE_API_KEY = "HNRPHATHVHEBKXMVVLNTYXOGGUHPUSGVMB33TP2XNOFWMGGV"; //obtain your own KEY at https://developer.foursquare.com/
public static String FOURSQUARE_API_ID = "A9ABCD"; //obtain your own API_ID at https://developer.foursquare.com/
public static String FOURSQUARE_API_VERSION = "20150326";
}


and here's the full error message log:

:TMessagesProj:transformClasses_enhancedWithInstant+reloadDexForArmDebug UP-TO-DATE
:TMessagesProj:incrementalArmDebugTasks
:TMessagesProj:prePackageMarkerForArmDebug
:TMessagesProj:fastDeployArmDebugExtractor
:TMessagesProj:generateArmDebugInstantRunAppInfo
:TMessagesProj:transformClassesWithDexForArmDebug
To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
Error:UNEXPECTED TOP-LEVEL ERROR:
Error:java.lang.OutOfMemoryError: GC overhead limit exceeded
:TMessagesProj:transformClassesWithDexForArmDebug FAILED
Error:Execution failed for task ':TMessagesProj:transformClassesWithDexForArmDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_40\bin\java.exe'' finished with non-zero exit value 3
Information:BUILD FAILED
Information:Total time: 13 mins 5.264 secs
Information:3 errors
Information:0 warnings


Any one knows why this happens and how to make it run?

and I'll be so grateful to anyone who has a working Telegram project for sharing that code with me.

Answer

Problem

As it's said in the log message, Gradle needs more memory because it uses Dex In Process feature.

To run dex in process, the Gradle daemon needs a larger heap. It has approximately 910 MB. For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.

Gradle is itself written in Java, and like any other Java program the amount of memory it has is limited by the heap size.

Heapsize, Stacksize and Garbage Collection Fundamentals

Solution

Option 1 - Increase Heap Size

To increase the heap size, increase the maximum available amount of heap - set a JVM option -Xmx. Try setting 2GB as the log message suggests: -Xmx2048M. In Gradle, it's done by writing org.gradle.jvmargs=-Xmx2048M in the project gradle.properties. This file is located next to build.gradle. If it doesn't, create one manually.

After that, if the error persists, try to explicitly tell the Dex In Process feature how much memory it now has - add this to the build.gradle (app module). Note, that it should be smaller than the total amount of memory available to Gradle, i.e. if Gradle has 2GB, set 1GB for Dex In Process:

android {
 ....
 dexOptions {
    // To prevent OutOfMemory
    javaMaxHeapSize "1G"
  }
}

Try bigger values, actual values depend on a project.

Option 2 - Turn Off Dex In Process Feature

Although builds are said to be slower without this feature, try turning it off.

android {
    // ...
    dexOptions {
        dexInProcess = false
    }
}
Comments