0
点赞
收藏
分享

微信扫一扫

KOOM原理分析之一些基础知识



文章目录

  • 资料
  • Profile工具的使用
  • 内存性能分析器概览
  • 内存计算方式
  • 查看内存分配情况(Record一段)
  • 查看全局JNI引用
  • 原生内存性能分析器
  • 将堆转储另存为 HPROF 文件
  • HPROF Agent
  • Binary Dump Format(format=b)
  • Handling of Arrays


资料

使用内存性能分析器查看应用的内存使用情况 HPROF Agent

Profile工具的使用

内存性能分析器概览

KOOM原理分析之一些基础知识_性能分析


① 用于强制执行垃圾回收事件的按钮。

② 用于捕获堆转储的按钮。

③ 用于指定性能分析器多久捕获一次内存分配的下拉菜单。选择适当的选项可帮助您在进行性能剖析时提高应用性能。

④ 用于缩放时间轴的按钮。

⑤ 用于跳转到实时内存数据的按钮。

⑥ 事件时间轴,显示活动状态、用户输入事件和屏幕旋转事件。

⑦ 内存使用量时间轴,它会显示以下内容:

一个堆叠图表,显示每个内存类别当前使用多少内存,如左侧的 y 轴以及顶部的彩色键所示。

一条虚线,表示分配的对象数,如右侧的 y 轴所示。

每个垃圾回收事件的图标。

内存计算方式

KOOM原理分析之一些基础知识_Profile_02


Java:从 Java 或 Kotlin 代码分配的对象的内存。

Native:从 C 或 C++ 代码分配的对象的内存。

Graphics:图形缓冲区队列为向屏幕显示像素(包括 GL 表面、GL 纹理等等)所使用的内存。(请注意,这是与 CPU 共享的内存,不是 GPU 专用内存。)

Stack:您的应用中的原生堆栈和 Java 堆栈使用的内存。这通常与您的应用运行多少线程有关。

Code:您的应用用于处理代码和资源(如 dex 字节码、经过优化或编译的 dex 代码、.so 库和字体)的内存。

Others:您的应用使用的系统不确定如何分类的内存。

Allocated:您的应用分配的 Java/Kotlin 对象数。此数字没有计入 C 或 C++ 中分配的对象。

查看内存分配情况(Record一段)

KOOM原理分析之一些基础知识_Profile_03

KOOM原理分析之一些基础知识_Hprof_04


可以直接跳转到文件

KOOM原理分析之一些基础知识_sed_05

KOOM原理分析之一些基础知识_Profile_06


选择检查的堆:

default heap:当系统未指定堆时。

image heap:系统启动映像,包含启动期间预加载的类。此处的分配确保绝不会移动或消失。

zygote heap:写时复制堆,其中的应用进程是从 Android 系统中派生的。

app heap:您的应用在其中分配内存的主堆。

JNI heap:显示 Java 原生接口 (JNI) 引用被分配和释放到什么位置的堆。

KOOM原理分析之一些基础知识_Hprof_07


Arrange by class:根据类名称对所有分配进行分组。这是默认值。

Arrange by package:根据软件包名称对所有分配进行分组。

Arrange by callstack:将所有分配分组到其对应的调用堆栈。

查看全局JNI引用

KOOM原理分析之一些基础知识_sed_08

原生内存性能分析器

KOOM原理分析之一些基础知识_sed_09

Allocations:在选定时间段内通过 malloc() 或 new 运算符分配的对象数。
Deallocations:在选定时间段内通过 free() 或 delete 运算符解除分配的对象数。
Allocations Size:在选定时间段内所有分配的总大小(以字节为单位)。
Deallocations Size:在选定时间段内所有已释放内存的总大小(以字节为单位)。
Total Count:Allocations 列中的值减去 Deallocations 列中的值所得的结果。
Remaining Size:Allocations Size 列中的值减去 Deallocations Size 列中的值所得的结果。

KOOM原理分析之一些基础知识_Profile_10


Allocations:堆中的分配数。

Native Size:此对象类型使用的原生内存总量(以字节为单位)。只有在使用 Android 7.0 及更高版本时,才会看到此列。

Shallow Size:此对象类型使用的 Java 内存总量(以字节为单位)。

Retained Size:为此类的所有实例而保留的内存总大小(以字节为单位)。

KOOM原理分析之一些基础知识_Java_11


Depth:从任意 GC 根到选定实例的最短跳数。

Native Size:原生内存中此实例的大小。 只有在使用 Android 7.0 及更高版本时,才会看到此列。

Shallow Size:Java 内存中此实例的大小。

Retained Size:此实例所支配内存的大小(根据支配项树)。

将堆转储另存为 HPROF 文件

hprof-conv heap-original.hprof heap-converted.hprof

HPROF Agent

Binary Dump Format(format=b)

The binary output begins with the information:

[u1]*

An initial NULL terminated series of bytes representing the format name and version, in this implementation and historically, the string "JAVA PROFILE 1.0.1" (18 u1 bytes) followed by a NULL byte. If the TAG "HEAP DUMP SEGMENT" is used this string will be "JAVA PROFILE 1.0.2".

u4

size of identifiers. Identifiers are used to represent UTF8 strings, objects, stack traces, etc. They can have the same size as host pointers or sizeof(void*), but are not required to be.

u4

high word of number of milliseconds since 0:00 GMT, 1/1/70

u4

low word of number of milliseconds since 0:00 GMT, 1/1/70

Followed by a sequence of records that look like:

u1

TAG: denoting the type of the record

u4

TIME: number of microseconds since the time stamp in the header

u4

LENGTH: number of bytes that follow this u4 field and belong to this record

[u1]*

BODY: as many bytes as specified in the above u4 field

The following TAGs are supported:

STRING IN UTF8

0x01


LOAD CLASS

0x02


UNLOAD CLASS

0x03


STACK FRAME

0x04


STACK TRACE

0x05


ALLOC SITES

0x06


HEAP SUMMARY

0x07


START THREAD

0x0A


END THREAD

0x0B


HEAP DUMP

or

HEAP DUMP SEGMENT

0x0C

or

0x1C

Contains any number of sub-tags, each begins a u1 field (no order implied here):


HEAP DUMP END

0x2C

Terminates a series of HEAP DUMP SEGMENTS.  Concatenation of HEAP DUMP SEGMENTS equals a HEAP DUMP.

CPU SAMPLES

0x0D


CONTROL SETTINGS

0x0E


ID

ID for this string

[u1]*

UTF8 characters for string (NOT NULL terminated)

Basic Type

2

object

4

boolean

5

char

6

float

7

double

8

byte

9

short

10

int

11

long

Handling of Arrays

Commands can be sent to HPROF via the socket connection, the accepted COMMAND TAGS are:

FORCE GC (Not implemented)

0x01

DUMP HEAP

0x02

ALLOC SITES

0x03

HEAP SUMMARY

0x04

EXIT THE VM

0x05

DUMP TRACES

0x06

CPU SAMPLES

0x07

CONTROL

0x08

EOF (used to terminate socket connection)

0xFF

The commands take the form:

u1

COMMAND TAG

u4

serial number

u4

number of bytes that follow

[u1]*

ALLOC SITES

u2Flags:u4cutoff ratio (floating point between 0.0 and 1.0)

CPU SAMPLES

u2ignoredu4cutoff ratio (floating point between 0.0 and 1.0)

CONTROL



举报

相关推荐

0 条评论