How does Android OS calculate memory usage in an application?
I mean the memory usage of the application displayed in the menu "Settings"> "Application"> "Launch". Why is this number so different from application heap size in DDMS? For example, in the picture, OS says that my application uses 151 MB (including the Google PLAY service), but DDMS says that only 40 MB of memory is used.
And the second question: Google Analytics used 27 MB, is it just for my application or for all applications using the Google Analytics service? If this is for my application, then it is expensive!

Android Developer Links
Process statistics: understanding how your application uses RAM -> http://android-developers.blogspot.com/2014/01/process-stats-understanding-how-your.html
Managing application memory -> https://developer.android.com/training/articles/memory.html
Researching the use of your RAM → https://developer.android.com/tools/debugging/debugging-memory.html
Android 4.4 KitKat introduced a new system service called procstats , which will help you better understand how your application uses RAM resources on the device.
You can access procstats data using the adb shell command, but for convenience there is also a new Stats Stats developer tool that provides a graphical interface for the same data. You can find process statistics in Settings > Developer options > Process Stats .

Looking at system memory usage and background processes
When you open process statistics, you see a summary of system-wide memory conditions and detailed information on how processes use memory over time. The image on the right gives you an example of what you can see on a typical device.
Memory analysis for specific processes


Process statistics memory details for Clock and Keyboard processes over the past 3.5 hours.
The details of these two processes show that:
- The reason Clock works in general is because it is used as the current screen saver when the device is in standby mode.
- Despite the fact that the Clock process lasted less than half the time the keyboard worked, its use of the plunger was significantly larger (almost 3 times), so its overall weight is greater.
Essentially, procstats provides a “memory usage indicator” that is very similar to using data storage or data usage, showing how much RAM is used by applications running in the background. Unlike storage or data, memory usage is much harder to quantify and measure, and procstats uses some tricks to do this. To illustrate the complexity of using memory, consider a related topic: task managers.
RAM snapshot
Total PSS by OOM adjustment: 31841 kB: Native 13173 kB: zygote (pid 23001) 4372 kB: surfaceflinger (pid 23000) 3721 kB: mediaserver (pid 126) 3317 kB: glgps (pid 22993) 1656 kB: drmserver (pid 125) 995 kB: wpa_supplicant (pid 23148) 786 kB: netd (pid 121) 518 kB: sdcard (pid 132) 475 kB: vold (pid 119) 458 kB: keystore (pid 128) 448 kB: /init (pid 1) 412 kB: adbd (pid 134) 254 kB: ueventd (pid 108) 238 kB: dhcpcd (pid 10617) 229 kB: tf_daemon (pid 130) 200 kB: installd (pid 127) 185 kB: dumpsys (pid 14207) 144 kB: healthd (pid 117) 139 kB: debuggerd (pid 122) 121 kB: servicemanager (pid 118) 48217 kB: System 48217 kB: system (pid 23064) 49095 kB: Persistent 34012 kB: com.android.systemui (pid 23163 / activities) 7719 kB: com.android.phone (pid 23357) 4676 kB: com.android.nfc (pid 23371) 2688 kB: com.nuance.xt9.input (pid 23344) 24945 kB: Foreground 24945 kB: com.android.settings (pid 24811 / activities) 17136 kB: Visible 14026 kB: com.google.process.location (pid 23472) 3110 kB: com.android.defcontainer (pid 13976) 6911 kB: Perceptible 6911 kB: com.google.android.inputmethod.latin (pid 23298) 14277 kB: A Services 14277 kB: com.google.process.gapps (pid 23513) 26422 kB: Home 26422 kB: com.android.launcher (pid 23395 / activities) 21798 kB: B Services 16242 kB: com.google.android.apps.currents (pid 23767) 5556 kB: android.process.media (pid 7738) 145869 kB: Cached 41588 kB: com.google.android.apps.plus (pid 24689) 21417 kB: com.google.android.deskclock (pid 23966 / activities) 14463 kB: com.google.android.apps.docs (pid 8644) 14303 kB: com.google.android.gm (pid 9115) 11014 kB: com.google.android.music:main (pid 7716) 10688 kB: com.google.android.apps.magazines (pid 13892) 10240 kB: com.google.android.gms (pid 23338) 9882 kB: com.google.android.youtube (pid 5131) 8807 kB: com.google.android.apps.walletnfcrel (pid 8937) 3467 kB: com.google.android.setupwizard (pid 8922) Total RAM: 998096 kB Free RAM: 574945 kB (145869 cached pss + 393200 cached + 35876 free) Used RAM: 392334 kB (240642 used pss + 107196 buffers + 3856 shmem + 40640 slab) Lost RAM: 30817 kB Tuning: 64 (large 384), oom 122880 kB, restore limit 40960 kB (high-end-gfx)