日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/1/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 KOOM原理分析之一些基础知识 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 資料
  • Profile工具的使用
    • 內(nèi)存性能分析器概覽
    • 內(nèi)存計算方式
    • 查看內(nèi)存分配情況(Record一段)
    • 查看全局JNI引用
    • 原生內(nèi)存性能分析器
    • 將堆轉儲另存為 HPROF 文件
  • HPROF Agent
    • Binary Dump Format(format=b)
    • Handling of Arrays

資料

使用內(nèi)存性能分析器查看應用的內(nèi)存使用情況
HPROF Agent

Profile工具的使用

內(nèi)存性能分析器概覽


① 用于強制執(zhí)行垃圾回收事件的按鈕。
② 用于捕獲堆轉儲的按鈕。
③ 用于指定性能分析器多久捕獲一次內(nèi)存分配的下拉菜單。選擇適當?shù)倪x項可幫助您在進行性能剖析時提高應用性能。
④ 用于縮放時間軸的按鈕。
⑤ 用于跳轉到實時內(nèi)存數(shù)據(jù)的按鈕。
⑥ 事件時間軸,顯示活動狀態(tài)、用戶輸入事件和屏幕旋轉事件。
⑦ 內(nèi)存使用量時間軸,它會顯示以下內(nèi)容:
一個堆疊圖表,顯示每個內(nèi)存類別當前使用多少內(nèi)存,如左側的 y 軸以及頂部的彩色鍵所示。
一條虛線,表示分配的對象數(shù),如右側的 y 軸所示。
每個垃圾回收事件的圖標。

內(nèi)存計算方式


Java:從 Java 或 Kotlin 代碼分配的對象的內(nèi)存。
Native:從 C 或 C++ 代碼分配的對象的內(nèi)存。
Graphics:圖形緩沖區(qū)隊列為向屏幕顯示像素(包括 GL 表面、GL 紋理等等)所使用的內(nèi)存。(請注意,這是與 CPU 共享的內(nèi)存,不是 GPU 專用內(nèi)存。)
Stack:您的應用中的原生堆棧和 Java 堆棧使用的內(nèi)存。這通常與您的應用運行多少線程有關。
Code:您的應用用于處理代碼和資源(如 dex 字節(jié)碼、經(jīng)過優(yōu)化或編譯的 dex 代碼、.so 庫和字體)的內(nèi)存。
Others:您的應用使用的系統(tǒng)不確定如何分類的內(nèi)存。
Allocated:您的應用分配的 Java/Kotlin 對象數(shù)。此數(shù)字沒有計入 C 或 C++ 中分配的對象。

查看內(nèi)存分配情況(Record一段)


可以直接跳轉到文件


選擇檢查的堆:
default heap:當系統(tǒng)未指定堆時。
image heap:系統(tǒng)啟動映像,包含啟動期間預加載的類。此處的分配確保絕不會移動或消失。
zygote heap:寫時復制堆,其中的應用進程是從 Android 系統(tǒng)中派生的。
app heap:您的應用在其中分配內(nèi)存的主堆。
JNI heap:顯示 Java 原生接口 (JNI) 引用被分配和釋放到什么位置的堆。


Arrange by class:根據(jù)類名稱對所有分配進行分組。這是默認值。
Arrange by package:根據(jù)軟件包名稱對所有分配進行分組。
Arrange by callstack:將所有分配分組到其對應的調用堆棧。

查看全局JNI引用

原生內(nèi)存性能分析器

Allocations:在選定時間段內(nèi)通過 malloc() 或 new 運算符分配的對象數(shù)。
Deallocations:在選定時間段內(nèi)通過 free() 或 delete 運算符解除分配的對象數(shù)。
Allocations Size:在選定時間段內(nèi)所有分配的總大小(以字節(jié)為單位)。
Deallocations Size:在選定時間段內(nèi)所有已釋放內(nèi)存的總大小(以字節(jié)為單位)。
Total Count:Allocations 列中的值減去 Deallocations 列中的值所得的結果。
Remaining Size:Allocations Size 列中的值減去 Deallocations Size 列中的值所得的結果。


Allocations:堆中的分配數(shù)。
Native Size:此對象類型使用的原生內(nèi)存總量(以字節(jié)為單位)。只有在使用 Android 7.0 及更高版本時,才會看到此列。
Shallow Size:此對象類型使用的 Java 內(nèi)存總量(以字節(jié)為單位)。
Retained Size:為此類的所有實例而保留的內(nèi)存總大小(以字節(jié)為單位)。


Depth:從任意 GC 根到選定實例的最短跳數(shù)。
Native Size:原生內(nèi)存中此實例的大小。 只有在使用 Android 7.0 及更高版本時,才會看到此列。
Shallow Size:Java 內(nèi)存中此實例的大小。
Retained Size:此實例所支配內(nèi)存的大小(根據(jù)支配項樹)。

將堆轉儲另存為 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
ID
ID for this string
[u1]*
UTF8 characters for string (NOT NULL terminated)

