Wednesday, March 2, 2011

Tip: Speeding Up Your Android Emulator Launch

The latest version of the Android emulator comes with a feature called "snapshots." It needs to be enabled as a feature of each AVD. Luckily, this version also includes the ability to edit existing AVDs.

First, enable the feature:

Enabling Snapshots

Second, when launching the AVD, choose to load from the snapshot and save the snapshot. When a snapshot isn't found, the AVD boots up from scratch. As we all know, this takes quite a long time even on very fast machines.

Enable Snapshot options

Now, when you exit, the system will store off a snapshot of the state of the AVD. This takes a little while, depending on how much RAM is assigned to the AVD. After saving the state once, the AVD will now launch very quickly - usually in just a couple of seconds.

However, the exit is no longer super speedy and often triggers "Not Responding" type messages. If you always want to return to exactly where you left off, this is how it will work. Overall, the behavior is much faster. However, if you want it to come up clean each time, just make sure the first time you boot it's clean, then exit to save the snapshot. Now, when you launch the AVD, only check load from snapshot, but make sure save to snapshot is unchecked.

Don't save over old snapshot

Now, the system will just load the AVD from the one snapshot you created and not save the state each time you exit. This means super speedy launches into a cleanly booted emulator as well as super speedy exits since the snapshot doesn't have to be saved each time.

You'll start to feel like you don't need to keep the emulators running all the time. You also won't necessarily go looking for a phone each time just to save the emulator boot-up time. (You'll still go after the phone or tablet when debugging for performance or with code bases that are otherwise slow on the emulator or other such reasons.)

How much difference does it actually make? Here are some test results running on a 6 core 3 GHz desktop with 8GB of RAM and a relatively speedy SSD:

  • Cold launch of Android 3.0 emulator to a usable state: 4 minutes 35 seconds
  • Snapshop launch of same Android 3.0 AVD: 8 seconds

That cuts the emulator launch time by 97%. Put another way, the cold launch takes 34 times longer. Taking these steps is well worth the minimal additional effort.


Happy Android Coding!

27 comments:

cineto said...

I just recently started developing on android and found the speed of the emulater good enough. It should be the solid state disk that host the emulator image.

Shane Conder said...

The emulator performance, for me, is reasonable at 480x320, poor at 800x480, and almost unusable at 1280x800.

However, this tip is dealing with launch performance. I've updated the post to include the change in launch times on my desktop machine. I hadn't measured it before, but a 97% savings is pretty substantial. :)

eZee.se said...

Oh my!
This is one of the most useful tips on this entire blog! Thank you!

One quick question: (I am on the SAMs book, hour7) in the book at the end of the chapters there are "exercises" that the reader is supposed to do. Sometimes I get stuck on these and re-read the chapter a one or twice more... but still cant solve it (I admit I am rusty on Java) - are the answers to these exercises somewhere else or are we really totally alone in figuring these out?

Thanks!

Shane Conder said...

Hi eZee.se,

