如何使用android studio分析内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24547555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:23:44  来源:igfitidea点击:

How to analyze memory using android studio

androidandroid-studioprofilingheapheap-memory

提问by Prachi

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?

最近从eclipse切换到android studio。如何在android studio中检查应用程序堆和内存分配?在 Eclipse 中,我们有 MAT 工作室中是否有任何东西可以检查堆转储、hprof 文件?

回答by joselufo

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

  1. 首先,您已安装MAT下载)或使用:

    brew cask 安装内存分析器

  2. 在 Android Studio 中打开 Android Device Monitor 或 DDMS。

  3. 选择您的流程“com.example.etc..”

  4. 单击进程列表上方的更新堆。

  5. 在右侧面板中,选择堆选项卡。

  6. 单击原因 GC。

  7. 单击进程列表上方的转储 HPROF 文件。

  8. 当我们下载文件 HPROF 时,我们必须打开终端并运行此命令以生成文件以使用 MAT 打开它。

  9. 打开终端并运行此命令

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

./hprof-conv 路径/file.hprof exitPath/heap-converted.hprof

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

命令“hprof-conv”位于 sdk 的 platform-tools 文件夹中。

  1. And ready and MAT can open and open the converted file ( heap-converted.hprof ) .
  1. 并且ready和MAT可以打开和打开转换后的文件(heap-converted.hprof)。

回答by cVoronin

There is another way to see allocations:

还有另一种查看分配的方法:

Start and stop allocation tracking

开始和停止分配跟踪

See the result

查看结果

回答by Jay

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

Android 监视器 -> 监视器(在 logcat 旁边) -> 内存 -> 转储 java 堆

enter image description here

在此处输入图片说明

回答by Akshay

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.

  1. 单击进程列表上方的更新堆。

  2. 在右侧面板中,选择堆选项卡。

  3. 单击原因 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.

它们不是必需的。只需在 DDMS 中选择您的应用程序/进程,然后单击转储 HPROF 配置文件选项。之后,完全按照上述步骤操作。

回答by Arade

One can also do the following to get more options,

还可以执行以下操作以获得更多选择,

  1. Go to Tools
  2. Then to Android
  3. finally to Android Device Monitor
  1. 转到工具
  2. 然后到安卓
  3. 终于到了Android Device Monitor

回答by Icy Tsai

  1. First install MAT,in order to use it in Android Studio, you shall chooseStand-alone Eclipse RCP Applicationsto 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 .hproffile in Heap Snapshotfolder

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

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

  1. 首先安装MAT,为了在Android Studio中使用,需要选择Stand-alone Eclipse RCP Applications安装,可以作为独立工具使用

  2. 在 Android Studio 中,运行您的应用程序

  3. 在底部面板中,选择6:Android,然后选择Memory Monitor

  4. 点击 Dump Java Heap

  5. 切换到 Captures 选项卡(在左侧面板中),您可以在.hprof文件Heap Snapshot夹中找到该文件

  6. 为了.hprof在 MAT 中打开文件,您应该转换格式:左键单击.hprof文件,然后选择Export to standard .hprof

  7. 打开MAT,然后打开.hprof已经转换格式的文件

回答by dunno

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).

请注意,在最新的 Android Studio (1.3+) 中,堆 (Android hprof) 捕获已移至 Android 选项卡下的内存监视器子选项卡(如 cVoronin 的答案中的第一张图片)。

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+.

捕获完成后,它会自动保存到您项目下的“捕获”目录中(如果您愿意,可以在事后重命名文件)。hprof 文件将在 1.3+ 的新 hprof 查看器中自动打开。

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.

当然,您始终可以选择将其转换为标准 hprof 格式并在 MAT 中查看。只需右键单击 Captures 浏览器中的文件(在项目浏览器下),然后在那里选择转换。和往常一样,如果不使用新的查看器,您会在此过程中丢失一些额外的 Android 特定信息,因为标准 hprof 不支持这些信息。

回答by Christine

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

我从 Eclipse 切换到 Android Studio,但我仍然在 Eclipse 中使用 MAT,并带有 DDMS 插件。这要容易得多。

回答by dankito

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

我知道这是一个很老的问题,但 Android Studio 3 现在有一个内置的分析器。

See documentation here: Android Profiler in Android Studio 3.0

请参阅此处的文档:Android Studio 3.0 中的 Android Profiler

回答by Gv Ravi

you can use android profiler in new android studio.

您可以在新的 android studio 中使用 android profiler。