Why "Code" and "Native" sections in Memory Profiler use too much memory and how can I reduce it?
Asked Answered
I

1

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

Intended answered 1/3, 2018 at 9:45 Comment(15)
Have you tried using Proguard?Nicotinism
yes, I am using ProGuardIntended
I hope you have checked thisNicotinism
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 developer.android.com/studio/profile/memory-profiler.htmlSeldom
I have read it already, it does not provide much information.Intended
Is it empty activity, no media players etc? Do you importing any libraries such analitycs etc?Seldom
adb shell dumpsys meminfo <process-name>Impeditive
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.Chapeau
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?Habitue
@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.Intended
@cezmisakar I was just curious to know what was the problem, you mean that even using proguard your code-memory is not shrinking?Habitue
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".Intended
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 possibleHabitue
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.Intended
@cezmisakar , Did you find anything else to reduce code section of the memory ?Discard
H
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).

Habitue answered 9/3, 2018 at 17:3 Comment(5)
Is Java + Code + Stack what occupies available heap? https://mcmap.net/q/27130/-detect-application-heap-size-in-androidAldwon
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.Habitue
One observation on code section of the memory, if you restart your device and check, it goes down. Not sure on the technical reason.Discard
@Discard 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 processHabitue
@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.Discard

© 2022 - 2024 — McMap. All rights reserved.