I'm not sure which of the exercises you're having trouble with but we've had some reader feedback that the exercises are too difficult. Basically, we treated the exercises as "challenges" - meant to push your experience beyond what you learned so you get even more out of the book. Most readers seem to want "review" exercises; therefore in the second edition, we are making the exercises substantially easier. So basically, don't concern yourself if you are stuck on a particular exercise in the first edition. Come back to it later (after reading further, or feel free to ask us on this blog about specific questions...

Here are hints for each exercise in Hour 7:

1. This is talked about on pages 122-123.

2. This is also talked about on pages 122-123.

3. Most of the changes needed are given right in the exercise. You may have to look up more information on the control and you will have to find your own video. A good place to look are the small animation videos at archive.org -- most of those come in mobile-friendly formats. An alternate is to record a short clip with your Android device and use that.

eZee.se said...

I usually go through a book in a very linear page by page way, not going to the second page till I complete the first.

And although I like a good challenge constant googling for something not really covered like setOrder() (from exercise 1) and how to implement it takes quite a bit of time.
I'm sure once I get some experience under my belt it would be easier but right now it's pretty confusing and hard.

> Most readers seem to want "review" exercises;
This makes sense, if you have taught me 15 pages and ask me questions or make me do things that cover those 15 pages (or the 15 before that) I think it would even make me feel good that I was able to answer those questions/exercises.

> we are making the exercises substantially easier.
I hope not too easy!
I have no problem with the difficulty of the exercises, just that it covers something that has already been taught to me or pointed out to me.

For example:
Blah blah blah and also check out the related attribute fuBar()

then give me a exercise where I have to use fuBar()
would be moderatly hard but still very acceptable.

> or feel free to ask us on this blog about specific questions...
Im afraid I would be bothering you guys for nearly every exercise to come then. :(

> An alternate is to record a short clip with your Android device and use that.
I don't have an android device and depend on the emulator. I am waiting for the dual core HTCs to hit before I buy one.

Laurie Darcey said...

Just to be clear, we did not consider the exercises "too difficult" the first time and in almost all cases, everything needed is right there in the chapter and the exercises already get progressively more difficult. There are a few (less than half a dozen in the whole first edition) exercises that we now consider unfairly difficult for beginners based on some feedback and we decided to remove them and add more "feel good" beginner exercises in their place because that is what most readers seem to desire.

Having to look up a class method by name that isn't explicitly covered in the book is a reasonable exercise after Hour 2. People wishing to write Android applications must be capable of looking up SDK class documentation, as we discuss in the first few chapters in depth. Learning to use the docs (locally installed or online) is part of learning Android and you cannot live without it, just like learning to use the library is part of any 3rd grader's education. Some readers do not seem to want to use the SDK documentation at all, and this is not something we can recommend or justifiably support.

eZee.se said...

> and this is not something we can recommend or justifiably support.

Absolutely!

But a lot of us don't really know how to use the docs well enough and are just getting our feet wet.

Perhaps an explanation on using the docs properly would have helped.

Using example one of "setOrder()", a quick google search gave me the definition and tells me it takes an int (2=random).
(http://developer.android.com/reference/android/view/animation/LayoutAnimationController.html#setOrder%28int%29)

Great, but where and how do I use setOrder()?

Now I know it, but for someone starting out in the basics (whihc is what this book is about) its hard to figure out.

I come from a Java (ages back) and PHP background, the docs for the php methods have some pretty good explanations for everything(for example rand(),explode(),implode() etc) but here these pages don't really have these examples and since we are starting out... its hard to figure out exactly whats meant when its not covered before.

While I get (and totally support) your attempt to make us better programmers remember one thing:
Just throwing a person into the pool will get some swimming but some are going to drown without help.

eZee.se said...

One last thing to keep in mind.

I am a guitarist, programmer and artist (among other things) and sometimes I help friends with all of the above.

Many a time a friend cannot get a certain concept in programming/sketching, or a certain fingering in playing the guitar and I either think or say "it's easy, just do this" and have gotten a frustrated "it's easy for you!" back.

You guys have been playing around with android/java/iphone etc so long it's almost second nature - and like with me; its easy to sometimes forget how it was when we were learning the basics.

Shane Conder said...

First, you are basically on the first coding chapter of the book and it's natural to be a little anxious. We recommend "just keep reading" when you get stuck on an exercise.

> Great, but where and how do I use setOrder()?

We didn't see the exercises as "try it yourselves" with complete steps. You're meant to figure out the steps yourself based on the hour and previous hours information. The specific exercise you are asking about (a) asks you to do something specific that is very similar to what is done on page 122 (b) gives you a hint on how to do it by giving you the exact class name and method name to call, (c) we provide you with the exact parameter to send to that method. As such, we do not consider this exercise one of the difficult ones. Learning how to randomize animations is not a fundamental concept you need to fret over, but doing the exercise shows you understood the discussion about how there are different ways to apply animations to views. Does that clear things up or would you like an answer?

>Perhaps an explanation on using the docs properly would have helped.

Reread Hour 2, especially around pages 27-29. Note the sentence at the top of page 29 that basically suggests that you "Now is a good time to go learn to use the docs." We discuss the docs again in the first Q&A of that hour and finally, throughout the book, we give URLs to specific documentation implying you really need to check it out. We will consider adding an exercise to Hour 2 to look up a class method by name in the documentation.

All development books are companions to the platform documentation and we always try to state this explicitly throughout the book, especially since the Android SDK gets updated more often than most.

>but here these pages don't really have these examples and since we are starting out...

As we say in the book, the Android documentation is quite well put together. It has search. You can sometimes find a method example usage on the web, or in the source code provided with the book. Just try it out. What's the worst that can happen? Your app crashes? Fix it. Talked about debugging in Hours 1-2.

>Just throwing a person into the pool will get some swimming but some are going to drown without help.

Hate to break this to you, but you voluntarily jumped in this pool when you wanted to learn a skill in 24 hours... Luckily, nothing bad will happen in this pool. And, in your example, those that don't drown learn better and faster. :) Besides, the exercises are optional. You will learn what you need to learn in the book without the exercises. The exercises are meant to throw you in and make you work a little harder.

eZee.se said...

Thanks for replying!

Shane Conder said...

Hi eZee.se,

I thought I'd point out here that we just posted a walk-through of the exercise you referenced.

eZee.se said...

Very cool!

I came to pretty much the same ending but on a different line.

I added the "controller.setOrder(2);"

right after:
"LayoutAnimationController controller..."

I liked that post as it even explains your thought process, but like I mentioned above its your thought process that is backed by a LOT of experience which makes it flow naturally.
Hopefully I will get there after a while but for total newbs like me starting out that flow is more like a forced trickle.

Please also note that I mentioned that I _totally_ support you guys trying to make us better programmers by making us explore ways to accomplish something.
Hindsight is 20-20 of course and looking back at how I solved it it seems easy. But getting there as a total newbie was not.

I am not criticizing you guys or making this personal in anyway (and I hope I did not come off as such), I think you have done an awesome job with the book (so far I am on hour 9 and will prob be getting Android Wireless after I am done with this) just mentioning some things that I feel could have made the journey to Android goodies smoother.

Tony said...

I'm confused about an aspect of this snapshot feature. I've played around with it and understand how to get it setup to boot and exit quickly using a snapshot. But it seems that when you run the emulator this way it doesn't allow you to install or rebuild any apps since it constantly loads from the same snapshot.

I set up the snapshot feature through the separate "SDK and AVD Manager", but afterward I usually run the emulator only through the "Run Android Application" button in Eclipse. This is the only way that I know how to get an app installed to the emulator. But doing this is pointless since the emulator loads from a snapshot so the app either doesn't get installed or the new code changes don't get reflected.

It's great that it loads so fast but it seems like it's only meant to play around with an Android platform or with your app after you've installed it (using a fresh and slow reboot) and then saved a snapshot. This of course defeats the purpose of using snapshots if I have to reboot every time I want to install a new app or make code changes anyway. Am I missing something here? Thanks!

Shane Conder said...

Hi Tony,

I'm not sure I understand your problem. For me, once I have emulators running (1 or more) they behave exactly like connected physical devices.

That is, when I choose the Debug or Run drop down in Eclipse, and then choose one of my configurations, the emulators come up in the Android Device Chooser dialog. Which one the app is loaded on to run or debug depends on which one I choose, at that point.

It makes no difference if it's in the snapshot or not. In fact, I prefer when apps aren't in the snapshot because then I'm loading them on a far cleaner emulator environment than otherwise.

Part of what I don't understand is when you say "I run the emulator with the Run Android Application" -- which is it? The run and debug buttons are for the app -- to load it up on a device or emulator instance. Running the emulator is generally done separately and only once (well, per "work" session).

I generally have my run and debug configurations set to manual so I can make sure to choose the running emulators or connected devices. But, even if I didn't and there was nothing running, I'd still get the dialog where I can choose to launch the emulator.

Once the emulator is running, I do not generally close it. I can exit the app, I can use the Eclipse buttons to stop the app if I choose. I treat it just (basically) the same as a powered on device.

In addition, each time I choose the run or debug configuration, and then the target to run it on, Eclipse uses adb to install the APK and launch that. This happens every time the APK changes. So, I don't understand what you'd be doing that would not allow it to install or rebuild apps. Installing happens each time. Rebuilding doens't involve the emulators or devices. And neither of these, as far as I've seen, has anything to do with how the emulator started.

I usually launch the emulator from the SDK and AVD Manager, but sometimes I'll use the Android Device Chooser (it's Start button, of course).

So, I must be missing something from your question...

Thanks for the feedback! We'll figure out what's going on. :)

xstream said...

I think he does not want to start the emulator separately as eclipse does that automatically if you just press the start button and the emulator is not already running. The problem is that if you do it that way the "save snapshot" option is enabled for that launch of the emulator. To get the desired behavior you simply need to:
0. follow the instructions from the post
1. click on the small arrow next to the start button
2. select run configurations
3. select target
4. add -no-snapshot-save under additional emulator command line options
5. click apply

Tony said...

Hey Shane and xstream, thanks for the replies. I feel silly now. After playing around some more I realized what you were talking about Shane. For some reason I was under the impression that I needed to restart the emulator each time I wanted to install a new app or after I've made changes to the code. I've realized now it behaves exactly like you say, which of course makes the convenience of the snapshot feature apparent (besides taking up >500MB diskspace).

As xstream mentioned I wasn't launching the emulator separately. Instead I was just hitting the Run button in Eclipse and that automatically launched one of my AVDs. I've since realized this method sucks since I have to enter all options (e.g. scaling screen size and -no-snapshot-save) into the command line in the project's Run Configuration. So now I do as you say and just launch emulators through the SDK and AVD manager separately.

On an unrelated note I find it strange that the AVD manager includes options to create an AVD with audio playback/recording support when it seems the emulator has no functionality to support these things. I've even noticed that the "mic" that usually shows up in the Google search widget disappears a few seconds after the emulator launches.

Shane Conder said...

Hi Tony,

Thanks for following up. That explains a lot. :)

