简体   繁体   中英

Android Studio using 100% CPU on an i7 processor for project Rebuild

My Windows 7 machine has a quad core i7 processor. When I Rebuild my project, it takes on average 25 seconds. And when I launch the app, it takes on average 36 seconds (before the app is uploaded to the device).

I have 588 files in my project's /src folder, which includes all of my java and xml code. I've got two .so libs each 5MB and 7 jars in my /libs folder.

See my attached screenshot. As you can see my CPU is maxed out at 100% the entire time. My iTunes music pauses, and I get a "Poor Performance" pop-up in the lower right hand corner of my windows taskbar. That's how bad it is.

I'm using Android Studio 1.2.1.1

Most of the time is spent during the preDex and dex operations.

Here's what I've tried so far (separately, I haven't tried them all together):

  1. adding gradle.properties -> "org.gradle.daemon=true"
  2. Power Saving
  3. Mode Invalidate Caches /
  4. Restart Global Gradle Setings -> Offline
  5. work Compiler -> Make project automatically

Nothing has worked yet. I can't imagine that this is a common problem, am I right? Am I being too imaptient because this really is that much slower than Eclipse?

I guess my questions are:

  1. Could this be due to the size of my jars or so files?
  2. I tookover a project that had many nested views in XML files. Could this be causing a problem?

I'm really reaching for straws so if anyone has any information, esepecially why the dex operation is taking up so much CPU, that would be awesome.

I guess it goes without saying that this is happening if I edit an XML file, do a rebuild, and then launch the app. If there's nothing to clean and rebuild... when I just do a Make Project... the average build time is 3 seconds.

在此输入图像描述

Here are the three improvements I was able to make:

I was preDexing my JARs every time I built the project, so I found this solution:

dexOptions {
    preDexLibraries = false
}

I was using the entire Google Play Services library:

compile('com.google.android.gms:play-services:+') {
    exclude module: 'support-v4'
}

When all I needed was Google Cloud Messenger:

compile('com.google.android.gms:play-services-gcm:+') {
    exclude module: 'support-v4'
}

In Eclipse, I would always do a Rebuild and then launch app with the play button. In Android Studio, now I am just doing a Clean and then launch app with the play button. Also the Run button in Android Studio does NOT work every time right after the Clean. This was causing what seemed to be delays because nothing was happening. So now I leave the Gradle Console open to make sure that the run button is working, and when it doesn't I just hit it a second time.

What I used to have:

Rebuild: 26 seconds
Launch:  36 seconds
Install: 15 seconds

and now:

Clean:    8 seconds
Launch:  22 seconds
Install: 15 seconds

which is a major improvement! Hopefully this helps someone else.

As stated on the tracker page for this issue , the team has identified this as the problem:

--parallel-threads only applies to project parallelization.

For android tasks that are running in parallel, we always create as many threads as possible

From the page, it seems that they target release 1.3 to address this (see comment #13 there).

In the meantime, what has helped me to cope on Windows 7 is to set the CPU affinity for the Android Studio process (and its child processes) to spare at least one of the cores (as suggested by comment #9 on the page).

There are many ways to do this, but you might want to try the top-voted answer on this superuser question (which suggested to use Process Lasso ) that appears to work well enough for me.

In addition to optimizations specific to Gradle (see below), I recommend that you try disabling anti-virus protection for your Gradle caches directory and your Android Studio project directory. For me, this reduces my build times by roughly 50%. Excluding those same directories from Windows Search indexing can also help.

Gradle optimizations I use, in ~/.gradle/gradle.properties.

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx6144m <-- Tweak this based on available RAM
org.gradle.caching=true
org.gradle.parallel=true
kotlin.incremental=true

Note that enabling caching means you sometimes have to explicitly clear your caches when switching branches. I run this script when I run into puzzling build issues.

#!/bin/bash

# Clean Android cache
./gradlew cleanBuildCache

# Clean Gradle cache, prompting for each directory
find ~/.gradle/caches -maxdepth 1 -name build-cache* -print -exec rm -rfI {} \;

# Clean Project
./gradlew clean

# Stop Gradle Daemon
./gradlew --stop

To be honest, Android Studio is hands down better than Eclipse because of the UI designer. The downside is that it uses gradle instead of Ant. Gradle is also better but slower - especially on Windows. It runs much better on Linux. If you haven't used Linux before, fear not. Linux Mint is a stable OS that has a UI that is similar to Windows. You'll be right at home in no time. It consumes fewer resources so that leaves more processing power for the gradle build. Make the switch. You'll never go back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM