0

using the getRuntimeMemoryUsage() to get memory usage from Runtime.getRuntime()

but when compare with the profiler couldn't find the match.

i.e. the profiler memory's java shows 22MB but the code getRuntimeMemoryUsage() return 12MB.

How to get the memory usage as what is shown in the profiler?

fun getRuntimeMemoryUsage(): Long {

        //calculate memory usage of currently running application:

        var freeSize = 0L
        var totalSize = 0L
        var usedSize = 0L
        try {
            val runtime = Runtime.getRuntime()
            runtime.gc()
            freeSize = runtime.freeMemory() / 1048576L
            totalSize = runtime.totalMemory() / 1048576L
            usedSize = (totalSize - freeSize)

            val maxHeapSizeInMB = runtime.maxMemory() / 1048576L

        } catch (e: Exception) {
            e.printStackTrace()
        }

        return usedSize
    }
lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

0

The android studio profilers use a few different methods to capture the memory of an application. The most common method is to poll "dumpsys meminfo". However this is not something that is feasible for an app that wants to be released in the playstore.

  • Please either answer the question with a possible solution or be more clear that there is no solution. – apena Jul 22 '20 at 20:27