I haven't fully tested, but I think some of the settings may be for testing detection code, not necessarily for something that can work or not. Not sure, though. There are quite a few. :)

Cheers!

Carl said...

I got hit with a major GOTCHA! when trying to launch the emulator from a snapshot today.

My habit is to use the run button (usually with automatic AVD selection). Of course, this launches directly into the emulator without any launch options.

Another unsuccessful technique I discovered was to launch with manual AVD selection, then click the (default) OK button on Android Device Chooser dialog (instead of the Start button, which shows the Launch Options dialog).

FINALLY, I read one of Shane's most recent posts very carefully and tried launching from the Android SDK and AVD Manager option on the Window menu. No wiley OK button on that dialog, just the successful Start button!

Now that I'm able to load the emulator from a snapshot, the only piece I'm missing is how to run a new version of my app after making changes. I'm accustomed to using the run button, which tells me that my app is already running (even though I terminated it using a menu item that calls finish()).

Carl said...

Sorry for the back-to-back posts, but I figured out how to run a new copy of my application on a previously loaded emulator just after hitting the blog's Publish button (Murphy's Law).

The top half of the Android Device Chooser dialog (which is seen when launch for the target is set to Manual) shows devices that are already running. When I select the emulator that is already running, my new code runs there just like I wanted it to!

