Tuning android for low ram devices

3,009 views
2,685 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,009
On SlideShare
0
From Embeds
0
Number of Embeds
791
Actions
Shares
0
Downloads
26
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Tuning android for low ram devices

  1. 1. Memory Tuning Android for Low-RAM Devices Chris Kühl & Iago López Galeiras, Endocode AG
  2. 2. What's low-RAM? < 512MB
  3. 3. 512MB? That's so 2012! Why do that to yourself? You have no choice ;) Mod-maintainer for older device Android on embedded devices Virtualized Android, anyone?
  4. 4. What to tune? Apps Dalvik VM Activity Manager Linux kernel
  5. 5. Apps Be a good citizen Apps that manage their own memory well make the whole system run better
  6. 6. Keep Your Apps Trim Google says, You should implement onTrimMemory(int) to incrementally release memory based on current system constraints.
  7. 7. 7 Trim Levels Running Cached TRIM_MEMORY_RUNNING_MODERATE TRIM_MEMORY_BACKGROUND TRIM_MEMORY_RUNNING_LOW TRIM_MEMORY_MODERATE TRIM_MEMORY_RUNNING_CRITICAL TRIM_MEMORY_COMPLETE ...and TRIM_MEMORY_UI_HIDDEN TRIM_MEMORY_COMPLETE ≈ onLowMemory
  8. 8. Dalvik VM Android Java VM DEX format Zygote
  9. 9. Dalvik is chatty Dalvik's logcat output can paint a pretty good picture. 12-3015:35:43.135:D/dalvikvm(15368):GC_EXPLICITfreed39K,66%free2349K/6788K,p aused24ms+8ms,total129ms 12-3015:35:43.682:D/dalvikvm(15368):GC_CONCURRENTfreed282K,5%free10382K/10823 K,paused5ms+4ms 12-3015:35:44.043:D/dalvikvm(15368):GC_CONCURRENTfreed319K,5%free10507K/10951 K,paused3ms+3ms 12-3015:35:44.723:D/dalvikvm(15368):GC_CONCURRENTfreed364K,5%free10568K/11079 K,paused3ms+4ms 12-3015:35:44.803:D/dalvikvm(15368):GC_FOR_ALLOCfreed312K,6%free10549K/11207K ,paused31ms 12-3015:35:45.034:D/dalvikvm(15368):GC_FOR_ALLOCfreed316K,6%free10542K/11207K ,paused48ms 12-3015:35:45.074:D/dalvikvm(15368):GC_FOR_ALLOCfreed1K,6%free10631K/11207K, paused43ms
  10. 10. Dalvik Properties Generally set these in PRODUCT_PROPERTY_OVERRIDES. PRODUCT_PROPERTY_OVERRIDES:= dalvik.vm.heapstartsize=5m dalvik.vm.heapgrowthlimit=64m dalvik.vm.heapsize=128m dalvik.vm.heaptargetutilization=0.80 dalvik.vm.heapminfree=512k dalvik.vm.heapmaxfree=2m
  11. 11. Dalvik VM - JIT Google says... For the really low-memory devices, we recommend the JIT be disabled entirely. Can save up to 1.5MB per process!
  12. 12. ActivityManager Managing activities since 2008
  13. 13. ActivityManager & Memory management Continually ranks apps based on their importance to the user.
  14. 14. Adjustments staticfinalintHIDDEN_APP_MAX_ADJ=15; staticintHIDDEN_APP_MIN_ADJ=9; staticfinalintSERVICE_B_ADJ=8; staticfinalintPREVIOUS_APP_ADJ=7; staticfinalintHOME_APP_ADJ=6; staticfinalintSERVICE_ADJ=5; staticfinalintBACKUP_APP_ADJ=4; staticfinalintHEAVY_WEIGHT_APP_ADJ=3; staticfinalintPERCEPTIBLE_APP_ADJ=2; staticfinalintVISIBLE_APP_ADJ=1; staticfinalintFOREGROUND_APP_ADJ=0; staticfinalintPERSISTENT_PROC_ADJ=-12; staticfinalintSYSTEM_ADJ=-16;
  15. 15. lowmemorykiller A kernel module to preempt the oom and provide an interface for user-space to prioritize apps cat/sys/module/lowmemorykiller/parameters/adj 0,1,2,4,9,15 cat/sys/module/lowmemorykiller/parameters/minfree 3031,4218,5406,7454,8642,10158
  16. 16. Configuring lowmemorykiller privatefinalint[]mOomAdj=newint[]{ FOREGROUND_APP_ADJ,VISIBLE_APP_ADJ,PERCEPTIBLE_APP_ADJ, BACKUP_APP_ADJ,HIDDEN_APP_MIN_ADJ,HIDDEN_APP_MAX_ADJ }; privatefinallong[]mOomMinFreeLow=newlong[]{ 8192,12288,16384, 24576,28672,32768 }; privatefinallong[]mOomMinFreeHigh=newlong[]{ 32768,40960,49152, 57344,65536,81920 };
  17. 17. dumpsys meminfo TotalPSSbyOOMadjustment: 18642kB:System 18642kB:system(pid451) 13979kB:Persistent 9884kB:com.android.systemui(pid1791) 4095kB:com.android.phone(pid689) 18349kB:Foreground 18349kB:com.android.launcher(pid3106) 18895kB:Visible 9498kB:com.google.process.gapps(pid735) 7078kB:com.google.process.location(pid667) 2319kB:com.android.smspush(pid795) 8222kB:Perceptible 5623kB:com.android.inputmethod.latin(pid646) 2599kB:com.android.location.fused(pid676) 2810kB:AServices 2810kB:de.telekom.droidsync(pid3117) 45844kB:Background 14552kB:android.process.acore(pid3133) 5566kB:com.google.android.syncadapters.calendar(pid3270) 5475kB:com.google.android.apps.genie.geniewidget(pid3185) 4940kB:com.google.android.gsf.login(pid3254) 4362kB:com.android.browser(pid3359) 3898kB:com.android.calendar(pid3305) 3691kB:com.android.providers.calendar(pid3286) 3360kB:com.cyanogenmod.lockclock(pid3327)
  18. 18. dumpsys activity oom (1/3) OOMlevels: SYSTEM_ADJ:-16 PERSISTENT_PROC_ADJ:-12 FOREGROUND_APP_ADJ:0 VISIBLE_APP_ADJ:1 PERCEPTIBLE_APP_ADJ:2 HEAVY_WEIGHT_APP_ADJ:3 BACKUP_APP_ADJ:4 SERVICE_ADJ:5 HOME_APP_ADJ:6 PREVIOUS_APP_ADJ:7 SERVICE_B_ADJ:8 HIDDEN_APP_MIN_ADJ:10 HIDDEN_APP_MAX_ADJ:15
  19. 19. dumpsys activity oom (2/3) ProcessOOMcontrol: PERS#7:adj=sys /F trm=15451:system/1000(fixed) oom:max=-16hidden=10client=10empty=10curRaw=-16setRaw=-16cur=-16set=-16 keeping=truehidden=falseempty=falsehasAboveClient=false PERS#10:adj=pers/F trm=15689:com.android.phone/1001(fixed) oom:max=-12hidden=10client=10empty=10curRaw=-12setRaw=-12cur=-12set=-12 keeping=truehidden=falseempty=falsehasAboveClient=false PERS#3:adj=pers/F trm=151791:com.android.systemui/u0a10030(fixed) oom:max=-12hidden=10client=10empty=10curRaw=-12setRaw=-12cur=-12set=-12 keeping=truehidden=falseempty=falsehasAboveClient=false Proc#0:adj=fore/FAtrm=153106:com.android.launcher/u0a10022(top-activity) oom:max=15hidden=10client=10empty=10curRaw=0setRaw=0cur=0set=0 keeping=truehidden=falseempty=falsehasAboveClient=false Proc#9:adj=vis /F trm=15795:com.android.smspush/u0a10032(service) com.android.smspush/.WapPushManager<=Proc{689:com.android.phone/1001} oom:max=15hidden=10client=10empty=10curRaw=1setRaw=1cur=1set=1 keeping=truehidden=falseempty=truehasAboveClient=false
  20. 20. dumpsys activity oom (3/3) ProcessesthatarewaitingtoGC: ProcessProcessRecord{422a4260735:com.google.process.gapps/u0a10033} lowMem=true,lastgced=3110616msago,lastlowMem=18198msago ProcessProcessRecord{422f13b0451:system/1000} lowMem=true,lastgced=3110616msago,lastlowMem=18198msago ProcessProcessRecord{4247c388667:com.google.process.location/u0a10033} lowMem=true,lastgced=3110616msago,lastlowMem=18198msago ProcessProcessRecord{4231a630646:com.android.inputmethod.latin/u0a10021} lowMem=true,lastgced=3110616msago,lastlowMem=18198msago ProcessProcessRecord{4250cfe0676:com.android.location.fused/u0a10018} lowMem=true,lastgced=3110616msago,lastlowMem=18198msago ProcessProcessRecord{422f2dd81791:com.android.systemui/u0a10030} lowMem=true,lastgced=3110616msago,lastlowMem=18198msago
  21. 21. Now for the Kernel Bits
  22. 22. KSM Kernel SamePage Merging KSM can save about 20-30MB of RAM if given a chance
  23. 23. Enabling KSM This is easy-peasy echo1|tee/sys/kernel/mm/ksm/run echo1000|tee/sys/kernel/mm/ksm/sleep_millisecs echo128|tee/sys/kernel/mm/ksm/pages_to_scan
  24. 24. Oh yeah... You need to mark pages as mergeable. void*mmap(void*addr,size_tsize,intprot,intflags,intfd,off_toffset){ ... if(result!=MAP_FAILED&&(flags&(MAP_PRIVATE|MAP_ANONYMOUS))!=0){ ErrnoRestorererrno_restorer; madvise(result,size,MADV_MERGEABLE); } returnresult; }
  25. 25. Is it working? dumpsys meminfo answers this TotalPSS:126741kB KSM:6672kBsavedfromshared1128kB 24552kBunshared;102936kBvolatile
  26. 26. extra_free_kbytes Raises the low water mark used to trigger kswapd
  27. 27. References Dalvik VM Internals - 2008 Google I/O A JIT Compiler for Android's Dalvik VM - 2010 Google I/O Running Android with low RAM Managing Your App's Memory Investigating your RAM Usage Understanding the Linux Virtual Memory Manager - Zones Reducing Memory Access Lateny
  28. 28. THE END

×