Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After upgrading to Gradle 5 and Android Plugin 3.3, my build fails with "Metaspace"

This happened when I upgraded to Gradle 5.0 from 4.10.3, and also the Android Gradle Plugin from 3.2.1 to 3.3.0-rc03 (the latter because it's the first version of AGP to officially support Gradle 5).

When I attempt to sync my project after this, I get an error. The error only says:

* What went wrong:
Metaspace

Subsequent builds either also emitted this error, or something even more obscure, like:

* What went wrong:
Could not initialize class com.gradle.scan.a.e.c

There's no obvious solution to this problem. A search for "gradle metaspace" turns up release notes for Gradle 5, which themselves only refer to it here:

#7385 - Limit Metaspace used by Gradle

And the issue itself isn't the most helpful.

like image 883
AutonomousApps Avatar asked Jan 04 '19 19:01

AutonomousApps


1 Answers

As we know, Java 8 removed PermGen and replaced it with Metaspace. Pre-Gradle 5, no max was set for Metaspace, meaning it could grow without bound (which it would only do in the presence of a memory leak, either from a plugin or Gradle itself). With Gradle 5, a maximum limit of 256 MB is now set by default. Once the Gradle process hits this limit, the build will fail with Metaspace as the error. This limit is set because it is seen as both "large enough" and sane, and also with the explicit idea to discover memory leaks more quickly. (Source: I work for Gradle.)

This post gives a clue, but doesn't directly answer the question.

It turns out that increasing the metaspace limit is simple. Add this to your gradle.properties:

org.gradle.jvmargs=-XX:MaxMetaspaceSize=512m

Or some other value that makes sense to you. 512 is simply twice the default. Obviously this doesn't "fix" the problem, since the problem is a memory leak somewhere. But it will help.

like image 129
AutonomousApps Avatar answered Dec 17 '22 01:12

AutonomousApps