How to analyze memory using android studio
Asked Answered
C

10

74

Recently switch to android studio from eclipse. How to check app heap and memory allocation in android studio? In Eclipse we have MAT is there anything in the studio to check heap dump, hprof file?

Covarrubias answered 3/7, 2014 at 7:28 Comment(0)
G
120

I'll explain it in an easy way with steps:

  1. First, you have install MAT ( download ) or use:

    brew cask install memoryanalyzer

  2. In Android Studio open Android Device Monitor or DDMS.

  3. Select your process "com.example.etc.."

  4. Click Update Heap above the process list.

  5. In the right-side panel, select the Heap tab.

  6. Click in Cause GC.

  7. Click Dump HPROF file above the process list.

  8. When we downloaded the file HPROF, we have to open the Terminal and run this command to generate the file to open it with MAT.

  9. Open terminal and run this command

./hprof-conv path/file.hprof exitPath/heap-converted.hprof

The command "hprof-conv" is in the platform-tools folder of the sdk.

  1. And ready and MAT can open and open the converted file ( heap-converted.hprof ) .
Glavin answered 3/7, 2014 at 7:48 Comment(9)
curious as to what's supposed to happen when dumping the hprof; after clicking that option, nothing appears to happen. At what point should there be any indication that it's completed the dump, and where does it put the output? Thanks.Ellinger
@wkhatch, it can take awhile for the dialog to display. You should see a window asking the save location. In case it helps anyone else, I didn't realize that the MAT download link is a standalone download (open mat.app). I thought it was a link to the Eclipse plugin only.Hollenbeck
Thanks @Hollenbeck . I got it working on another try. For some reason, it just hung up the first few times I tried it.Ellinger
In recent Android Studio versions (1.2.x) a heap dump can be quickly generated via: 'Android' (6) tab > 'Memory' sub-tab > "Dump Java Heap" icon: This would create and download an .hprof file to a folder named captures in your project dir. From there, steps 9-10 of @TeRRo's description would complete the job.Pattipattie
@undo thanks for the update!. Feel free to edit the answer if you can if not I will update soon.Glavin
For the thirsty OSXers: brew cask install memoryanalyzer for standalone MATAbsentee
To add to @d4vidi's comment, Android Studio now also opens the hprof file automatically when you take a heap dump.Restrict
@Pattipattie It takes surprisingly long time to Fetch Results by the suggested method. Any clue why?Alexisaley
Getting Error: Cask 'memoryanalyzer' is unavailable: No Cask with this name exists. In osxBackboard
M
40

There is another way to see allocations:

Start and stop allocation tracking

See the result

Margaretemargaretha answered 13/1, 2015 at 10:47 Comment(1)
The button has been moved into the "Memory" tab, now.Invocate
C
13

Android Monitor -> Monitors (beside logcat) -> Memory -> dump java heap

enter image description here

Compare answered 16/3, 2016 at 7:28 Comment(1)
I believe this answer is out of date - There's no Monitor next to Log Cat as far as I can see nowCentenarian
C
3

I agree with above answer except below steps

  1. Click Update Heap above the process list.

  2. In the right-side panel, select the Heap tab.

  3. Click in Cause GC.

They are not necessary.Just select your app/process in DDMS, and click to dump HPROF profile option. After that, follow the steps exactly as above.

Chaworth answered 8/1, 2015 at 0:54 Comment(0)
S
3

One can also do the following to get more options,

  1. Go to Tools
  2. Then to Android
  3. finally to Android Device Monitor
Sinecure answered 6/2, 2015 at 15:47 Comment(0)
C
3
  1. First install MAT,in order to use it in Android Studio, you shall chooseStand-alone Eclipse RCP Applications to install,which can be used as independent tool

  2. In Android Studio , run your application

  3. In the bottom-side panel, select 6:Android,then select Memory Monitor

  4. Click Dump Java Heap

  5. Switch to the Captures tab (in the left-side panel), you can find the .hprof file in Heap Snapshot folder

  6. In order to open the .hprof file in MAT, you shall convert the format: left-click on the .hprof file, then choose Export to standard .hprof

  7. Open MAT, then open the .hprof file that you have converted the format

Chevrotain answered 14/8, 2015 at 3:19 Comment(0)
W
2

Note that in the latest Android Studio (1.3+), the heap (Android hprof) capture has been moved to the Memory Monitor subtab under the Android tab (like the first image in cVoronin's answer).

When the capture is finished, it will be automatically saved to the "captures" directory under your project (you can rename the file after the fact if you wish). The hprof file will automatically be opened up in the new hprof viewer in 1.3+.

Of course, you always have the option to convert it to standard hprof format and view it in MAT. Just right click the file in the Captures browser (under the Project browser) and select convert there. And as usual, you'd lose some additional Android-specific information along the way by not using the new viewer, since standard hprof doesn't support those.

Woodruff answered 25/8, 2015 at 17:19 Comment(0)
O
2

I switched from Eclipse to Android Studio, but I still use MAT in Eclipse, with the DDMS plugin. It's so much easier.

Osmen answered 4/2, 2016 at 10:23 Comment(1)
Since I posted this, Android Studio has greatly improved its analysis tools. I now use AS for analysis.Osmen
M
2

I know this is quite an old question, but Android Studio 3 has now a built in profiler.

See documentation here: Android Profiler in Android Studio 3.0

Marcellemarcellina answered 26/10, 2017 at 13:21 Comment(1)
can you tell me how to use android studio 3 analyzer?Nonplus
P
0

you can use android profiler in new android studio.

Pruinose answered 17/1, 2020 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.