128
Cache's Explained

  1. elanglois Lollipop May 1, 2018

    elanglois, May 1, 2018 :
    TL;DR See Diagnostic Tests here


    Cache

    So, what's a "cache" and why would I clear one? The purpose of this thread 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 "oneplus.com" to an IP address (35.168.72.102). 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 (OnePlus) 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, and the latter is specific to Oneplus. 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. Oneplus's OOS uses a similar trick to save a copy of a process in an already loaded state, just before it displays it's interface. Oneplus calls it an "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.

    Updates
    I've just converted from Vertex/Nougat to OOS/Oreo (OpenBeta). I used a backup program (custom) that saved all my APKs and the data and wiped everything else, restoring all the APKs and data using root access, but none of the caches (since it would require a lot of space to back it all up, and the ART cache will change from Nougat to Oreo anyway). It specifically avoids saving the GMS id so that notifications continue to work. There have been no crashes or other issues. All data was saved. A couple apps needed to be downloaded from Google Play so that the license key could be properly installed, but this went quickly.

    Had I just done a simpler update, I would have just flashed it and waited for the system to fix itself on its own. Only wipe things if you get bootloops or other problems. Always wait for a problem to surface before you start nuking things. Wiping things without cause always makes things worse!

    When you have recently performed a major update, the ART cache is invalid and all the apps need to be converted and optimized fresh. This is going to slow your device a bit and cause a noticeable drain on your battery. Every major update you'll see everyone complaining that the new update is crap and the old version runs great, and how it was so much better. Now that Oreo is out, Nougat is great and Oreo is crap. How did Nougat become great? When Nougat was released, everyone said Nougat was crap and Marshmallow was great. And every time people complain about the drain, someone else will tell them to wipe caches. What does that do? All the progress your device has made will be erased!

    Conclusion
    Stop wiping caches and just USE your device. With any new update, make sure the settings are configured (keep pocket mode on or your fingerprint sensor will read your pocket as a finger and thrash your battery looking for a fingerprint) and just let the device run its course. Check the battery thread for more info.
     
    Last edited: May 5, 2018

    #1
  2. elanglois Lollipop May 5, 2018

    Stickied Post
    elanglois, May 5, 2018 :
    TL;DR
    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.
     

    #30
    ironass, Spetch, p51d007 and 8 others like this.
  3. dsmonteiro Community Consultant Community Expert May 1, 2018

    dsmonteiro, May 1, 2018 :
    Really thorough thread! Kudos!
     

    #2
  4. idkwhoiam322 Marshmallow May 1, 2018

    idkwhoiam322, May 1, 2018 :
    That's a good read , will add to my thread ;)
    Wow, that was extremely detailed!! Thanks a ton for your effort and sharing your knowledge :D
     
    Last edited: May 1, 2018

    #3
  5. G_Sai_Raina_syku Jelly Bean May 1, 2018

    G_Sai_Raina_syku, May 1, 2018 :
    really amazing and detailed post .
    Read it once but will read it again just because I enjoyed it and got to learn few new things.
    Thanks for a great thread among an ocean of redundant threads
     

    #4
  6. Nick 59 Lollipop May 1, 2018

    Nick 59, May 1, 2018 :
    Excellent post, thank you.
    Problem will be getting people to read it but link saved and I'll certainly be pointing people to this thread.
    Great job!
     
    Last edited: May 1, 2018

    #5
  7. Cheetosdust Starting Point Expert May 1, 2018


    #6
  8. Deepak.Si KitKat May 1, 2018

    Deepak.Si, May 1, 2018 :
    Something knowledgeable to read after a long time..thank you for your time and effort.
     

    #7
  9. rahulsharma10771 Gingerbread May 1, 2018


    #8
    elanglois likes this.
  10. rahulsharma10771 Gingerbread May 1, 2018


    #9
  11. Deactivated User May 1, 2018


    #10
    elanglois likes this.
  12. elanglois Lollipop May 1, 2018

    elanglois, May 1, 2018 :
    Here is the Battery Thread for any battery life issues, tips, and tricks.
    https://forums.oneplus.com/threads/op3-3t-battery-and-heating-tips-nougat-oreo.542075/

    Personally, I use Greenify, but set to aggressive (I don't mind late messages if my phone is dozing), and shallow hibernation, with root working mode. I also use blu_spark's amazing kernel with the blu_active governor rather than interactive. It's faster with less battery drain and very comparable to EAS (which is not yet Oreo-ready). Then, I have 3C (pro version) to edit the startup apps so I can turn off a lot of the cruft - I don't need notifications from games, Google plus, or YouTube so I just stop all that mess from loading at boot.
     

    #11
  13. elanglois Lollipop May 1, 2018

    elanglois, May 1, 2018 :
    Now added this link at end of the OP as well so others can find it easier.
     

    #12
  14. labib123 Froyo May 1, 2018

    labib123, May 1, 2018 :
    I was just googling this! What a coincidence. Thank you for your elaborate explaination
     

    #13
    elanglois likes this.
  15. meatandy Nougat May 1, 2018

    meatandy, May 1, 2018 :
    You deserve a pin on your thread Mr. E.

    Nice work .
     

    #14
  16. Loveit Jelly Bean May 1, 2018

    Loveit, May 1, 2018 :
    Great thread and a very informative read. Of course I already can't remember most of it so will have another read later on.
     

    #15
  17. the_o2 Lollipop May 1, 2018

    the_o2, May 1, 2018 :
    Nice effort and beautiful explanation, I had the confusion between Cleaning cache Partition form recovery and the DALVIK cache.
     

    #16
  18. SoniaB Nougat Senior Moderator May 1, 2018

    SoniaB, May 1, 2018 :
    Great thread.
    In depth explanation.
    Thread pinned for visibility [​IMG]
     

    #17
  19. the_o2 Lollipop May 1, 2018

    the_o2, May 1, 2018 :
    Once I have problem with my Moto Z play after the OTA, After 2 or 3 days of having problem I read this somewhere

    The cache partition contains most of the temporary files, logs and remembered bits from the various applications on your Android device. They can also be left over files from before your last Android update. If that happens your device could be trying to use stored files that are not meant for your current version of Android. Think of it the same way you think of your browser cache. Just like a browser cache, sometimes those stored bits cause more problem than they solve and so it is a good idea to delete the cache periodically.

    And after the clearing the cache partition from recovery my phone started working like normal.
    So I think it is recommended to clear the cache partition after each OTA updates.
    Clearly me if I'm wrong.
     

    #18
    MKT_IRFAN3735 and Nick 59 like this.
  20. MKT_IRFAN3735 Ice Cream Sandwich May 1, 2018

    MKT_IRFAN3735, May 1, 2018 :
    Good Job man [e]1f44f[/e]
    Today onwards I'll not go to clear cache
    but when i want to clear cache ,answer me simply plz¿
     

    #19
  21. elanglois Lollipop May 1, 2018

    elanglois, May 1, 2018 :
    Cache partition does have logs, but none that your apps can access in any way. Your issues could have been from the update not recognizing that the ART cache was invalid (old). This would have shown as multiple apps crashing on start. Was that the issue? What you cleared that fixed it was the ART cache. Confusion arises because stock recovery clears both the cache partition and ART cache at once as if they are the same.

    Don't clear it on every update. Only if you have issues that would be caused by the cache (apps crashing).
     

    #20
    Shivang Joshi and the_o2 like this.