Skip to content

Instantly share code, notes, and snippets.

@sdetilly
Created November 12, 2025 18:09
Show Gist options
  • Select an option

  • Save sdetilly/015f976a28faf5b533816a4411dafadf to your computer and use it in GitHub Desktop.

Select an option

Save sdetilly/015f976a28faf5b533816a4411dafadf to your computer and use it in GitHub Desktop.
Use this to test the memory profiler on android studio using either fetchData or fetchData2
import android.os.Bundle
import android.os.Debug
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.SystemBarStyle
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.ui.graphics.toArgb
import androidx.compose.runtime.*
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.tillylabs.androiddemo.ui.theme.AndroidDemoTheme
import kotlinx.coroutines.delay
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.light(
scrim = Color(0xFF1C1C1E).toArgb(),
darkScrim = Color(0xFF1C1C1E).toArgb()
),
navigationBarStyle = SystemBarStyle.dark(Color(0xFF1C1C1E).toArgb())
)
setContent {
AndroidDemoTheme {
DemoApp()
}
}
}
}
@Composable
fun DemoApp() {
var gcCount by remember { mutableLongStateOf(0L) }
var gcTime by remember { mutableLongStateOf(0L) }
var lastGcCount by remember { mutableLongStateOf(0L) }
var gcEventsSinceLastCheck by remember { mutableIntStateOf(0) }
LaunchedEffect(Unit) {
while (true) {
val currentGcCount = Debug.getRuntimeStat("art.gc.gc-count").toLongOrNull() ?: 0
val currentGcTime = Debug.getRuntimeStat("art.gc.gc-time").toLongOrNull() ?: 0
if (currentGcCount > lastGcCount) {
gcEventsSinceLastCheck = (currentGcCount - lastGcCount).toInt()
Log.d("GC_MONITOR", "GC Event! Total: $currentGcCount, New events: $gcEventsSinceLastCheck")
}
gcCount = currentGcCount
gcTime = currentGcTime
lastGcCount = currentGcCount
delay(100) // Check every 100ms
}
}
Box(
modifier = Modifier
.fillMaxSize()
) {
Column(
modifier = Modifier
.align(Alignment.Center)
.padding(16.dp)
) {
Text(
text = """
GC Count: $gcCount
GC Time: ${gcTime}ms
""".trimIndent(),
)
Button(
onClick = {
fetchData2()
}
) {
Text("Fetch Data")
}
}
}
}
// Bad!
private fun fetchData() {
var string = ""
repeat(50000) {
string += "a"
}
println("String value: $string")
}
// Better!
private fun fetchData2() {
val stringBuilder = StringBuilder()
repeat(50000) {
stringBuilder.append("a")
}
println("String value: $stringBuilder")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment