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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
GDG Ternopil
July 22, 2015
Programming
0
81
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
2
85
Застосування ML в реальних проектах - Андрій Дерень
gdgternopil
2
120
Android Architecture Components by Ihor Dzikovskyy
gdgternopil
0
160
First look at Room Persistence by Oleksiy Sazhko
gdgternopil
0
120
Mobile Applications Architecture by Constantine Mars
gdgternopil
1
97
Tuning your SQLite with SQLDelight & SQLBrite - Mkhytar Mkhoian
gdgternopil
0
280
Speeding up development with AutoValue - Andrii Rakhimov
gdgternopil
1
97
The Mistery of Gradle Plugins - Dmytro Zaitsev
gdgternopil
1
82
Xamarin Build native Android & iOS apps with C# - Vitalii Smal
gdgternopil
1
110
Other Decks in Programming
See All in Programming
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
240
AI巻き込み型コードレビューのススメ
nealle
2
360
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
5
470
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
140
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
7.4k
要求定義・仕様記述・設計・検証の手引き - 理論から学ぶ明確で統一された成果物定義
orgachem
PRO
1
140
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.6k
MUSUBIXとは
nahisaho
0
130
ぼくの開発環境2026
yuzneri
0
230
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
140
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
180
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Visualization
eitanlees
150
17k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
440
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
66
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
New Earth Scene 8
popppiees
1
1.5k
The Limits of Empathy - UXLibs8
cassininazir
1
220
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
110
Color Theory Basics | Prateek | Gurzu
gurzu
0
200
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
66
Faster Mobile Websites
deanohume
310
31k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
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