Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Avoiding Memory leaks by Viktor Kifer
Search
GDG Ternopil
July 22, 2015
Programming
0
71
Avoiding Memory leaks by Viktor Kifer
Avoiding Memory leaks by Viktor Kifer
GDG Ternopil
July 22, 2015
Tweet
Share
More Decks by GDG Ternopil
See All by GDG Ternopil
Semi supervised learning with Autoencoders by Ілля Горев
gdgternopil
1
70
Застосування ML в реальних проектах - Андрій Дерень
gdgternopil
1
98
Android Architecture Components by Ihor Dzikovskyy
gdgternopil
1
150
First look at Room Persistence by Oleksiy Sazhko
gdgternopil
1
82
Mobile Applications Architecture by Constantine Mars
gdgternopil
2
64
Tuning your SQLite with SQLDelight & SQLBrite - Mkhytar Mkhoian
gdgternopil
0
250
Speeding up development with AutoValue - Andrii Rakhimov
gdgternopil
0
72
The Mistery of Gradle Plugins - Dmytro Zaitsev
gdgternopil
0
57
Xamarin Build native Android & iOS apps with C# - Vitalii Smal
gdgternopil
0
68
Other Decks in Programming
See All in Programming
アクセシビリティの観点からみたFluent UI Blazorのすすめ
tomokusaba
1
180
計算量オーダーの話
tsudaahr
1
130
スタックトレース始めてみた
kuro_kurorrr
5
1.3k
The Design of Everyday APIs - PyCon 2024
roguelynn
1
640
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | KotlinConf
prof18
0
110
Kubernetes Code Contribution入門
bells17
3
700
slog登場に伴うloggerの取り回し手法の見直し / kamakura.go #6
arthur1
1
440
Rearchitect Ripper
yui_knk
2
350
Upgrading Legacy to the Latest PHP Version
afilina
PRO
0
160
メッセージとイベントを中核に置いたシステム設計の有用性について
j5ik2o
PRO
8
600
JS RPCを理解する
yusukebe
5
390
EduBlocksでPythonのブロックプログラミング体験!
ueponx
0
170
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Practical Orchestrator
shlominoach
183
9.8k
How to train your dragon (web standard)
notwaldorf
76
5.3k
What's new in Ruby 2.0
geeforr
338
31k
Faster Mobile Websites
deanohume
300
30k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
41
4.5k
We Have a Design System, Now What?
morganepeng
43
6.8k
GitHub's CSS Performance
jonrohan
1025
450k
Documentation Writing (for coders)
carmenintech
60
4k
Large-scale JavaScript Application Architecture
addyosmani
504
110k
The Brand Is Dead. Long Live the Brand.
mthomps
49
32k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
Transcript
Avoiding memory leaks
Memory Leak? Objects stay in memory even when they are
no longer used by application
Detecting memory leaks
Symptoms • Frequent calls to GC • Lesser amount of
memory reclaimed on each GC • Memory usage increases over time • as a result -> OOM and Response time decreases
Example 1 public class MainActivity extends AppCompatActivity implements SomeListener {
protected void onCreate(Bundle savedInstanceState) { // … Singleton.getInstance().register(this); } public void onSomeEvent() { // some code here } }
Permanently increasing memory usage
Example 1 public class MainActivity extends AppCompatActivity implements SomeListener {
protected void onCreate(Bundle savedInstanceState) { // ... Singleton.getInstance().register(this); } // SomeListener interface implementation protected void onDestroy() { // ... Singleton.getInstance().unregister(this); } }
LeakCanary https://github.com/square/leakcanary
Memory Dump • DDMS - “Dump HPROF file” • android.os.Debug.
dumpHprofData (“filename”) %sdk%/platform-tools/ hprof-conv
Memory Analysis Tool
Preventing memory leak
General rules • unregister event listeners • avoid string concatenation
in loops (use StringBuilder) • avoid unnecessary object creation
General rules (Continue) • close resources in finally blocks (Stream,
Reader, Connection etc) • set static objects to null
General Rules (Android) • call Bitmap.recycle() • avoid object creation
on onDraw() • use getApplicationContext() instead of activity context, but only when appropriate
References
Java Reference? java.lang.ref.Reference<T> • WeakReference • SoftReference • PhantomReference
Strong Reference Activity activity = getActivity();
WeakReference Reference<Activity> activityRef = new WeakReference<Activity>(getActivity()); Activity activity = activityRef.get();
if (activity != null) { // ... }
Non-static inner and anonymous classes contain strong reference to their
outer class public class MyActivity extends Activity { // Anonymous class SomeListener contains strong reference to MyActivity instance Singleton.getInstance().addListener(new SomeListener() { // ... }); }
Example 2 public class HandlerLeakExampleActivity extends AppCompatActivity { private final
Handler mLeakyHandler = new Handler() { @Override public void handleMessage(Message msg) { // ... } }; }
Example 2 public class HandlerLeakExampleActivity extends AppCompatActivity { private static
class MyHandler extends Handler { private final WeakReference<HandlerLeakExampleActivity> mActivity; public MyHandler(HandlerLeakExampleActivity activity) { mActivity = new WeakReference<HandlerLeakExampleActivity>(activity); } public void handleMessage(Message msg) { HandlerLeakExampleActivity activity = mActivity.get(); if (activity != null) { // ... } } } private final MyHandler mHandler = new MyHandler(this); }
Q&A