Memory Leaks on Android

  Рет қаралды 56,616

CodingWithMitch

CodingWithMitch

Күн бұрын

In this video I talk about memory leaks on Android.
Practical example starts: 5:53
If you've never heard of memory leaks, chances are you probably have a lot of them.
Specifically speaking, this is what I talk about in the video:
1. Memory leaks
2. What exactly memory leaks are
3. Why you want to avoid memory leaks
4. The most common way memory leaks occur
5. Coding patterns that typically lead to memory leaks
6. And then I’ll finish up by showing you a practical way you can detect memory leaks in your code
➤Learn to Code Like me:
↻ codingwithmitch.com/register/
➤Memory Leaks Blog Post:
↻ codingwithmitch.com/blog/memo...
➤My Instagram:
↻ / mitch.tabian

Пікірлер: 112
@codinginflow
@codinginflow 5 жыл бұрын
They should build the LeakCanary logic into the garbage collector
@codingwithmitch
@codingwithmitch 5 жыл бұрын
That would make it too easy 😅
@zameelnm2365
@zameelnm2365 3 жыл бұрын
yeph i thought it too if LeakCanary can detect why can't collect
@napalm_exe
@napalm_exe 5 жыл бұрын
Was always curious about the term "Memory Leak", now thanks to you I have a better understanding! Thank you for all your awesome content Mitch!
@samha1513
@samha1513 5 жыл бұрын
I love how you did the animation for the HEAP and GC. I did not know about the Leakcanary and now that I do, I will definitely use it. Thanks
@codingwithmitch
@codingwithmitch 5 жыл бұрын
The animations were done by a fan. I really liked what he did.
@samha1513
@samha1513 5 жыл бұрын
Yes it was amazing.
@BrilTek
@BrilTek 4 жыл бұрын
Thanks a lot for this. This video helped me understand memory leaks as well as CONTEXTS(which I had been struggling with)
@andrej753
@andrej753 5 жыл бұрын
The quality of content you put out is amazing! Keep going!
@seifeldenehab3763
@seifeldenehab3763 4 жыл бұрын
this was really helpful ! thanks mitch ^^
@b.k4142
@b.k4142 5 жыл бұрын
Great job mitch! when i learn some thing new from mitch i got more energy to learn hard android programming!
@harinarayananv.m989
@harinarayananv.m989 4 жыл бұрын
Simple and to the point, really helpful, thank you for making this video:)
@agungkurniawan4078
@agungkurniawan4078 5 жыл бұрын
Hi, Mitch. Good content. Keep up the good works!
@corporateworld7gl6re8h
@corporateworld7gl6re8h 3 жыл бұрын
This was really very helpful to understand memory leaks, thank you very much, please keep going with these kind of awesome contents:)
@davidscammell9823
@davidscammell9823 3 жыл бұрын
Great video, thanks Mitch. Looking at Leakcanary I can see they have second version for Kotlin. Will add this to my code!
@Rassy_
@Rassy_ 5 жыл бұрын
Excited for more great content 2019 💪🏾
@codingwithmitch
@codingwithmitch 5 жыл бұрын
:D
@mytubekt
@mytubekt 5 жыл бұрын
How to give more thumbs up instead of only one! :) Love it, so informative!
@CiprianBindiu
@CiprianBindiu 5 жыл бұрын
Very helpful and educational. Thank you, Mitch.
@GAMITECH_RK03
@GAMITECH_RK03 3 жыл бұрын
Nice content it cleared all my doubts about memory leak and gc Keep it going
@dawoodamir2011
@dawoodamir2011 5 жыл бұрын
That is definitely good practicing for not using context in a background thread really helpfull video
@hotony4092
@hotony4092 4 жыл бұрын
Good illustration. Simple and clear, thanks.
@M1A2_Abrams_MBT
@M1A2_Abrams_MBT 3 жыл бұрын
Great video man. Liked and subbed.
@techmarinar
@techmarinar 3 жыл бұрын
hey man thank you very much , you always doing what I need , thank you
@rahulabrol2211
@rahulabrol2211 4 жыл бұрын
Thank you so much for explaining this way..it clears all doubts of mine
@mformobileapps
@mformobileapps 3 жыл бұрын
you are amazing mitch..
@shivamprasad782
@shivamprasad782 2 жыл бұрын
Great video. Thanks for the clear illustration.
@kinleyk3759
@kinleyk3759 Жыл бұрын
Bro how do you do on phone?
@bjugdbjk
@bjugdbjk 5 жыл бұрын
Excellent Explanation.Good in details.
@toseefalikhan2297
@toseefalikhan2297 5 жыл бұрын
Really helpful!
@sagarprajapati8769
@sagarprajapati8769 4 жыл бұрын
@codingwithmitch , Hi can you please make more videos on this topic and also on ANR and High CPU usage topic ?
@RatneshNavlakhe
@RatneshNavlakhe 2 жыл бұрын
Thanks for the video, one question I have is can we Lazy operator instead of WeakReference ?
@JoelMathewmatgoogle
@JoelMathewmatgoogle 4 жыл бұрын
Amazing video!
@AmitVerma-iq3oe
@AmitVerma-iq3oe 5 жыл бұрын
thanks a lot for good content waiting for more
@oleksandrgrument7948
@oleksandrgrument7948 5 жыл бұрын
Good explanation, thanks
@sha17187
@sha17187 4 жыл бұрын
Thanks for this great tutorial! Can you please show the error log for the crash?
@codinginflow
@codinginflow 5 жыл бұрын
I have a memory leak every Saturday when I come home from the Pub, amirite? (Just kidding, I don't drink)
@codingwithmitch
@codingwithmitch 5 жыл бұрын
Haha. I usually have a couple drinks on Friday with my pals. So this is accurate for me. I got up at 1030 this morning and brain didn't feel good
@babintandukar8917
@babintandukar8917 5 жыл бұрын
so basically hangover is memory leak ggwp :V .
@davidscammell9823
@davidscammell9823 3 жыл бұрын
After much vigorous testing, I can conclude writing code on a hangover makes it much more difficult than it needs to be!
@codinginflow
@codinginflow 3 жыл бұрын
@@davidscammell9823 😆
@minarezkalla4403
@minarezkalla4403 3 жыл бұрын
Amazing ,Thanks alot
@ShivamSharma-kv6bf
@ShivamSharma-kv6bf 3 жыл бұрын
I always used this method, now got cleared thanks
@safees3165
@safees3165 5 жыл бұрын
Great video man! Any chance you could switch to a dark theme? Will definitely help us nite owls
@codingwithmitch
@codingwithmitch 5 жыл бұрын
Thanks. I like white sorry friend.
@lucienchu9649
@lucienchu9649 4 жыл бұрын
So, a memory leak happens when a background threat holds a Context or Activity reference, am I correct? So if I have another normal Java class that holds (requires) a Context or Activity reference but that class does not deal with any background thread, so there would not be any memory leak even though it holds such a reference. Since an object of this class would be gone as soon as the Activity holding that object is gone. Correct me if I was wrong. Because I was so afraid that I manually set the reference to null on each onPause or onDestroy method in an Acticity, in order to make sure the reference would be release :(
@SAROJKUMAR-rr1rr
@SAROJKUMAR-rr1rr 4 жыл бұрын
very good explanation on memory leak
@plunkettdog8304
@plunkettdog8304 4 жыл бұрын
Ok just say I have a context object on a background task but then the associated activity is terminated but it cant be cleaned up, I can understand that the app is then using up more resources than it needs to at this point but when the background task finishes won't the memory be able to be reclaimed by the GC? so what is the big deal if memory is taken up for an extra second or two? Wouldn't this only be a problem on long running background threads?
@rockson6839
@rockson6839 5 жыл бұрын
Nice ... Please how long have you been using Android studio and Java?
@codingwithmitch
@codingwithmitch 5 жыл бұрын
3 years
@jimpauloovejera2599
@jimpauloovejera2599 3 жыл бұрын
How about you use an ApplicationContext? Would you still cancel the async on onDestroy?
@babintandukar8917
@babintandukar8917 5 жыл бұрын
so u can just apply this to any one class which extends application class ..?
@himanshukandwal1374
@himanshukandwal1374 4 жыл бұрын
Great 😌
@ummelaila4628
@ummelaila4628 Жыл бұрын
Helpful
@user-bh3ul2yk3q
@user-bh3ul2yk3q 4 жыл бұрын
Cooool...It is so simple
@omkarpawar1741
@omkarpawar1741 5 жыл бұрын
can you create a tutorial for ANR "Application Not Responding" (ANR)
@georgeclinton2727
@georgeclinton2727 4 жыл бұрын
What if after a while the Garbage collector cleans the memory holding the context object when the activity is destroyed and the weak reference in the AsyncTask is trying to use the context to access a resource for example. Will the App crash? Please help me explain
@darrinreed8027
@darrinreed8027 3 жыл бұрын
How do I deal with graphics memory? I have tried several times to get any memory leaks, but I have only gotten a memory leak a few days ago, but I have noticed that the graphics memory doesn't go down any. I haven't been able to find anything as far as managing the graphics memory.
@michealandretis7243
@michealandretis7243 2 жыл бұрын
So how is this not causing a crash ? context is garbage collected before asynctask is completed.. effectively null and mContext points to nothing ? or ... ? could you please explain this?
@smonkey001
@smonkey001 Жыл бұрын
Well, to be fair, it's not like your background thread never end. Once it ends, its grabbing to the objects also end, and once the next round of GC kick in and it will be collected.
@user-sankarsana
@user-sankarsana 4 жыл бұрын
Very well! Which emulator are you using?
@codingwithmitch
@codingwithmitch 4 жыл бұрын
Vysor, a google chrome extension
@yumyum17
@yumyum17 5 жыл бұрын
Thanks for the video, but the leakcanary is not showing any leaks for me. I used your code from the video and I ran my app on emulator and physical device.
@LeonardoHernandezIC
@LeonardoHernandezIC 4 жыл бұрын
It's not all heap ... There is also the stack in relation to memory consumption.
@amanaggarwal5884
@amanaggarwal5884 3 жыл бұрын
Hi mitch, this is a very good tutorial but i am curious 1. if we use LeakCanary then it always shows a notification when there is memeory leak? if we dont want to show these types of notifications to users and want LeakCanary to run in the background for our mess. Can we customize that too?? 2. Cant we use java Finalize() too??.
@codingwithmitch
@codingwithmitch 3 жыл бұрын
Don't use leak canary in your production build
@amanaggarwal5884
@amanaggarwal5884 3 жыл бұрын
I dont have any company experience. I am learning from you guys(you and codeinflow and more) . Can you tell us more how we gonna code in professional build and he we can stop memory leak and give us some tips what we should do while making a professional build.
@anujgoyal5253
@anujgoyal5253 3 ай бұрын
where to get code where leak is happening !
@MaisUmSomente
@MaisUmSomente 3 жыл бұрын
AWSOME
@uio86a
@uio86a 2 жыл бұрын
It's refreshed after using a movie or camera and going to another app, is there any solution?
@robertpeschke7746
@robertpeschke7746 Жыл бұрын
So funny to see this video now. "Leak Canary is created by a very well-renowned company called square....". I think you know a lot more about this company now!
@codinginflow
@codinginflow 5 жыл бұрын
Are you recording this from inside a box
@codingwithmitch
@codingwithmitch 5 жыл бұрын
A Birdbox
@sexchrist9966
@sexchrist9966 3 жыл бұрын
V. V. G. V. Vv V. v v v v bbbbbbbbBbBbBBbtbtbtbtbtbtbtbtttttbtbBut. . V. V. . V. . B. . . V. V V. V. V. . V. v. ut. But be. Gg. . G. But v. . od. V. V. G g. . . . . . b But v v. V. be. V. V. tbtbt v . V. . G. @@codingwithmitch
@AbdullahKhan-qn6nu
@AbdullahKhan-qn6nu 4 жыл бұрын
still leaking memory with this approach while loading next activity
@telephon3208
@telephon3208 2 жыл бұрын
firstly heap is data structure. secondly when you instantiate some class you write it to heap, not "take it away FROM heap"
@hamidmahmoodi6690
@hamidmahmoodi6690 4 жыл бұрын
nice, tnx
@progtom7585
@progtom7585 3 жыл бұрын
Learned a heap, (Pun intended) thanks heaps
@josephmolina5477
@josephmolina5477 5 жыл бұрын
Is the Android garbage collector different than Java's?
@workmanager2008
@workmanager2008 4 жыл бұрын
Not covered all the possible cases
@isahjade1903
@isahjade1903 2 жыл бұрын
4:19 The most common way memory leaks occur
@exclusiven940
@exclusiven940 5 жыл бұрын
Why do you not have Apps on Playstore...It's kinda like teaching stuff when you don't implement it yourself??
@codingwithmitch
@codingwithmitch 5 жыл бұрын
I understand what you're saying. The truth is, I haven't had any ideas I think are good enough to invest 1-2 months developing.
@exclusiven940
@exclusiven940 5 жыл бұрын
CodingWithMitch I'm sorry Mitch Tabian. I wanted to follow your channel with the intent of LEARNING stuff and implementing. You're one of the best at what you do. Please don't take the comment negatively. I want to see things work for you. Please try! What if it does
@codingwithmitch
@codingwithmitch 5 жыл бұрын
@@exclusiven940 I definitely will. Just waiting for the right time and the right idea.
@viktorvostrikov9625
@viktorvostrikov9625 5 жыл бұрын
Wow, just searched in google by recent and this video appeared... I can't use memory profiler in android 3.0 to find memory leaks.. It is so frustrating. Maybe someone does know where is detect leaked activities checkbox?
@codingwithmitch
@codingwithmitch 5 жыл бұрын
Use leak canary
@viktorvostrikov9625
@viktorvostrikov9625 5 жыл бұрын
@@codingwithmitch I did used it but had some strange memory leaks, which I could not figure out to this day. maybe you saw similar messages? stackoverflow.com/questions/53311250/what-does-viewrootlmpl-mattachinfo-means
@codingwithmitch
@codingwithmitch 5 жыл бұрын
@@viktorvostrikov9625 I'm not sure. I don't see anything in the code you provided that would explain what Leak Canary is saying.
@dbtechprojects2392
@dbtechprojects2392 3 жыл бұрын
setting up LeakCanary as we speak........
@medomody242
@medomody242 5 жыл бұрын
Very good , and I have a question would you please answer me I made an android app that shows the yearly vacation in a table using sqlite database , and I have the problem of gc because I made a lot of views at the beginning. How can I make the view comes faster because I use a tableview and I add to it textviews. Would you help me ? My name is medo
@codingwithmitch
@codingwithmitch 5 жыл бұрын
I'm not going to review your code or help you code, but you can ask me a specific question and I can try to help.
@medomody242
@medomody242 5 жыл бұрын
My question is how to load thousands of textviews inside a tableview in a short time without gc affect ? I need only an advice from you Mr
@medomody242
@medomody242 5 жыл бұрын
And thank you very much cause really all what you do is helpful and high class
@codingwithmitch
@codingwithmitch 5 жыл бұрын
@@medomody242 why not just use a single textview? Then just append all the data to a StringBuilder or something and display it. Or use a recyclerview if there's thousands.
@medomody242
@medomody242 5 жыл бұрын
I tried this Mr but I have a big scroll table one column fixed with recyclerview it scroll only one row but I need all to be scrolled
@ankitrajdwivedi996
@ankitrajdwivedi996 5 жыл бұрын
how to prevent memory leak because of view in previous activity's fragment..????..
@codingwithmitch
@codingwithmitch 5 жыл бұрын
Did leak canary tell you it's leaking?
@ankitrajdwivedi996
@ankitrajdwivedi996 5 жыл бұрын
@@codingwithmitch yes it points to normal relative layout. But I can't find why because that layout is not static and also not accessing in any background process it always on ui thread.
@codingwithmitch
@codingwithmitch 5 жыл бұрын
@@ankitrajdwivedi996 Did you instantiate the fragment using the new instance method?
@ankitrajdwivedi996
@ankitrajdwivedi996 5 жыл бұрын
@@codingwithmitch yes some places I have but in some places adding through the fragment transaction.. both places I have that issue...
@codingwithmitch
@codingwithmitch 5 жыл бұрын
@@ankitrajdwivedi996 post a gist containing the code causing an error
@bukunmi6378
@bukunmi6378 5 жыл бұрын
Nice to see ur face and not hear ur cool voice only 😁.
@ben6
@ben6 3 жыл бұрын
Doesn't this memory leak stop as soon as AsyncTask is finished? It other words, 5 seconds? You could argue the AsyncTask can be running on for a long time after that. SURE, but then thats the problem: you're running AsyncTask in the background for no good reason. This is a much bigger problem than having reference to the activity since you might be doing some work on that thread and holding resources. TLDR: I believe this video highlights how to program badly with AsyncTask (then fixes it). It doesn't show a real-life case of memory leak.
@robertmaurin349
@robertmaurin349 4 жыл бұрын
Application memory à supprimer
@mohamamdhassanrezaeitabar3695
@mohamamdhassanrezaeitabar3695 5 жыл бұрын
what's your emulator?
@codingwithmitch
@codingwithmitch 5 жыл бұрын
vysor, a google chrome extension
@RenatKaitmazov
@RenatKaitmazov 4 жыл бұрын
The example with AsyncTask shown in the video is too simple and more importantly is outdated. In real life nobody uses it. Nowadays developers use either Rx or coroutines. It would be more relevant and valuable to show an example using these technologies.
@tamiatyesha1552
@tamiatyesha1552 3 жыл бұрын
p
@rahulabrol2211
@rahulabrol2211 4 жыл бұрын
Thank you so much for explaining this way..it clears all doubts of mine
Find and Fix MEMORY LEAKS with Leak Canary in Android 👀
8:31
Philipp Lackner
Рет қаралды 35 М.
Process Death and ViewModels (My apps are BROKEN)
26:32
CodingWithMitch
Рет қаралды 12 М.
JPEG is Dying - And that's a bad thing
8:09
2kliksphilip
Рет қаралды 37 М.
Understanding Android memory usage (Google I/O '18)
39:05
Android Developers
Рет қаралды 66 М.
How to detect and fix Memory Leaks on Android with Android Studio
8:53
Lukas Lechner - Advanced Android Development
Рет қаралды 37 М.
From Physics Student to Software Engineer
6:22
CodingWithMitch
Рет қаралды 11 М.
How to Kotlin - from the lead Kotlin language designer (Google I/O '18)
39:24
iOS Development is Easier than Android Development (VLOG)
20:41
CodingWithMitch
Рет қаралды 52 М.
Advice from a Principal Software Engineer at Amazon (Steve Huynh)
22:45
Trash talk (Android Dev Summit '18)
39:54
Android Developers
Рет қаралды 8 М.