xref: /aosp_15_r20/external/leakcanary2/shark-graph/src/main/java/shark/HeapGraph.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1 package shark
2 
3 import shark.HeapObject.HeapClass
4 import shark.HeapObject.HeapInstance
5 import shark.HeapObject.HeapObjectArray
6 import shark.HeapObject.HeapPrimitiveArray
7 
8 /**
9  * Enables navigation through the heap graph of objects.
10  */
11 interface HeapGraph {
12   val identifierByteSize: Int
13 
14   /**
15    * In memory store that can be used to store objects this [HeapGraph] instance.
16    */
17   val context: GraphContext
18 
19   val objectCount: Int
20 
21   val classCount: Int
22 
23   val instanceCount: Int
24 
25   val objectArrayCount: Int
26 
27   val primitiveArrayCount: Int
28 
29   /**
30    * All GC roots which type matches types known to this heap graph and which point to non null
31    * references. You can retrieve the object that a GC Root points to by calling [findObjectById]
32    * with [GcRoot.id], however you need to first check that [objectExists] returns true because
33    * GC roots can point to objects that don't exist in the heap dump.
34    */
35   val gcRoots: List<GcRoot>
36 
37   /**
38    * Sequence of all objects in the heap dump.
39    *
40    * This sequence does not trigger any IO reads.
41    */
42   val objects: Sequence<HeapObject>
43 
44   /**
45    * Sequence of all classes in the heap dump.
46    *
47    * This sequence does not trigger any IO reads.
48    */
49   val classes: Sequence<HeapClass>
50 
51   /**
52    * Sequence of all instances in the heap dump.
53    *
54    * This sequence does not trigger any IO reads.
55    */
56   val instances: Sequence<HeapInstance>
57 
58   /**
59    * Sequence of all object arrays in the heap dump.
60    *
61    * This sequence does not trigger any IO reads.
62    */
63   val objectArrays: Sequence<HeapObjectArray>
64 
65   /**
66    * Sequence of all primitive arrays in the heap dump.
67    *
68    * This sequence does not trigger any IO reads.
69    */
70   val primitiveArrays: Sequence<HeapPrimitiveArray>
71 
72   /**
73    * Returns the [HeapObject] corresponding to the provided [objectId], and throws
74    * [IllegalArgumentException] otherwise.
75    */
76   @Throws(IllegalArgumentException::class)
findObjectByIdnull77   fun findObjectById(objectId: Long): HeapObject
78 
79   /**
80    * Returns the [HeapObject] corresponding to the provided [objectIndex], and throws
81    * [IllegalArgumentException] if [objectIndex] is less than 0 or more than [objectCount] - 1.
82    */
83   @Throws(IllegalArgumentException::class)
84   fun findObjectByIndex(objectIndex: Int): HeapObject
85 
86   /**
87    * Returns the [HeapObject] corresponding to the provided [objectId] or null if it cannot be
88    * found.
89    */
90   fun findObjectByIdOrNull(objectId: Long): HeapObject?
91 
92   /**
93    * Returns the [HeapClass] corresponding to the provided [className], or null if the
94    * class cannot be found.
95    */
96   fun findClassByName(className: String): HeapClass?
97 
98   /**
99    * Returns true if the provided [objectId] exists in the heap dump.
100    */
101   fun objectExists(objectId: Long): Boolean
102 
103   /**
104    * Returns the 1-based index in the heap dump of the object corresponding to the provided
105    * [objectId], and throws [IllegalArgumentException] otherwise.
106    *
107    * This is the object index that YourKit provides in its Java profiler.
108    */
109   fun findHeapDumpIndex(objectId: Long): Int
110 
111   /**
112    * Returns the [HeapObject] corresponding to the provided [heapDumpIndex], and throws
113    * [IllegalArgumentException] if [heapDumpIndex] is less than 1 or more than [objectCount].
114    *
115    * This is the object index that YourKit provides in its Java profiler.
116    */
117   fun findObjectByHeapDumpIndex(heapDumpIndex: Int): HeapObject
118 }
119