Александр Терещук - memory analyzer tool and memory optimization tips in android
Post on 15-May-2015
450 Views
Preview:
TRANSCRIPT
MEMORY ANALYZER TOOL AND MEMORY OPTIMIZATION TIPS 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.
Each application works in it’s own process and VM instance.
Large system library.
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 — Concurrent.
remark — Stop-the-world.
sweep — Concurrent.
Garbage Collection: Concurrent Mark and Sweep
Eclipse Memory Analyzer Tool
Shallow size
Shallow size of an object is the amount of memory allocated to store the object itself, not taking into account the referenced objects == Sum of the size of the “object header” and the fields of an object
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
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 Dominator to Y is called “immediate Dominator”
Immediate Dominator
Immediate Dominator
Immediate Dominator
Immediate Dominator
Immediate Dominator
Demo
Common pitfalls
“Static” Drawable
private 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);
}
Non-static Handler class
public class MyActivity extends Activity {
Handler myhandler = new Handler() { ... };
}
WebView (only for 2.x)
View.setTag(key,tag) - pre 4.0
public 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 wouldn't be thrown. But they aren't 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]; }
}
Questions ?
The End
Email: atereshchuk@stanfy.com.ua
Skype: rivne2
top related