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
73
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
73
Застосування ML в реальних проектах - Андрій Дерень
gdgternopil
1
100
Android Architecture Components by Ihor Dzikovskyy
gdgternopil
1
150
First look at Room Persistence by Oleksiy Sazhko
gdgternopil
1
95
Mobile Applications Architecture by Constantine Mars
gdgternopil
2
69
Tuning your SQLite with SQLDelight & SQLBrite - Mkhytar Mkhoian
gdgternopil
0
250
Speeding up development with AutoValue - Andrii Rakhimov
gdgternopil
0
73
The Mistery of Gradle Plugins - Dmytro Zaitsev
gdgternopil
0
58
Xamarin Build native Android & iOS apps with C# - Vitalii Smal
gdgternopil
0
82
Other Decks in Programming
See All in Programming
イベント駆動で成長して委員会
happymana
1
340
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
350
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
180
WebAssembly Unleashed: Powering Server-Side Applications
chrisft25
0
120
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
300
最新TCAキャッチアップ
0si43
0
210
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
120
Click-free releases & the making of a CLI app
oheyadam
2
120
Tauriでネイティブアプリを作りたい
tsucchinoko
0
380
Djangoの開発環境で工夫したこと - pre-commit / DevContainer
hiroki_yod
1
250
cmp.Or に感動した
otakakot
3
260
Featured
See All Featured
Gamification - CAS2011
davidbonilla
80
5k
We Have a Design System, Now What?
morganepeng
50
7.2k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Side Projects
sachag
452
42k
Embracing the Ebb and Flow
colly
84
4.5k
Rails Girls Zürich Keynote
gr2m
94
13k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Scaling GitHub
holman
458
140k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Documentation Writing (for coders)
carmenintech
65
4.4k
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