Shane Conder said...

Hi Carl,

Thanks for your comments.

Are you using debug and run configurations? If not, read on.

(I don't ever use run, so this will just talk about debug.)

The debug button has a drop down menu. Using that, there are options for configurations. What you want to do is create a configuration for each app (eclipse project) you're debugging. Then you use the drop down to launch the app. If it's already running, it'll close it, load the new code, and run it again for debugging.

(These aren't exact steps, just the general idea.)

StatiK EffecK said...

It seems that the save/launch snapshot feature has broken with Android SDK Revision 12 ;( I hope they fix it soon. See http://code.google.com/p/android/issues/detail?id=18366

Unknown said...

Workaround for crash when loading from snapshot is listed here:
http://code.google.com/p/android/issues/detail?id=21110

Unknown said...

Hi
I'm new in android development. when I running my application, emulator take a long time to launch. I searched net and found your weblog. I followed your way, changed AVD setting , but after that emulator does not display anything with any error or warning!
What's the reason of?

I'n in ubuntu 11.10
android 2.2
android-sdk-r15
ADT eclipse plugin 15.0.0

Are these versions compatible with each other?
I will appreciate if you help me.
Thanks
Fazileh

Shane Conder said...

Hi Unknown (Fazileh),

The tools have known issues with the snapshot functionality currently. Check out: http://tools.android.com/knownissues

For launching an emulator, I usually plan on it spending 5-10 minutes coming up. Sometimes it's faster, but not always. Once up, though, I do my best to not close it -- that way, I'm just loading new apps without any overhead of launching the emulator.

Unknown said...

Hi Shane Conder,
Thanks for reply, my cpu caused this problem.
my problem solved.
Thank you.

Fazileh

Abhinai said...

my android emulator works very slow takes about 5minutes to load the homescreen. i am using andriod 4.0.3 sdk and emulator avd-4.0.3.
can anyone suggest me how to run the emulator run faster

islamona2008 said...

Thanks very very very much :)
Previously it took 3 minutes but now it takes 5 seconds to run a simple app on my Pentium4 processor with 2GB of RAM :)))))