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
76
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
75
Застосування ML в реальних проектах - Андрій Дерень
gdgternopil
1
110
Android Architecture Components by Ihor Dzikovskyy
gdgternopil
1
160
First look at Room Persistence by Oleksiy Sazhko
gdgternopil
1
100
Mobile Applications Architecture by Constantine Mars
gdgternopil
2
78
Tuning your SQLite with SQLDelight & SQLBrite - Mkhytar Mkhoian
gdgternopil
0
270
Speeding up development with AutoValue - Andrii Rakhimov
gdgternopil
0
80
The Mistery of Gradle Plugins - Dmytro Zaitsev
gdgternopil
0
69
Xamarin Build native Android & iOS apps with C# - Vitalii Smal
gdgternopil
0
99
Other Decks in Programming
See All in Programming
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
220
deno-redisの紹介とJSRパッケージの運用について (toranoana.deno #21)
uki00a
0
150
ニーリーにおけるプロダクトエンジニア
nealle
0
590
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
850
0626 Findy Product Manager LT Night_高田スライド_speaker deck用
mana_takada
0
120
プロダクト志向ってなんなんだろうね
righttouch
PRO
0
170
Benchmark
sysong
0
270
Bytecode Manipulation 으로 생산성 높이기
bigstark
2
380
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
48
31k
Enterprise Web App. Development (2): Version Control Tool Training Ver. 5.1
knakagawa
1
120
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
1.2k
datadog dash 2025 LLM observability for reliability and stability
ivry_presentationmaterials
0
180
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
49
14k
KATA
mclloyd
30
14k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.8k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
800
Typedesign – Prime Four
hannesfritz
42
2.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
24k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Embracing the Ebb and Flow
colly
86
4.7k
A Modern Web Designer's Workflow
chriscoyier
694
190k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
138
34k
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