Android Memory Management - How does it work?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Android's memory "management" definitely needs some improvement. Upgrading from 4 GB to 6 GB of memory (S8+ to S9+) has made a HUGE difference for me but I still get what seem like random flushes where apps reload after simply switching between two apps.

👍︎︎ 49 👤︎︎ u/bernath 📅︎︎ Jan 24 2019 🗫︎ replies

Tl dw?

👍︎︎ 12 👤︎︎ u/[deleted] 📅︎︎ Jan 24 2019 🗫︎ replies

If you have an Essential Phone on Pie the question is more "why doesn't it work?"

👍︎︎ 8 👤︎︎ u/[deleted] 📅︎︎ Jan 24 2019 🗫︎ replies

No issues on Android Oreo 8.1 Google Pixel 1

👍︎︎ 5 👤︎︎ u/[deleted] 📅︎︎ Jan 24 2019 🗫︎ replies

I'm just here for the hot takes.

👍︎︎ 3 👤︎︎ u/mec287 📅︎︎ Jan 24 2019 🗫︎ replies
Captions
hello my name's Gary Sims and this is Gary explains now I've been working on a video for the annual thority channel about how much ram do you need in an android smartphone for 2019 and in doing that I have been refreshing my memory about how RAM management works in Linux and in Android and I thought really it kind of warranted its own video rather than trying to roll that up into a look at how much RAM is actually needed so the plan is I'm going to do a video on this channel now about RAM management and then there'll be a follow-up video on the Android first channel about how much RAM do you actually need so if you want to find out more please let me explain at the heart of the Android operating system is Linux and the Linux kernel and every time you start a new app what actually happens you're creating a process inside of Linux now a process is responsible for all of the different resources that the kernel might allocate to the different apps so a CPU time is a resource and input and output writing file right into the network reading from the network is a resource and RAM memory is a resource now the thing about CPU time and i/o is in one sense they're infinite if you have a busy CPU and you want to run a new task your task will also get a slice of that CPU time and if it's very busy it will just take longer so although it might happen quicker when the CPU is less busy when it's more busy it just takes a longer amount of time but it still gets done and the same is true of i/o if you're kind of downloading a big file or you're writing something to the internal storage if there's other i/o going on your i/o might be delayed but ultimately will also happen it just might take longer however a ram it's kind of a fixed amount if you run out of RAM there is no more I'm waiting longer for the RAM to kind of magically free itself up is not going to happen so Android and Linux have to have a system of how it copes with the problem of their a limited amount of RAM inside of a smartphone so when you tap on an app a new process is launched and that process will say to the Linux kernel I need this amount of memory just to load the program from the internal storage so it'll start to give it some memory and then once the program starts to run it maybe wants to load up some media file with a lot some sprites it might want to download something from the Internet and each of these actions the process the app will say to the Linux kernel can I have some more memory please can I have another megabyte can another two megabytes for these files that I'm downloading to these files that I'm reading and the Kint Linux kernel will say yep here's some memory you can actually have it now and use it for what you need to now the way it actually works is that Linux has a pool of what we're going to call available memory now that's different to free memory because actually in a multitasking operating some free memory is a bad thing you don't want memory sitting there doing nothing there's always something you can be doing with memory for example it can be given over to improving file i/o or network i/o for caching it can be given over to other programs that need to be running actually having a large amount of free empty unused memory is a bad idea so limit or does it uses as much memory as it can maybe kind of you know up to 95 percent something like that and it uses it for all the different things that it can that it can do and make that memory useful but it's also marked as available memory which means at any moment it can be stopped being used as a buffer stopped being used as some kind of caching and it can that be given over to a new process so let's say on the pixel 3 for example it's a phone that comes with 4 gigabytes of memory some of that memory taken up by linux some of their memory taken up by android itself and then the available memory once you kind of reboot the phone in a fresh situation would be around maybe 1.7 gigabytes so when an app actually starts up Linux will look at the available memory and say here are some chunks that you can use to do the things you want to do and actually divide up the memory into pages let's say 4 kilobytes pages that it can hand over to different processes as it needs them and that's all fine when there is enough memory when you've just rebooted your phone and you start the first app well it's got lots of available memory 1.7 gigabytes here what do you want take as much as you need not a problem at all but over time you start to use other apps and you start to load them into memory and now the free memory kind of reduces as the number of apps increases at some point there won't be enough memory available memory around so that the next app can do the thing it needs to do and now Linux and Android have a problem and there are one or two things that it can do one is it can use swapping and the other is it can kill an already existing process to free up some memory so let's look at both of those so swapping is an idea that we get from servers and desktops even on Windows and Mac OS and Linux it's an idea that's been around for a decade and the idea is that when you don't have enough memory you can take some of that active memory that's currently in RAM and you can actually write it out to a hard disk and then you can free up that block of memory that page of memory and then later on if that page is actually needed then you can read it back from the hardest and plop it somewhere else in memory and of course you want to write out to the hard disk memory that has not been used or as infrequently used because you don't want to write it out there and then two seconds later have to read it back again now Android doesn't do that to the hardest what it actually does is it uses a thing called Zed Ram and Zed of course in UNIX terms is the symbol for compression so it's compressed RAM and what it says let's allocate a chunk of memory here that we're going to use for swapping but rather than writing it to reduce what we do is we take the memory we compress it and then we're going to store it in this special bit of memory so let's say you start a new app it wants some memory Linux looks and it's available maybe I don't have enough oh I know what I'll do I will go down my list of memory pages and I'll look at what not used very often what's not being used at the moment and I'll kind of compress it down and write it into this area of compressed RAM so if it had let's say 128 K of memory that it wasn't using at this very moment you might compress it down to 64 K right the 64 K into the compressed area and 128 K will now become free inside of the main memory of course once it's been written to that compressed memory it can't be accessed it's like a zip file you can't actually access it now you actually have to write uncompress it expand it and then write it back into main memory if you want to use it so this is the equivalent of swapping out and swapping back again and if that piece of memory is needed then what the kernel will do it'll actually take that compressed version expand it and put it back somewhere into main memory now there is a thing that doesn't happen on lit on Android with Linux but it can happen on Linux and that is where it's forever writing things out to the disk and then reading things back in again but they do make room for the thing it's reading back in it has to write something else back out and you kind of get this cycle of it constantly reading and writing from the swap file and that's called thrashing now Android doesn't have a problem with thrashing because when it runs out of swap space and when it gets that point where it can't write more things to the swap file it just starts killing processes so they say when you've started an app it becomes a process and when it doesn't have enough free memory and when it can't get any more memory from using the swap file what it will do is it will look down that list of memory and it will pick an app that you haven't used for a while or an app that hasn't kind of been executed recently that's still in memory and it kills it and of course in killing it it frees up all the memory that app was using maybe 300 megabytes 500 megabytes whatever that app is it's gone now and that memory now can be used for a new app or for an existing app to expand its memory footprint now that might sound a bit brutal because the app kind of just gets you know chopped and out it goes but actually that's the way Android is designed when an app gets moved into the background it knows that it may never come back again it may die after that so it has to save its current state and all apps are written in that way that when they are switching in the background they save their current state so what that means is that when you switch using recent apps to a an app it could still be in memory so it just kind of carries on exactly where it left off or it may have to start again from fresh and once it starts it needs to actually read that state information so that it can work out where it was now sometimes that working out where it was is not seamless because for example if you in the middle playing a game it may not bring you back to exactly the same point it might bring you back to the beginning of that level or something like that so how well the app copes with this kind of reload situation is dependent on the app and the person that wrote that app now then it is also very capable of killing off multiple apps if it needs to find bigger amounts of space if you start running a game you know for example like Need for Speed No Limits then that can take up quite a large chunk of memory and so it'll happily kill out three or four apps from the from the memory but they still stay there in the recent apps list because if you do scroll to the musing reason apps it will just reload the app now this reloading of the app of course can cause a slight user experience problem in that you have to wait again while the app reloads and that's why of course having more ram is better from a user experience point of view but of course this other video that I'm working on is how much ram do you need to actually make that user experience seamless ok so that's about it so you've got an amount of available memory that the kernel can use when you start an app that start to process the protis starts to request memory and Linux hands memory to that process from the available memory there's not enough available memory it can start using the swap file and then if it needs to it'll kill off existing apps to make way for the foreground app the one you've just launched so that it has a space in memory one little note is where--this cried swapping is a little bit simplified because actually the nuances of when it decides to swap some pages of memory out to the Zen RAM is actually a bit more complicated but basically it does it when it's under pressure internal memory pressure to find some space for other apps to run inside of main memory so that's about it my name's gary sims and this is garrix places little follow-on to this video will be one on the angular authority channel about how much ram do you really need it for gig six gigs 8 gigs 10 gigs see what I have to say about that in this follow-up video which is going to be published very soon whatever thing yes I've got new glasses ok that's it so if you like this video please do give it a thumbs up please don't forget to subscribe to this channel and well that's it I'll see you in the next one
Info
Channel: Gary Explains
Views: 60,281
Rating: 4.9652901 out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Linux, Linux Kernel, Virtual Memory, Swapping, Swap Space, ZRAM, Android, Recent Apps, Low Memory Killer, zRAM swap, lowmemorykiller, Android low memory killer daemon, lmkd
Id: LEbKFLbUNzM
Channel Id: undefined
Length: 11min 33sec (693 seconds)
Published: Thu Jan 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.