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
89
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Avoiding Memory leaks by Viktor Kifer
Avoiding Memory leaks by Viktor Kifer
GDG Ternopil
July 22, 2015
More Decks by GDG Ternopil
See All by GDG Ternopil
Semi supervised learning with Autoencoders by Ілля Горев
gdgternopil
2
94
Застосування ML в реальних проектах - Андрій Дерень
gdgternopil
2
130
Android Architecture Components by Ihor Dzikovskyy
gdgternopil
0
180
First look at Room Persistence by Oleksiy Sazhko
gdgternopil
0
130
Mobile Applications Architecture by Constantine Mars
gdgternopil
1
120
Tuning your SQLite with SQLDelight & SQLBrite - Mkhytar Mkhoian
gdgternopil
0
290
Speeding up development with AutoValue - Andrii Rakhimov
gdgternopil
1
110
The Mistery of Gradle Plugins - Dmytro Zaitsev
gdgternopil
1
97
Xamarin Build native Android & iOS apps with C# - Vitalii Smal
gdgternopil
1
130
Other Decks in Programming
See All in Programming
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
390
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.8k
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
670
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
230
メールのエイリアス機能を履き違えない
isshinfunada
0
250
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
1.1k
運用ダッシュボードの設計を誰も教えてくれないのだけどみなさんどうしてるんですか? - チームに監視するという文化を根付かせるための第一歩を踏みたい -
satoshi256kbyte
1
130
Building a Meta Ray-Ban display app
akkeylab
0
170
yield再入門 #phpcon
o0h
PRO
0
1.2k
Go 1.27 における memory allocation の高速化
andpad
0
270
React本体のコードリーディング
high_g_engineer
1
150
5分で問診!Composer セキュリティ健康診断
codmoninc
0
1.1k
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
490
BBQ
matthewcrist
89
10k
WCS-LA-2024
lcolladotor
0
800
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
What's in a price? How to price your products and services
michaelherold
247
13k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
300
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
220
KATA
mclloyd
PRO
35
15k
What does AI have to do with Human Rights?
axbom
PRO
1
2.3k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
660
A Modern Web Designer's Workflow
chriscoyier
698
190k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
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