LOAD CLASS
0x02
u4
class serial number (always > 0)
ID
class object ID
u4
stack trace serial number
ID
class name string ID

UNLOAD CLASS
0x03
u4
class serial number

STACK FRAME
0x04
ID
stack frame ID
ID
method name string ID
ID
method signature string ID
ID
source file name string ID
u4
class serial number
u4
> 0
line number
0
no line information available
-1
unknown location
-2
compiled method (Not implemented)
-3
native method (Not implemented)


STACK TRACE
0x05
u4
stack trace serial number
u4
thread serial number
u4
number of frames
[ID]*
series of stack frame ID's

ALLOC SITES
0x06
u2
Bit mask flags:
0x1
incremental vs. complete
0x2
sorted by allocation vs. line
0x4
whether to force GC (Not Implemented)

u4
cutoff ratio (floating point)
u4
total live bytes
u4
total live instances
u8
total bytes allocated
u8
total instances allocated
u4
number of sites that follow:
u1
array indicator: 0 means not an array, non-zero means an array of this type (See Basic Type)
u4
class serial number
u4
stack trace serial number
u4
number of live bytes
u4
number of live instances
u4
number of bytes allocated
u4
number of instances allocated


HEAP SUMMARY
0x07
u4
total live bytes
u4
total live instances
u8
total bytes allocated
u8
total instances allocated

START THREAD
0x0A
u4
thread serial number
ID
thread object ID
u4
stack trace serial number
ID
thread name string ID
ID
thread group name ID
ID
thread parent group name ID

END THREAD
0x0B
u4
thread serial number

HEAP DUMP
or
HEAP DUMP SEGMENT
0x0C
or
0x1C
Contains any number of sub-tags, each begins a u1 field (no order implied here):
ROOT UNKNOWN
0xFF
ID
object ID

ROOT JNI GLOBAL
0x01
ID
object ID
ID
JNI global ref ID

ROOT JNI LOCAL
0x02
ID
object ID
u4
thread serial number
u4
frame number in stack trace (-1 for empty)

ROOT JAVA FRAME
0x03
ID
object ID
u4
thread serial number
u4
frame number in stack trace (-1 for empty)

ROOT NATIVE STACK
0x04
ID
object ID
u4
thread serial number

ROOT STICKY CLASS
0x05
ID
object ID

ROOT THREAD BLOCK
0x06
ID
object ID
u4
thread serial number

ROOT MONITOR USED
0x07
ID
object ID

ROOT THREAD OBJECT
0x08
ID
thread object ID
u4
thread serial number
u4
stack trace serial number

CLASS DUMP
0x20
ID
class object ID
u4
stack trace serial number
ID
super class object ID
ID
class loader object ID
ID
signers object ID
ID
protection domain object ID
ID
reserved
ID
reserved
u4
instance size (in bytes)
u2
size of constant pool and number of records that follow:
u2
constant pool index
u1
type of entry: (See Basic Type)
value
value of entry (u1, u2, u4, or u8 based on type of entry)

u2
Number of static fields:
ID
static field name string ID
u1
type of field: (See Basic Type)
value
value of entry (u1, u2, u4, or u8 based on type of field)

u2
Number of instance fields (not including super class's)
ID
field name string ID
u1
type of field: (See Basic Type)


INSTANCE DUMP
0x21

ID
object ID
u4
stack trace serial number
ID
class object ID
u4
number of bytes that follow
[value]*
instance field values (this class, followed by super class, etc)

OBJECT ARRAY DUMP
0x22

ID
array object ID
u4
stack trace serial number
u4
number of elements
ID
array class object ID
[ID]*
elements

PRIMITIVE ARRAY DUMP
0x23
ID
array object ID
u4
stack trace serial number
u4
number of elements
u1
element type (See Basic Type)
[u1]*
elements (packed array)


HEAP DUMP END
0x2C
Terminates a series of HEAP DUMP SEGMENTS.? Concatenation of HEAP DUMP SEGMENTS equals a HEAP DUMP.
CPU SAMPLES
0x0D
u4
total number of samples
u4
number of traces that follow:
u4
number of samples
u4
stack trace serial number


CONTROL SETTINGS
0x0E
u4
Bit mask flags:
0x1
alloc traces on/off
0x2
cpu sampling on/off

u2
stack trace depth

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
u2
Flags:
u4
cutoff ratio (floating point between 0.0 and 1.0)

CPU SAMPLES
u2
ignored
u4
cutoff ratio (floating point between 0.0 and 1.0)

CONTROL
u2
Sub option:
0x1
Turn alloc traces on
0x2
Turn alloc traces off
0x3
Turn CPU sampling on:
ID
thread object ID (0 for all threads)

0x4
Turn CPU sampling off:
ID
thread object ID (0 for all threads)

0x5
Clear CPU sampling
0x6
Set max stack depth:
u2
New max stack depth



總結

以上是生活随笔為你收集整理的KOOM原理分析之一些基础知识的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。