{"id":2786,"date":"2024-12-03T04:06:56","date_gmt":"2024-12-03T04:06:56","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=2786"},"modified":"2024-12-05T04:25:38","modified_gmt":"2024-12-05T04:25:38","slug":"how-to-clear-ram-clear-cache","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/linux\/how-to-clear-ram-clear-cache\/","title":{"rendered":"How to clear RAM &#8211; Clear Cache &#8211; Drop Cache"},"content":{"rendered":"\n<p>The &#8220;1, 2, 3&#8221; cache clear method in Linux refers to clearing different types of caches managed by the operating system. Each cache type is managed differently and cleared by writing a specific value to the file <code>\/proc\/sys\/vm\/drop_caches<\/code>.<\/p>\n\n\n\n<h3>Cache Types:<\/h3>\n\n\n\n<ol><li><strong>PageCache (Value: <code>1<\/code>)<\/strong>:<ul><li>PageCache stores file contents that are cached in memory to reduce disk I\/O.<\/li><\/ul><\/li><li><strong>Dentries and Inodes (Value: <code>2<\/code>)<\/strong>:<ul><li>Dentry is a cache of directory entries, and inodes store file metadata.<\/li><\/ul><\/li><li><strong>PageCache + Dentries + Inodes (Value: <code>3<\/code>)<\/strong>:<ul><li>Clears both PageCache and Dentry\/inode caches.<\/li><\/ul><\/li><\/ol>\n\n\n\n<h3>Steps to Clear Cache with Examples:<\/h3>\n\n\n\n<h4><strong>1. Check Current Memory Usage<\/strong><\/h4>\n\n\n\n<p>Use the <code>free -h<\/code> command to view current memory and cached memory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">free -h\n<\/pre>\n\n\n\n<p>Look under the &#8220;buff\/cache&#8221; column to see the cache usage.<\/p>\n\n\n\n<h4><strong>2. Clear Cache<\/strong><\/h4>\n\n\n\n<p>Use the following commands to clear caches. Root privileges are required, so prefix with <code>sudo<\/code>:<\/p>\n\n\n\n<h5>Clear PageCache:<\/h5>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo sh -c \"echo 1 > \/proc\/sys\/vm\/drop_caches\"\n<\/pre>\n\n\n\n<h5>Clear Dentry and Inode Cache:<\/h5>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo sh -c \"echo 2 > \/proc\/sys\/vm\/drop_caches\"\n<\/pre>\n\n\n\n<h5>Clear Both PageCache and Dentry\/Inode Cache:<\/h5>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo sh -c \"echo 3 > \/proc\/sys\/vm\/drop_caches\"\n<\/pre>\n\n\n\n<h4><strong>3. Check Memory Usage Again<\/strong><\/h4>\n\n\n\n<p>Run the <code>free -h<\/code> command again to verify that the cache has been cleared.<\/p>\n\n\n\n<h3>Example Workflow:<\/h3>\n\n\n\n<ol><li><strong>Check Memory Before Clearing:<\/strong> <code>free -h<\/code> Output: <code>total used free shared buff\/cache available Mem: 15Gi 4.0Gi 2.0Gi 0.5Gi 9Gi 10Gi Swap: 2Gi 0B 2Gi<\/code><\/li><li><strong>Clear All Caches (PageCache, Dentries, and Inodes):<\/strong> <code>sudo sh -c \"echo 3 &gt; \/proc\/sys\/vm\/drop_caches\"<\/code><\/li><li><strong>Check Memory After Clearing:<\/strong> <code>free -h<\/code> Output: <code>total used free shared buff\/cache available Mem: 15Gi 4.0Gi 7.0Gi 0.5Gi 4Gi 11Gi Swap: 2Gi 0B 2Gi<\/code><\/li><\/ol>\n\n\n\n<p>Notice how the &#8220;buff\/cache&#8221; column decreases.<\/p>\n\n\n\n<h3>Best Practices<\/h3>\n\n\n\n<ul><li>Clearing caches isn&#8217;t typically necessary because Linux uses unused memory to cache files and automatically clears it when needed.<\/li><li>Use this method only for debugging or specific needs (e.g., testing performance under no cache conditions).<\/li><\/ul>\n\n\n\n<p>Would you like to try this on your system now? Let me know if you need additional help!<\/p>\n\n\n\n<p>Let&#8217;s break down <strong>PageCache<\/strong>, <strong>Dentries<\/strong>, and <strong>Inodes<\/strong> with simple explanations and commands you can use on your Ubuntu system to see how they work.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>1. <strong>PageCache<\/strong><\/h3>\n\n\n\n<ul><li><strong>What it is:<\/strong><ul><li>PageCache stores file data read from disk in memory to speed up future reads.<\/li><li>If a file is accessed again, the system reads it from memory instead of the slower disk.<\/li><\/ul><\/li><li><strong>Example:<\/strong><ol><li><strong>Create a Large File:<\/strong> <code>dd if=\/dev\/zero of=testfile bs=1M count=100<\/code> This creates a file <code>testfile<\/code> of 100 MB size.<\/li><li><strong>Clear Cache:<\/strong> <code>sudo sh -c \"echo 3 &gt; \/proc\/sys\/vm\/drop_caches\"<\/code><\/li><li><strong>Read the File (Populates PageCache):<\/strong> <code>time cat testfile &gt; \/dev\/null<\/code> The first read is slow because it loads data from disk.<\/li><li><strong>Read the File Again (From PageCache):<\/strong> <code>time cat testfile &gt; \/dev\/null<\/code> The second read is faster as it fetches data from memory (PageCache).<\/li><li><strong>Verify Cache Usage:<\/strong> Check memory usage using: <code>free -h<\/code><\/li><\/ol><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>2. <strong>Dentries<\/strong><\/h3>\n\n\n\n<ul><li><strong>What it is:<\/strong><ul><li>A <strong>dentry<\/strong> (directory entry) cache stores metadata about directories, such as filenames and their locations in the filesystem.<\/li><li>This speeds up directory traversals and file lookups.<\/li><\/ul><\/li><li><strong>Example:<\/strong><ol><li><strong>Clear Cache:<\/strong> <code>sudo sh -c \"echo 3 &gt; \/proc\/sys\/vm\/drop_caches\"<\/code><\/li><li><strong>List a Large Directory (Populates Dentry Cache):<\/strong> <code>ls -R \/usr &gt; \/dev\/null<\/code><\/li><li><strong>List the Directory Again (From Dentry Cache):<\/strong> <code>time ls -R \/usr &gt; \/dev\/null<\/code> The second run will be faster because the directory structure is cached in memory.<\/li><\/ol><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>3. <strong>Inodes<\/strong><\/h3>\n\n\n\n<ul><li><strong>What it is:<\/strong><ul><li>An <strong>inode<\/strong> is a data structure storing metadata about a file (e.g., permissions, owner, size).<\/li><li>Inode caching stores this metadata to reduce disk reads.<\/li><\/ul><\/li><li><strong>Example:<\/strong><ol><li><strong>Find Inodes Used by the System:<\/strong> <code>df -i<\/code> This shows the inode usage for each mounted filesystem.<\/li><li><strong>Create Many Files (Populates Inode Cache):<\/strong> <code>mkdir testdir for i in {1..10000}; do touch testdir\/file$i; done<\/code><\/li><li><strong>Clear Cache:<\/strong> <code>sudo sh -c \"echo 2 &gt; \/proc\/sys\/vm\/drop_caches\"<\/code><\/li><li><strong>List Files (Rebuilds Inode Cache):<\/strong> <code>time ls testdir &gt; \/dev\/null<\/code><\/li><li><strong>List Files Again (From Inode Cache):<\/strong> <code>time ls testdir &gt; \/dev\/null<\/code> The second run will be faster due to inode caching.<\/li><\/ol><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>Summary of Commands:<\/h3>\n\n\n\n<ul><li><strong><code>free -h<\/code><\/strong>: Check memory usage (PageCache in &#8220;buff\/cache&#8221;).<\/li><li><strong><code>df -i<\/code><\/strong>: Check inode usage for filesystems.<\/li><li><strong><code>echo [1|2|3] &gt; \/proc\/sys\/vm\/drop_caches<\/code><\/strong>: Clear caches.<\/li><\/ul>\n\n\n\n<p>The command <code>dd if=\/dev\/zero of=testfile bs=1M count=100<\/code> is a Linux command used to create a file filled with zeros. Here&#8217;s a breakdown of each part:<\/p>\n\n\n\n<h3>Command Breakdown:<\/h3>\n\n\n\n<ol><li><strong><code>dd<\/code><\/strong>:<ul><li>A low-level command-line utility used for copying and converting data between files or devices.<\/li><\/ul><\/li><li><strong><code>if=\/dev\/zero<\/code><\/strong>:<ul><li><code>if<\/code> stands for &#8220;input file.&#8221;<\/li><li><code>\/dev\/zero<\/code> is a special file in Linux that produces a continuous stream of null bytes (zeros).<\/li><\/ul><\/li><li><strong><code>of=testfile<\/code><\/strong>:<ul><li><code>of<\/code> stands for &#8220;output file.&#8221;<\/li><li><code>testfile<\/code> is the name of the file where the data (zeros) will be written.<\/li><\/ul><\/li><li><strong><code>bs=1M<\/code><\/strong>:<ul><li><code>bs<\/code> stands for &#8220;block size.&#8221;<\/li><li>This sets the size of each block of data to 1 Megabyte (MB).<\/li><\/ul><\/li><li><strong><code>count=100<\/code><\/strong>:<ul><li>Specifies the number of blocks to copy.<\/li><li>In this case, 100 blocks of 1 MB each.<\/li><\/ul><\/li><\/ol>\n\n\n\n<h3>What the Command Does:<\/h3>\n\n\n\n<ul><li>It writes <strong>100 MB<\/strong> (1 MB \u00d7 100 blocks) of zero-filled data to a file named <code>testfile<\/code>.<\/li><\/ul>\n\n\n\n<h3>Example Output:<\/h3>\n\n\n\n<p>When you run the command, you&#8217;ll see something like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">100+0 records in\n100+0 records out\n104857600 bytes (105 MB) copied, 0.123456 s, 847 MB\/s\n<\/pre>\n\n\n\n<p>This means:<\/p>\n\n\n\n<ul><li><code>100+0 records in<\/code>: 100 blocks read from the input file (<code>\/dev\/zero<\/code>).<\/li><li><code>100+0 records out<\/code>: 100 blocks written to the output file (<code>testfile<\/code>).<\/li><li><code>104857600 bytes<\/code>: Total bytes written (100 MB).<\/li><li><code>0.123456 s<\/code>: Time taken to execute the command.<\/li><li><code>847 MB\/s<\/code>: Write speed.<\/li><\/ul>\n\n\n\n<h3>Verify the File:<\/h3>\n\n\n\n<ul><li><strong>Check the file size:<\/strong> <code>ls -lh testfile<\/code> You should see the size as <code>100 MB<\/code>.<\/li><li><strong>Inspect the file contents:<\/strong> <code>hexdump -C testfile | head<\/code> This will display the file&#8217;s contents in hexadecimal format, and you&#8217;ll see it filled with <code>00<\/code> (zeros).<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Why clearing dentries does not affect memory a lot <\/p>\n\n\n\n<p>The behavior you&#8217;re observing can be explained by how Linux handles <strong>PageCache<\/strong> and <strong>Dentries\/Inodes<\/strong> caching.<\/p>\n\n\n\n<h3>Key Points:<\/h3>\n\n\n\n<ol><li><strong>PageCache (Cleared with <code>echo 1 &gt; \/proc\/sys\/vm\/drop_caches<\/code>):<\/strong><ul><li>Clearing PageCache removes cached file data but does <strong>not<\/strong> clear dentries and inodes.<\/li><li>If you only clear the PageCache, Linux has to read file contents from disk, but dentry and inode information might still be available in memory.<\/li><\/ul><\/li><li><strong>Dentries and Inodes (Cleared with <code>echo 2 &gt; \/proc\/sys\/vm\/drop_caches<\/code>):<\/strong><ul><li>Clearing dentry and inode caches removes directory structure and file metadata information from memory.<\/li><li>If dentry and inode caches are cleared, listing directories or performing file operations becomes slower because the system must rebuild this information by reading from disk.<\/li><\/ul><\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>Your Test Results:<\/h3>\n\n\n\n<ol><li><strong>Before Cache Clear:<\/strong><ul><li>The first <code>ls -R<\/code> command runs quickly (<code>0.343s<\/code>) because both PageCache and dentry\/inode caches are populated.<\/li><\/ul><\/li><li><strong>After Clearing PageCache:<\/strong><ul><li>After running <code>echo 1 &gt; \/proc\/sys\/vm\/drop_caches<\/code>, the second <code>ls -R<\/code> command takes significantly longer (<code>2.756s<\/code>) because:<ul><li>PageCache is cleared, so file contents must be reloaded from disk.<\/li><li><strong>However<\/strong>, dentries and inodes are <strong>not cleared<\/strong>, so some metadata is still available.<\/li><\/ul><\/li><\/ul><\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>Why It Still Takes More Time:<\/h3>\n\n\n\n<p>The additional time comes from the fact that:<\/p>\n\n\n\n<ol><li>After clearing the <strong>PageCache<\/strong>, the system has to read the file contents (or parts of the directory structure) from disk, which is slower than fetching them from memory.<\/li><li>Even though dentries and inodes are still in memory, they depend on PageCache for efficient access to file content. So the system has to do extra work to populate the PageCache again.<\/li><li>Directory traversal involves a combination of <strong>file metadata (dentries\/inodes)<\/strong> and <strong>directory file contents<\/strong> (loaded via PageCache). Clearing just the PageCache impacts this process.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>Solution to Test Dentry Cache Impact:<\/h3>\n\n\n\n<p>To focus solely on <strong>dentry and inode caches<\/strong>, you need to clear them explicitly with:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sh -c \"echo 2 > \/proc\/sys\/vm\/drop_caches\"\n<\/pre>\n\n\n\n<p>Then rerun:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">time ls -R \/usr > \/dev\/null\n<\/pre>\n\n\n\n<p>You\u2019ll notice that this run will also take longer compared to when the dentry cache is intact, but the pattern will differ from clearing the PageCache.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3>Suggested Workflow to Understand All Cache Impacts:<\/h3>\n\n\n\n<ol><li><strong>Baseline:<\/strong> <code>time ls -R \/usr &gt; \/dev\/null<\/code><\/li><li><strong>Clear PageCache:<\/strong> <code>sh -c \"echo 1 &gt; \/proc\/sys\/vm\/drop_caches\" time ls -R \/usr &gt; \/dev\/null<\/code><\/li><li><strong>Clear Dentries and Inodes:<\/strong> <code>sh -c \"echo 2 &gt; \/proc\/sys\/vm\/drop_caches\" time ls -R \/usr &gt; \/dev\/null<\/code><\/li><li><strong>Clear Both PageCache and Dentries\/Inodes:<\/strong> <code>sh -c \"echo 3 &gt; \/proc\/sys\/vm\/drop_caches\" time ls -R \/usr &gt; \/dev\/null<\/code><\/li><\/ol>\n\n\n\n<p>This should give you a clear understanding of how each type of cache contributes to performance.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2024\/12\/image-1.png\"><img loading=\"lazy\" width=\"860\" height=\"754\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2024\/12\/image-1.png\" alt=\"\" class=\"wp-image-2794\"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;1, 2, 3&#8221; cache clear method in Linux refers to clearing different types of caches managed by the operating system. Each cache type is managed differently and cleared by writing a specific value to the file \/proc\/sys\/vm\/drop_caches. Cache Types: PageCache (Value: 1): PageCache stores file contents that are cached in memory to reduce disk [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2786"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2786"}],"version-history":[{"count":4,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2786\/revisions"}],"predecessor-version":[{"id":2795,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2786\/revisions\/2795"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}