Original topic:

Caches Explained

(Topic created on: 07-27-2020 03:21 PM)
560 Views
immi007
Expert Level 5
Options
Tech Talk

Cache


So, what's a "cache" and why would I clear one? The purpose of this post is to try and demystify what this is all about so that people can make better informed decisions about their devices.

(Please link here every time someone says to clear a cache!)

First, a cache (pronounced like "cash") is a temporary storage area that is faster than a longer-term storage area. The first time you fetch the data, it will be from the long-term, but slower storage, and from then on, the system fetches data from the faster storage. There are many different caches in an Android system, and they are all used for different things. We'll start at the outer-most, closest to the network.


Data Caches


Every app has it's own personal cache. It can store whatever it wants in here. Generally, this is going to be files fetched from the Internet, but it could also be things generates from other sources, such as thumbnails. A thumbnail is generated from the original image. It takes time (and CPU cycles, and thus precious mA from your battery) to generate a thumbnail, but they can be recreated any time. Because the cached data might contain personal information, each application gets its own cache that only it can see. That is why Android separates clearing an app's cache (which is regenerated the next time the app is used) from clearing the data (which would require you to set up the app again with new data in the app's database). In general, you shouldn't need to clear an app's cache. Even if something goes wrong, it's not likely to be the cache. If you are really low on space, you can clear ALL the cached data of all apps from Settings.


DNS Cache


This section is mainly for completeness since it happens under the covers where you can't see or control it, but DNS converts a name, such as "samsung.com" to an IP address (Ex: zz.168.zz.zz ). This can require multiple network requests to do. As you load a page, it might have multiple links (consider the number of images, css files, javascript files and such that are on just one web page) using the same domain name. Looking these up all the time would be very slow, so DNS requests are usually cached. Chrome browser has its own DNS lookup system, and its own DNS cache would can be cleared if needed (such as if you have a home network and change an IP address).

 

Cache partition


Your device has a special partition called "cache", but it doesn't hold any of the previously mentioned caches. I think it used to do more, but these days its primarily used for two purposes. First, when doing an automated system update, a script file needs to be given to the recovery system to tell it what you want it to do. Without it, you'll get a menu. The script is generally placed on the cache partition. Often, debug logs about the update go here as well. After all, if you are updating, you may be trashing all other locations on the phone! The other thing that can go here is Play Store updates. Google wanted to make sure that you can always update Play, even if your system is full. However, it needs to put the new copy somewhere safe before it deletes the old one. The cache partition is where these critical updates are supposed to go.


ART Cache


This area is a folder on your /data partition. Android is designed to run on any CPU, so APK files don't contain code for your CPU, but rather code for a virtual machine called "ART" (formerly Dalvik which is why this is sometimes called the "dalvik cache"). When an APK is loaded, the system converts the ART code into code for the local CPU. If your ARM/Snapdragon chip speaks Spanish, then an Intel x86 or Atom CPU speaks German, and ART is Russian. We have to translate it. It even optimizes the code for the capabilities in your CPU. On Windows, every application has to be rewritten to take advantage of new CPU features. On Android, we run on everything. This obviously takes time and significant resources. Each Android version changes the process somewhat (Lollipop would do this on boot ... remember how long that took?) to balance things out. Once we get some code generated for our local CPU, the ART Cache is where it goes. Nougat and higher will even optimize certain portions more aggressively over time based on how often certain code is executed. This means your ART cache is constantly changing, making your system faster over time.

When you erase caches from recovery, this is the cache that is deleted. While your CPU doesn't change, the location of various libraries in memory sometimes changes when upgrading from one major Android version to another, or from one developer's ROM to another. Most minor updates just change things that don't affect the memory layout in any way whatsoever. Android keeps track of which Android version generated the cache and will automatically clear out stale data. You don't have to do it yourself. The manufacturer (Samsung) might make some odd changes in an update, and if during testing it's found that this breaks the ART cache, they should include a common to wipe this cache in the script in charge of updating.

Do not wipe this cache randomly. If you find multiple applications (nearly all of them) are crashing/force-closing when you try to run them, then wiping the ART cache is likely your solution. Also, installing an OS writes to /system (the ROM) and /boot (the linux kernel), not /data. This means that when someone says to wipe caches before and after flashing, they are making you wipe an already wiped cache. It's totally redundant. Think twice before following instructions of someone that doesn't know that!

 


Zygotes and Embryos


I'll touch on this briefly for completeness, but these are specific to Android. Linux contains a system call called "fork", which splits the current process in two. The new child can then do whatever it wants. Android uses this to create a process called a "zygote", a baby that will become the Android apps when they are run. This zygote already has all the various libraries that the app will need already loaded so that process creation is much much faster. The zygote splits and the new child process then loads the new code from the APK, becoming a new Android process.

"Embryo" It's kept around until the system needs the RAM for something else and it makes running the app a second time incredibly fast since the slow storage is nearly untouched.


Disk Cache


While Android doesn't have an actual disk, the flash storage (16,64,128 or 256GB) is slower than regular RAM (4,6, or 8GB). Regular RAM is gone when you reboot your phone, but it's fast! Luckily, Linux uses all of the spare RAM you've got as disk cache. Basically, when something is loaded from disk into RAM, Linux keeps that RAM around in case you need that section of storage again. The next time you ask for that storage, you get the RAM copy so its faster. This happens automatically.

 


CPU Cache


Your CPU only works on "registers", special locations that run at the full speed of the processor. An ARM core has 16 of these, of a size equal to the number of "bits". A 32 bit core has 32 bit registers (4 bytes), and a 64 bit core has 8 byte registers. RAM must be accessed across a slower IO bus. To speed up access to system RAM, all modern CPUs have an on-die cache. This can sometimes be in multiple layers depending on if it's on-die or just on-chip, etc. You'll see it as L1, L2, or even L3 cache. This is much faster and more expensive than system RAM, but not as fast as more registers. This is all controlled through hardware and the Linux kernel, so you don't need to know much about it other than it has nothing to do with the other caches.

 


Factory Reset


This isn't a cache, but I thought I'd mention what happens when you factory reset your device. Basically, all of your /data partition is wiped out. Your cache partition is also wiped. Your ART cache is gone, your apps, and your settings. Your settings are nothing more than app data for system apps. In some cases, such as major updates, the old settings are changed in a way that isn't compatible or the format used by the SQL database library changes. In the former case, Android lets the app know what the old version of the data is, and tells the app to convert it. A properly written app (including system apps), just gets the command to convert the data and does so. In the latter case, the update should convert the data. Factory resets shouldn't be required, but we all know things don't always go as planned.

3 Comments
immi007
Expert Level 5
Tech Talk

If your problem is ...

* Random Crashes of most apps
Clear ART/Dalvik cache from recovery

* A few (or one) app crashes
Clear all apps data from App Info screen or uninstall app and reinstall from Play store.

* Bootloops
First, wait longer than normal after an update. It may not be looping. Second, get rid of custom overlays (themes) and Magisk modules before applying an update. Bootloops can be due to apps crashing, so try clearing cache. If all else fails, wipe data and flash ROM again, let your phone install apps from Google backup at setup screen.

* PlayStore misbehaves
Clear Play store and Play services data from App Info screens, then immediately reboot to recovery and wipe cache partition

* Battery Drain
Make sure pocket mode is on, consider turning OFF double-tap power for camera, delete suspect apps, and read the battery thread (link at end of first post). Do NOT clear caches.

* Slow phone after upgrade
Just Wait. Clearing caches makes it worse

* Other problems
If it's not crashing, it's almost certainly not a cache issue. Look in your logs for a clue. Clearing caches is not likely to fix it and can slow down the device

Aashish
Expert Level 4
Tech Talk
Good post.
immi007
Expert Level 5
Tech Talk
Thank you 😃
0 Likes