Skip to content

Commit b978cf1

Browse files
committed
dominators
1 parent b653011 commit b978cf1

8 files changed

+389
-117
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies {
2121
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
2222
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
2323
implementation(compose.desktop.currentOs)
24-
implementation("com.squareup.leakcanary:shark:2.5")
24+
implementation("com.squareup.leakcanary:shark-android:2.6")
2525
implementation("com.squareup.okio:okio:2.9.0")
2626
implementation("com.apurebase:kgraphql:0.16.0")
2727
}

gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
kotlin.code.style=official
2+
org.gradle.jvmargs=-Xmx4096m

src/main/kotlin/shark/HeapDumpLoadingState.kt

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,19 @@ package shark
22

33
import androidx.compose.runtime.MutableState
44
import androidx.compose.runtime.mutableStateOf
5-
import androidx.compose.runtime.remember
6-
import shark.HprofHeapGraph.Companion.openHeapGraph
75
import java.io.File
86
import java.util.concurrent.ExecutorService
97

108
class HeapDumpLoadingState(val file: File, val ioExecutor: ExecutorService) {
11-
val loadedGraph: MutableState<CloseableHeapGraph?> = mutableStateOf(null)
9+
val loadedGraph: MutableState<LoadedGraph?> = mutableStateOf(null)
1210

1311
fun load() {
1412
if (loadedGraph.value != null) {
1513
return
1614
}
1715
println("Opening ${file.path}")
1816
ioExecutor.execute {
19-
val fileSourceProvider = FileSourceProvider(file)
20-
val wrapper = object : DualSourceProvider by fileSourceProvider{
21-
override fun openRandomAccessSource(): RandomAccessSource {
22-
val realSource = fileSourceProvider.openRandomAccessSource()
23-
return object : RandomAccessSource by realSource {
24-
override fun read(sink: okio.Buffer, position: Long, byteCount: Long): Long {
25-
println("IO from thread ${Thread.currentThread().name}")
26-
return realSource.read(sink, position, byteCount)
27-
}
28-
}
29-
}
30-
}
31-
loadedGraph.value = wrapper.openHeapGraph()
17+
loadedGraph.value = LoadedGraph.load(file)
3218
}
3319
}
3420
}

src/main/kotlin/shark/HeapGraphWindow.kt

+24-19
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ import androidx.compose.material.MaterialTheme
1212
import androidx.compose.material.Text
1313
import androidx.compose.runtime.Composable
1414
import androidx.compose.runtime.getValue
15-
import androidx.compose.runtime.key
1615
import androidx.compose.runtime.mutableStateOf
1716
import androidx.compose.runtime.remember
1817
import androidx.compose.runtime.setValue
1918
import androidx.compose.ui.Alignment
2019
import androidx.compose.ui.Modifier
2120
import androidx.compose.ui.graphics.Color
2221
import androidx.compose.ui.unit.dp
23-
import shark.HeapObject.HeapClass
2422
import shark.Showing.ShowTree
2523
import shark.Showing.Start
2624

@@ -40,17 +38,10 @@ sealed class Showing {
4038
@Composable
4139
fun HeapGraphWindow(loadingState: HeapDumpLoadingState, pressedKeys: PressedKeys) {
4240
MaterialTheme {
43-
val graph = loadingState.loadedGraph.value
44-
if (graph == null) {
41+
val loadedGraph = loadingState.loadedGraph.value
42+
if (loadedGraph == null) {
4543
LoadingScreen(loadingState.file.name)
4644
} else {
47-
// TODO move
48-
val classesWithInstanceCounts = mutableMapOf<Long, Int>()
49-
classesWithInstanceCounts.putAll(graph.classes.map { it.objectId to 0 })
50-
graph.instances.forEach { instance ->
51-
classesWithInstanceCounts[instance.instanceClassId] =
52-
classesWithInstanceCounts[instance.instanceClassId]!! + 1
53-
}
5445

5546
// TODO Backstack doesn't want to have duplicate entries (ie that are equal) but recents
5647
// allow that to happen. Need to see what's what.
@@ -60,8 +51,7 @@ fun HeapGraphWindow(loadingState: HeapDumpLoadingState, pressedKeys: PressedKeys
6051

6152
Backstack(backstack) { screen ->
6253
HeapGraphScreen(
63-
graph,
64-
classesWithInstanceCounts,
54+
loadedGraph,
6555
pressedKeys,
6656
screen,
6757
canGoBack = backstack.size > 1,
@@ -83,8 +73,7 @@ fun HeapGraphWindow(loadingState: HeapDumpLoadingState, pressedKeys: PressedKeys
8373

8474
@Composable
8575
fun HeapGraphScreen(
86-
graph: HeapGraph,
87-
classesWithInstanceCounts: Map<Long, Int>,
76+
graph: LoadedGraph,
8877
pressedKeys: PressedKeys,
8978
showing: Showing,
9079
canGoBack: Boolean,
@@ -101,19 +90,35 @@ fun HeapGraphScreen(
10190
}
10291
}
10392
Text(text = "Home")
104-
// TODO Don't use trailing lambda syntax
10593
WrapTextBox("Tree", onClick = {
10694
val showTree =
10795
ShowTree(
10896
"List of classes",
10997
graph.classes.map {
110-
val instanceCount = classesWithInstanceCounts[it.objectId]!!
111-
it.toTreeItem(instanceCount)
98+
it.toTreeItem(graph.instanceCount(it))
11299
}
113100
.toList()
114101
)
115102
goTo(showTree)
116103
})
104+
WrapTextBox("Leaks", onClick = {
105+
goTo(ShowTree(
106+
"Leaking objects",
107+
graph.leakingObjectIds.map { graph.findObjectById(it).toTreeItem(graph) }.toList()
108+
))
109+
})
110+
WrapTextBox("All objects", onClick = {
111+
goTo(ShowTree(
112+
"All objects",
113+
graph.objects.map { it.toTreeItem(graph) }.toList()
114+
))
115+
})
116+
WrapTextBox("Dominators", onClick = {
117+
goTo(ShowTree(
118+
"Dominators",
119+
graph.dominatorsSortedRetained().filter { it != 0L }.map { graph.findObjectById(it).toTreeItem(graph) }
120+
))
121+
})
117122
Text(text = "Recents")
118123
for (item in recents.drop(1)) {
119124
TextBox("$item", onClick = {
@@ -140,7 +145,7 @@ fun HeapGraphScreen(
140145
pressedKeys = pressedKeys,
141146
rootItems = showing.initialItems,
142147
expandItem = { heapItem ->
143-
heapItem.expand(graph, classesWithInstanceCounts)
148+
heapItem.expand(graph)
144149
},
145150
onDoubleClick = { selectedItems ->
146151
val showTree = ShowTree(

0 commit comments

Comments
 (0)