13

My app uses 75MB memory when the user opens it for the first time.

I have used Android Profiler Tool from Android Studio to examine memory usage of my Android app. When the main screen opens up, the app starts using 75MB memory even though the main activity does not create any object that needs too much memory. No bitmaps or any big arrays etc.

40MB is from "Code" section, and 19MB is from "Native" which we do not load any native library in this Activity. We do load after user opens another Activity though. I am trying to reduce the memory usage and I was wondering how can I reduce from "Code" and "Native" section.

Screenshot from Android Profiler

enter image description here

Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32
cezmi sakar
  • 191
  • 1
  • 9
  • Have you tried using Proguard? – SripadRaj Mar 01 '18 at 09:47
  • yes, I am using ProGuard – cezmi sakar Mar 01 '18 at 10:07
  • I hope you have checked [this](https://developer.android.com/studio/profile/memory-profiler.html) – SripadRaj Mar 01 '18 at 10:15
  • Even if you're not using C++ in your app, you might see some native memory used here because the Android framework uses native memory to handle various tasks on your behalf, such as when handling image assets and other graphics—even though the code you've written is in Java or Kotlin. from https://developer.android.com/studio/profile/memory-profiler.html – Wrobel Mar 07 '18 at 07:29
  • I have read it already, it does not provide much information. – cezmi sakar Mar 08 '18 at 07:23
  • Is it empty activity, no media players etc? Do you importing any libraries such analitycs etc? – Wrobel Mar 08 '18 at 12:56
  • `adb shell dumpsys meminfo ` – rds Mar 08 '18 at 13:42
  • You can check for details(which object use how much memory) and possible memory leak using Profiler. Use "Dump Java Heap" button. If you wish to check for leak use "Export Capture to File" and save with .hprof extension. Now, you can see more details and possible memory leaks. – Thracian Mar 11 '18 at 17:13
  • hi @cezmisakar, one thing I don't understand, why did you awarded the bounty but didn't accept the answer? Didn't it work for you? – lelloman Mar 16 '18 at 15:06
  • @lelloman, I awarded bounty because your answer gave me some perspective and if I did not awarded, bounty would be expired, I did not accept the answer yet because it did not work for me. I am hoping there will be other answers with time. – cezmi sakar Mar 16 '18 at 15:22
  • @cezmisakar I was just curious to know what was the problem, you mean that even using proguard your code-memory is not shrinking? – lelloman Mar 16 '18 at 15:23
  • Sure it is shrinking but not as much as we need. After all of these optimizations, app is still using more than 100 MB (120 MB) and we need to make it less than 100MB when it is in max memory usage. Since "code" is taking too much memory I was wondering if there is a magical solution to reduce memory usage by "code" or "native". – cezmi sakar Mar 16 '18 at 15:30
  • I see, well one thing you could try is different screen resolution. Some of the memory is used for graphics and will probably be proportional to the resolution. Btw I'm asking for feedback because it is useful for me and for anybody who might read this post to have as much info as possible – lelloman Mar 16 '18 at 15:35
  • Actually, we are working with Google on this, they asked us to reduce the memory usage to add our app to the one of their program. So they are testing our app and they report us how much memory is used by our app. Our own tests were more than they reported, after your answer I realized "instant run" was causing the difference. – cezmi sakar Mar 16 '18 at 16:00
  • @cezmisakar , Did you find anything else to reduce code section of the memory ? – AndroidDev Nov 14 '22 at 08:17

1 Answers1

18

About native memory usage:

  1. Android framework can make use of native memory even if you have 0 native code in your app, see "native" here for reference. For instance, I just tried to make a sample project, just one Activity with one Button and native memory usage is 18mb, if I trigger a garbage collection it drops to 8mb though. In order to manually trigger a garbage collection in Android Studio, you can click on the "trash bin" icon on the top left of the memory profiler window. Don't be shy with that button, I usually have to press it many times in a row to see memory usage drop.

  2. Native memory can also be allocated from Java without the need of loading any native library with ByteBuffer.allocateDirect(int).

  3. When you say that you're not loading any native library until next Activity, if you're loading the library statically (within static { }) you are not guaranteed that the library will be actually loaded when the second Activity starts. It could very well happen that it gets loaded before. If you want to check when the library actually gets loaded you could try to add this method to your C code, it should be called when your library is loaded. It's super dirty but, hey, it works. You might log something instead of crashing.

__attribute__((constructor)) void init(void) { int a = *(int *) ((void *) 0); }

About code memory usage, you should reduce your code :)

If you're not doing it already, set minifyEnabled to true in your build type, assuming you're inspecting memory usage with debug build:

    ...
    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    ...

Also, turn off instant run (see "note" here).

lelloman
  • 13,883
  • 5
  • 63
  • 85
  • Is Java + Code + Stack what occupies available heap? https://stackoverflow.com/a/9428660/2075875 – Malachiasz May 29 '20 at 08:56
  • I don't know, I would surprised if it would though, as far as I know, heap, stack and code are different memory spaces. When you create objects they are allocated in the heap (and eventually garbage collected), code should change when classes are loaded by the virtual machine, and the stack is used at runtime to allocate the memory needed to execute code. – lelloman May 29 '20 at 09:41
  • One observation on code section of the memory, if you restart your device and check, it goes down. Not sure on the technical reason. – AndroidDev Sep 14 '22 at 13:01
  • @AndroidDev I think that the VM lazily loads code, if that's the reason you should see the usage going down also after a restarting the process – lelloman Sep 15 '22 at 14:11
  • @lelloman, that's right, code section of memory goes down when you restart the device (or) restart the process. But still not able to figure out how to optimize that part, apk is proguarded and size is around 15MB. – AndroidDev Nov 14 '22 at 08:19