MEMORY ANALYZER TOOLAND MEMORY OPTIMIZATIONTIPS IN ANDROID    Olexandr Tereshchuk
Memory efficiency
Problem   Low RAM size – 256MB to 512MB on    most of devices.   Low CPU performance – harder to    perform full GC.   ...
Zygote
Zygote – closer look
DEX
DEX – closer look
Garbage Collection:Mark and Sweep
Garbage Collection:Concurrent Mark and Sweep   initial mark — stop-the-world.   mark — Concurrent.   preclean — Concurr...
Garbage Collection:Concurrent Mark and Sweep
Eclipse Memory Analyzer Tool
Shallow sizeShallow size of an object is the amount of memoryallocated to store the object itself, not taking intoaccount ...
Retained size   Retained set of X       The set of objects that would be reclaimed, if we        could delete Object X ...
Demo
Dominator Tree   X dominates Y, if all paths from a GC Root    to Y go via X. “X is a Dominator of Y”   The closest Domi...
Immediate Dominator
Immediate Dominator
Immediate Dominator
Immediate Dominator
Immediate Dominator
Demo
Common pitfalls
“Static” Drawableprivate static Drawable sBackground;  @Override  protected void onCreate(final Bundle state) {    super.o...
Non-static Handler classpublic class MyActivity extends Activity {   Handler myhandler = new Handler() { ... }; }
WebView (only for 2.x)
View.setTag(key,tag) - pre 4.0public static class MainActivity extends Activity {   private final WeakHashMap<Parent, Pare...
Questions ?
The EndEmail: atereshchuk@stanfy.com.uaSkype: rivne2
Upcoming SlideShare
Loading in …5
×

Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android

695 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
695
On SlideShare
0
From Embeds
0
Number of Embeds
176
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android

  1. 1. MEMORY ANALYZER TOOLAND MEMORY OPTIMIZATIONTIPS IN ANDROID Olexandr Tereshchuk
  2. 2. Memory efficiency
  3. 3. Problem Low RAM size – 256MB to 512MB on most of devices. Low CPU performance – harder to perform full GC. Each application works in it’s own process and VM instance. Large system library.
  4. 4. Zygote
  5. 5. Zygote – closer look
  6. 6. DEX
  7. 7. DEX – closer look
  8. 8. Garbage Collection:Mark and Sweep
  9. 9. Garbage Collection:Concurrent Mark and Sweep initial mark — stop-the-world. mark — Concurrent. preclean — Concurrent. remark — Stop-the-world. sweep — Concurrent.
  10. 10. Garbage Collection:Concurrent Mark and Sweep
  11. 11. Eclipse Memory Analyzer Tool
  12. 12. Shallow sizeShallow size of an object is the amount of memoryallocated to store the object itself, not taking intoaccount the referenced objects == Sum of the size ofthe “object header” and the fields of an object
  13. 13. Retained size Retained set of X  The set of objects that would be reclaimed, if we could delete Object X Retained size of X  The Retained size of an object X is equal to the shallow size of the „Retained sets“ of X
  14. 14. Demo
  15. 15. Dominator Tree X dominates Y, if all paths from a GC Root to Y go via X. “X is a Dominator of Y” The closest Dominator to Y is called “immediate Dominator”
  16. 16. Immediate Dominator
  17. 17. Immediate Dominator
  18. 18. Immediate Dominator
  19. 19. Immediate Dominator
  20. 20. Immediate Dominator
  21. 21. Demo
  22. 22. Common pitfalls
  23. 23. “Static” Drawableprivate static Drawable sBackground; @Override protected void onCreate(final Bundle state) { super.onCreate(state); final TextView label = new TextView(this); label.setText("Leaks are bad"); if (sBackground == null) { sBackground = getDrawable(R.drawable.large_bitmap); } label.setBackground(sBackground); setContentView(label); }
  24. 24. Non-static Handler classpublic class MyActivity extends Activity { Handler myhandler = new Handler() { ... }; }
  25. 25. WebView (only for 2.x)
  26. 26. View.setTag(key,tag) - pre 4.0public static class MainActivity extends Activity { private final WeakHashMap<Parent, Parent.Child> mMap = new WeakHashMap<Parent, Parent.Child>(); @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); // If parents were collected OOM error wouldnt be thrown. But they arent collected so we get OOM here. for (int i = 0; i < 1000; ++i) { final Parent parent = new Parent(); mMap.put( parent, parent.mChild ); } }}public static class Parent { public final Child mChild = new Child(); public class Child { private final byte[] mJunk = new byte[10*1024*1024]; }}
  27. 27. Questions ?
  28. 28. The EndEmail: atereshchuk@stanfy.com.uaSkype: rivne2

×