Memory Leaks on Android

  Переглядів 56,078

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

КОМЕНТАРІ: 114
@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.
@andrej753
@andrej753 5 років тому
The quality of content you put out is amazing! Keep going!
@tekenaT
@tekenaT 4 роки тому
Thanks a lot for this. This video helped me understand memory leaks as well as CONTEXTS(which I had been struggling with)
@seifeldenehab3763
@seifeldenehab3763 4 роки тому
this was really helpful ! thanks mitch ^^
@agungkurniawan4078
@agungkurniawan4078 5 років тому
Hi, Mitch. Good content. Keep up the good works!
@Rassy_
@Rassy_ 5 років тому
Excited for more great content 2019 💪🏾
@codingwithmitch
@codingwithmitch 5 років тому
:D
@dawoodamir2011
@dawoodamir2011 5 років тому
That is definitely good practicing for not using context in a background thread really helpfull video
@harinarayananv.m989
@harinarayananv.m989 4 роки тому
Simple and to the point, really helpful, thank you for making this video:)
@GAMITECH_RK03
@GAMITECH_RK03 3 роки тому
Nice content it cleared all my doubts about memory leak and gc Keep it going
@b.k4142
@b.k4142 4 роки тому
Great job mitch! when i learn some thing new from mitch i got more energy to learn hard android programming!
@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!
@thetriwill
@thetriwill 5 років тому
Hey @mitch_tabian Man for the new year I rather want you to do 5 minute tips and tricks videos and Your Usual Geeky stuff. I'm saying that just to keep the balance right and the channel Growing ..Ryt fella!
@CiprianBindiu
@CiprianBindiu 5 років тому
Very helpful and educational. Thank you, Mitch.
@sagarprajapati8769
@sagarprajapati8769 3 роки тому
@codingwithmitch , Hi can you please make more videos on this topic and also on ANR and High CPU usage topic ?
@rahulabrol2211
@rahulabrol2211 4 роки тому
Thank you so much for explaining this way..it clears all doubts of mine
@RatneshNavlakhe
@RatneshNavlakhe 2 роки тому
Thanks for the video, one question I have is can we Lazy operator instead of WeakReference ?
@mytubekt
@mytubekt 5 років тому
How to give more thumbs up instead of only one! :) Love it, so informative!
@sha17187
@sha17187 4 роки тому
Thanks for this great tutorial! Can you please show the error log for the crash?
@M1A2_Abrams_MBT
@M1A2_Abrams_MBT 3 роки тому
Great video man. Liked and subbed.
@hotony4092
@hotony4092 4 роки тому
Good illustration. Simple and clear, thanks.
@techmarinar
@techmarinar 2 роки тому
hey man thank you very much , you always doing what I need , thank you
@shivamprasad782
@shivamprasad782 2 роки тому
Great video. Thanks for the clear illustration.
@kinleyk3759
@kinleyk3759 Рік тому
Bro how do you do on phone?
@lucienchu9649
@lucienchu9649 3 роки тому
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 :(
@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 😆
@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.
@ShivamSharma-kv6bf
@ShivamSharma-kv6bf 2 роки тому
I always used this method, now got cleared thanks
@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?
@omkarpawar1741
@omkarpawar1741 5 років тому
can you create a tutorial for ANR "Application Not Responding" (ANR)
@babintandukar8917
@babintandukar8917 5 років тому
so u can just apply this to any one class which extends application class ..?
@bjugdbjk
@bjugdbjk 5 років тому
Excellent Explanation.Good in details.
@AmitVerma-iq3oe
@AmitVerma-iq3oe 5 років тому
thanks a lot for good content waiting for more
@toseefalikhan2297
@toseefalikhan2297 5 років тому
Really helpful!
@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?
@mformobileapps
@mformobileapps 3 роки тому
you are amazing mitch..
@user-qu6mb9il6w
@user-qu6mb9il6w 4 роки тому
Very well! Which emulator are you using?
@codingwithmitch
@codingwithmitch 4 роки тому
Vysor, a google chrome extension
@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.
@yumyum17
@yumyum17 4 роки тому
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.
@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
@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?
@oleksandrgrument7948
@oleksandrgrument7948 4 роки тому
Good explanation, thanks
@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.
@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.
@uio86a
@uio86a Рік тому
It's refreshed after using a movie or camera and going to another app, is there any solution?
@anujgoyal5253
@anujgoyal5253 28 днів тому
where to get code where leak is happening !
@minarezkalla4403
@minarezkalla4403 3 роки тому
Amazing ,Thanks alot
@LeonardoHernandezIC
@LeonardoHernandezIC 4 роки тому
It's not all heap ... There is also the stack in relation to memory consumption.
@SAROJKUMAR-rr1rr
@SAROJKUMAR-rr1rr 4 роки тому
very good explanation on memory leak
@JoelMathewmatgoogle
@JoelMathewmatgoogle 4 роки тому
Amazing video!
@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
@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"
@AbdullahKhan-qn6nu
@AbdullahKhan-qn6nu 3 роки тому
still leaking memory with this approach while loading next activity
@josephmolina5477
@josephmolina5477 5 років тому
Is the Android garbage collector different than Java's?
@isahjade1903
@isahjade1903 2 роки тому
4:19 The most common way memory leaks occur
@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
@robertpeschke7746
@robertpeschke7746 9 місяців тому
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!
@workmanager2008
@workmanager2008 4 роки тому
Not covered all the possible cases
@user-bh3ul2yk3q
@user-bh3ul2yk3q 3 роки тому
Cooool...It is so simple
@ummelaila4628
@ummelaila4628 Рік тому
Helpful
@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.
@exclusiven940
@exclusiven940 5 років тому
Why do you not have Apps on Playstore...It's kinda like teaching stuff when you don't implement it yourself??
@thetriwill
@thetriwill 5 років тому
Explained ! Shut the fuck up! Not everyone has the same privileges as some "Lucky peeps" You don't need to be derogatory. He earns the he wants, who the fuck are you to judge. Let him enjoy doing what he loves to do...
@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........
@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
@himanshukandwal1374
@himanshukandwal1374 3 роки тому
Great 😌
@MaisUmSomente
@MaisUmSomente 3 роки тому
AWSOME
@robertmaurin349
@robertmaurin349 4 роки тому
Application memory à supprimer
@progtom7585
@progtom7585 2 роки тому
Learned a heap, (Pun intended) thanks heaps
@RenatKaitmazov
@RenatKaitmazov 3 роки тому
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.
@hamidmahmoodi6690
@hamidmahmoodi6690 4 роки тому
nice, tnx
@mohamamdhassanrezaeitabar3695
@mohamamdhassanrezaeitabar3695 4 роки тому
what's your emulator?
@codingwithmitch
@codingwithmitch 4 роки тому
vysor, a google chrome extension
@bukunmi6378
@bukunmi6378 5 років тому
Nice to see ur face and not hear ur cool voice only 😁.
@tamiatyesha1552
@tamiatyesha1552 3 роки тому
p
@rahulabrol2211
@rahulabrol2211 4 роки тому
Thank you so much for explaining this way..it clears all doubts of mine
Kotlin Coroutines Beginner Example (Android)
23:12
CodingWithMitch
Переглядів 157 тис.
What is virtual memory? - Gary explains
11:28
Android Authority
Переглядів 290 тис.
ISSEI funny story😂😂😂Strange World | Magic Lips💋
00:36
ISSEI / いっせい
Переглядів 53 млн
Эффект Карбонаро и соковыжималка
01:01
История одного вокалиста
Переглядів 2 млн
Kitten has a slime in her diaper?! 🙀 #cat #kitten #cute
00:28
ConstraintLayout deep dive (Android Dev Summit '18)
37:16
Android Developers
Переглядів 86 тис.
Understanding Android memory usage (Google I/O '18)
39:05
Android Developers
Переглядів 65 тис.
compileSdkVersion vs. targetSdkVersion - what's the difference?
5:33
Paulina talks Android
Переглядів 19 тис.
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Переглядів 55 тис.
How to detect and fix Memory Leaks on Android with Android Studio
8:53
Lukas Lechner - Advanced Android Development
Переглядів 35 тис.
Deep dive into Android Studio Profilers (Android Dev Summit '18)
41:28
Android Developers
Переглядів 39 тис.
Understand Kotlin Coroutines on Android (Google I/O'19)
37:49
Android Developers
Переглядів 173 тис.
iOS Development is Easier than Android Development (VLOG)
20:41
CodingWithMitch
Переглядів 51 тис.
RecyclerView OnClickListener (Best practice way)
11:48
CodingWithMitch
Переглядів 290 тис.
Memory leaks on Android
8:44
Kasper Finne Nielsen
Переглядів 1,1 тис.
ISSEI funny story😂😂😂Strange World | Magic Lips💋
00:36
ISSEI / いっせい
Переглядів 53 млн