2

I'm using Android Studio Profiler to check and optimize memory consumption. to do some tests, I have disabled every section in the app except for the main activity. so when the app launches, there is only a blank activity which does not do anything and does not hold any view. then when I run the profiler, it shows about 100 MB of memory is being used by my app, half of which is for native code.

Moreover, when I dump the heap, it shows only 6.3 MB of retained size. these numbers confuse me! how and why such large amounts of memory (100 MB) are being used while my activity is empty and not doing any task?

enter image description here enter image description here

Soheil
  • 587
  • 4
  • 20

2 Answers2

2

The screenshot shows the app heap is about 6.3 MB. There may be other heaps (clicking the "View app heap" menu). Those heaps combined are what the JVM is using, which should be consistent with the "Java" category from the profiler's timeline.

To find out what native memory is used for, it may be helpful to try Android Studio's native memory profiler during app startup.

When the profiler is used with a debuggable process, it will do things behind the scene, such as attaching a JVMTI agent. Those operations will occupy native memory. To eliminate those noise, please consider using a profileable build. Here is the instruction to build a profileable app, and you need Android Studio Bumblebee to view profileable processes in the profiler.

Shukang Z
  • 101
  • 3
-5

Here are some reasons why your app might be using more memory than expected, even with an empty activity:

Base Memory Overhead: Even an empty Android app has some baseline memory overhead due to the Android operating system and the necessary runtime components. This baseline memory usage can vary based on the Android version and device specifications.

Native Libraries: The native code you mentioned in the profiler might be libraries or components that are being loaded by your app. Some libraries, especially those used in third-party SDKs, can consume significant amounts of memory.

Resources and Assets: While your activity might not have any views or specific tasks, your app could still be loading resources and assets like images, fonts, and other files. These resources are loaded into memory when the app starts.

Java/Kotlin Objects: The Java/Kotlin runtime and the Android framework itself create objects and data structures behind the scenes to manage the app's lifecycle, UI components, and other functionalities. These objects consume memory, even in an empty activity.

Garbage Collection: The garbage collector in Android might not have run yet when you checked the memory usage, or it might not have released all the unused memory at that moment. Garbage collection is a non-deterministic process, and it might happen at different times during app execution.

Instruments Overhead: The Android Profiler itself may introduce some overhead while measuring memory usage, which can impact the reported numbers.

Caching and Optimization: Android OS and various components use caching and optimization techniques to improve performance. This can temporarily increase memory usage.