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
78
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
80
Застосування ML в реальних проектах - Андрій Дерень
gdgternopil
2
110
Android Architecture Components by Ihor Dzikovskyy
gdgternopil
0
160
First look at Room Persistence by Oleksiy Sazhko
gdgternopil
0
110
Mobile Applications Architecture by Constantine Mars
gdgternopil
1
82
Tuning your SQLite with SQLDelight & SQLBrite - Mkhytar Mkhoian
gdgternopil
0
270
Speeding up development with AutoValue - Andrii Rakhimov
gdgternopil
1
84
The Mistery of Gradle Plugins - Dmytro Zaitsev
gdgternopil
1
74
Xamarin Build native Android & iOS apps with C# - Vitalii Smal
gdgternopil
1
100
Other Decks in Programming
See All in Programming
Jakarta EE Core Profile and Helidon - Speed, Simplicity, and AI Integration
ivargrimstad
0
110
一人でAIプロダクトを作るための工夫 〜技術選定・開発プロセス編〜 / I want AI to work harder
rkaga
12
2.7k
A Gopher's Guide to Vibe Coding
danicat
0
170
DockerからECSへ 〜 AWSの海に出る前に知っておきたいこと 〜
ota1022
5
1.8k
画像コンペでのベースラインモデルの育て方
tattaka
3
1.8k
未来を拓くAI技術〜エージェント開発とAI駆動開発〜
leveragestech
2
170
tool ディレクティブを導入してみた感想
sgash708
1
150
学習を成果に繋げるための個人開発の考え方 〜 「学習のための個人開発」のすすめ / personal project for leaning
panda_program
1
110
Google I/O recap web編 大分Web祭り2025
kponda
0
2.9k
Microsoft Orleans, Daprのアクターモデルを使い効率的に開発、デプロイを行うためのSekibanの試行錯誤 / Sekiban: Exploring Efficient Development and Deployment with Microsoft Orleans and Dapr Actor Models
tomohisa
0
190
コンテキストエンジニアリング Cursor編
kinopeee
1
660
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
640
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
134
9.5k
The Cost Of JavaScript in 2023
addyosmani
53
8.8k
Agile that works and the tools we love
rasmusluckow
329
21k
Designing for Performance
lara
610
69k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
Rails Girls Zürich Keynote
gr2m
95
14k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
1k
4 Signs Your Business is Dying
shpigford
184
22k
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