Skip to content

Instantly share code, notes, and snippets.

Avatar

Pavlo Stavytskyi Morfly

View GitHub Profile
View GettingUss.kt
import android.os.Debug.MemoryInfo
val memoryInfo: MemoryInfo = ...
val ussKb = with(memoryInfo) {
getTotalPrivateClean() + getTotalPrivateDirty()
}
View GettingPss.kt
import android.os.Debug
import android.os.Debug.MemoryInfo
val memoryInfo = MemoryInfo()
Debug.getMemoryInfo(memoryInfo)
val summary: Map<String, String> = memoryInfo.getMemoryStats()
View GettingPssSimple.kt
import android.os.Debug
val pssKb: Long = Debug.getPss()
View memory_stats_example.txt
code: 12128 kB
stack: 496 kB
graphics: 996 kB
java-heap: 8160 kB
native-heap: 4516 kB
private-other: 2720 kB
system: 4955 kB // Includes all shared memory
total-pss: 33971 kB // A sum of everything except 'total-swap'
total-swap: 17520 kB
View GettingPssTimeLimited.kt
import android.os.Debug.MemoryInfo
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val pid = intArrayOf(android.os.Process.myPid())
// The sample rate for calling this API is limited to once per 5 minutes.
// If called more frequently, it returns the same data as pverious call.
val memoryInfo: MemoryInfo = activityManager.getProcessMemoryInfo(pid).first()
View JvmHeapAllocated.kt
val totalMemoryKb = Runtime.getRuntime().totalMemory() / 1024
val freeMemoryKb = Runtime.getRuntime().freeMemory() / 1024
val jvmHeapAllocatedKb = totalMemoryKb - freeMemoryKb
View NativeHeapAllocated.kt
import android.os.Debug
val nativeHeapAllocatedKb = Debug.getNativeHeapAllocatedSize() / 1024
View 1.movie-search.impl.MovieSearchEntryImpl.kt
class MovieSearchEntryImpl @Inject constructor() : MovieSearchEntry() {
@Composable
override fun Composable(...) {
val dataProvider = LocalDataProvider.current
val viewModel = injectedViewModel {
DaggerMovieSearchComponent.builder()
.dataProvider(dataProvider)
.build()
.viewModel
View 1.data.api.DataProvider.kt
interface DataProvider { ... }
val LocalDataProvider = compositionLocalOf<DataProvider> {
error("No data provider found!")
}
View movie-search.api.MovieSearchEntry.kt
abstract class MovieSearchEntry : FeatureEntry {
final override val featureRoute = "movie-search"
}