{"id":32,"date":"2023-12-15T00:06:08","date_gmt":"2023-12-15T00:06:08","guid":{"rendered":"https:\/\/resellerjungle.com\/articles\/?p=32"},"modified":"2023-12-15T00:22:53","modified_gmt":"2023-12-15T00:22:53","slug":"clear-ram-memory-cache-buffer-and-swap","status":"publish","type":"post","link":"https:\/\/resellerjungle.com\/articles\/clear-ram-memory-cache-buffer-and-swap\/","title":{"rendered":"How to Clear RAM Memory Cache, Buffer and Swap Space on Linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Clearing RAM memory cache, buffers, and swap space on <a href=\"https:\/\/resellerjungle.com\/linux-reseller-hosting\/\" data-type=\"link\" data-id=\"https:\/\/resellerjungle.com\/linux-reseller-hosting\/\">Linux <\/a>can sometimes be necessary for specific purposes like testing, debugging, or troubleshooting performance issues. However, it&#8217;s important to note that clearing these caches might affect system performance temporarily, and in most cases, the Linux kernel is efficient at managing these resources.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here in this article, we will explore how to clear RAM memory cache, buffer, and swap space on a <a href=\"https:\/\/www.squarebrothers.com\/\" data-type=\"link\" data-id=\"https:\/\/www.squarebrothers.com\/\" target=\"_blank\" rel=\"noopener\">Linux <\/a>system to enhance overall performance.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#understanding-ram-memory-cache-buffer-and-swap-space\">Understanding RAM Memory Cache, Buffer, and Swap Space<\/a><ul><li><a href=\"#ram-memory-cache\">RAM Memory Cache<\/a><\/li><li><a href=\"#buffer\">Buffer<\/a><\/li><li><a href=\"#swap-space\">Swap Space<\/a><\/li><\/ul><\/li><li><a href=\"#clearing-cache-buffer-and-swap-space-in-linux\">Clearing Cache, Buffer, and Swap Space in Linux<\/a><ul><li><a href=\"#how-to-clear-ram-memory-cache-in-linux\">How to Clear RAM Memory Cache in Linux?<\/a><ul><li><a href=\"#clearing-page-cache\">Clearing PageCache<\/a><\/li><li><a href=\"#clearing-dentries-and-inodes\">Clearing Dentries and Inodes<\/a><\/li><li><a href=\"#clearing-page-cache-dentries-and-inodes\">Clearing PageCache, Dentries, and Inodes<\/a><\/li><\/ul><\/li><li><a href=\"#how-to-clear-swap-space-in-linux\">How to Clear Swap Space in Linux?<\/a><\/li><\/ul><\/li><li><a href=\"#is-it-advisable-to-free-buffer-cache-and-swap-in-linux\">Is It Advisable to Free Buffer, Cache, and Swap in Linux?<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"understanding-ram-memory-cache-buffer-and-swap-space\">Understanding RAM Memory Cache, Buffer, and Swap Space<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s explore RAM memory cache, buffer, and swap space on a Linux system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ram-memory-cache\">RAM Memory Cache<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>RAM memory cache<\/strong>&nbsp;is a mechanism used by the kernel to keep regularly accessed data. While this boosts system responsiveness, an overloaded cache can lead to the retention of obsolete data, affecting performance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"buffer\">Buffer<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Like&nbsp;<strong>cache<\/strong>, the buffer holds data temporarily but differs in its purpose. Buffers store data being transferred between components like the CPU and hard disk, facilitating smooth communication. However, an excess of buffered data can hinder system speed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"swap-space\">Swap Space<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Swap space<\/strong>&nbsp;is an allocated area on the hard disk that acts as virtual memory when physical RAM is exhausted. While it prevents system crashes due to low memory, it can slow down the system if overused.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"clearing-cache-buffer-and-swap-space-in-linux\">Clearing Cache, Buffer, and Swap Space in Linux<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In certain situations, you may need to clear the&nbsp;<strong>cache<\/strong>,&nbsp;<strong>buffer<\/strong>, or&nbsp;<strong>swap space<\/strong>&nbsp;as explained below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-clear-ram-memory-cache-in-linux\">How to Clear RAM Memory Cache in Linux?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every Linux system has three options to clear the cache without interrupting any processes or services<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"clearing-page-cache\">Clearing PageCache<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1.<\/strong>&nbsp;To clear the&nbsp;<strong>PageCache<\/strong>&nbsp;only, you can use the following command, which will specifically clear the PageCache, helping to free up memory resources.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sync; echo 1 &gt; \/proc\/sys\/vm\/drop_caches<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"20\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1024x20.png\" alt=\"\" class=\"wp-image-33\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1024x20.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-300x6.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-768x15.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1536x30.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1.png 1899w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"clearing-dentries-and-inodes\">Clearing Dentries and Inodes<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2.<\/strong>&nbsp;To clear the&nbsp;<strong>dentries<\/strong>&nbsp;and&nbsp;<strong>inodes<\/strong>&nbsp;only, you can use the following command, which will sync the filesystem and clear both dentries and inodes, improving system performance by releasing cached directory and inode information.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sync; echo 2 &gt; \/proc\/sys\/vm\/drop_caches<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"19\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1024x19.png\" alt=\"\" class=\"wp-image-34\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1024x19.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-300x6.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-768x14.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1536x28.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2.png 1895w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"clearing-page-cache-dentries-and-inodes\">Clearing PageCache, Dentries, and Inodes<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3.<\/strong>&nbsp;To clear the&nbsp;<strong>pagecache<\/strong>,&nbsp;<strong>dentries<\/strong>, and&nbsp;<strong>inodes<\/strong>, you can use the following command, which will sync the filesystem and clear the pagecache, dentries, and inodes, helping to free up memory and improve system performance.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sync; echo 3 &gt; \/proc\/sys\/vm\/drop_caches<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"21\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1024x21.png\" alt=\"\" class=\"wp-image-35\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1024x21.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-300x6.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-768x15.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1536x31.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3.png 1892w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an explanation of each part of the above command:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>sudo<\/code>&nbsp;is used to execute the command as a superuser.<\/li>\n\n\n\n<li>The&nbsp;<code>sync<\/code>&nbsp;will flush the file system buffer.<\/li>\n\n\n\n<li>The&nbsp;<code>\u201c;\u201d<\/code>&nbsp;semicolon is used to separate multiple commands on a single line.<\/li>\n\n\n\n<li>The&nbsp;<code>echo 3 &gt; \/proc\/sys\/vm\/drop_caches<\/code>&nbsp;command is used to drop the page cache, a temporary storage area for recently accessed files.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: The&nbsp;<code>drop_caches<\/code>&nbsp;file controls which type of cached data should be cleared and the values are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1<\/code>&nbsp;\u2013 Clears only the page cache.<\/li>\n\n\n\n<li><code>2<\/code>&nbsp;\u2013 Clears dentries and inodes.<\/li>\n\n\n\n<li><code>3<\/code>&nbsp;\u2013 Clears page cache, dentries, and inodes.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned in the kernel documentation, writing to&nbsp;<strong>drop_caches<\/strong>&nbsp;will clean the cache without killing any application\/service,&nbsp;command echo&nbsp;is doing the job of writing to the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have to clear the disk cache, the first command is safest in enterprise and production as&nbsp;<code>\u201c...echo 1 &gt; \u2026.\u201d<\/code>&nbsp;will clear the&nbsp;<strong>PageCache<\/strong>&nbsp;only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is not recommended to use the third option above&nbsp;<code>\u201c...echo 3 &gt;\u201d<\/code>&nbsp;in production until you know what you are doing, as it will clear&nbsp;<strong>pagecache<\/strong>,&nbsp;<strong>dentries,<\/strong>&nbsp;and&nbsp;<strong>inodes<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-to-clear-swap-space-in-linux\">How to Clear Swap Space in Linux?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To clear swap space, you can use the&nbsp;<strong>swapoff<\/strong>&nbsp;command with the&nbsp;<code>-a<\/code>&nbsp;option, which will disable all swap partitions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapoff -a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, turn it back by running the following command, which will activate all swap partitions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon -a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"is-it-advisable-to-free-buffer-cache-and-swap-in-linux\">Is It Advisable to Free Buffer, Cache, and Swap in Linux?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In general, it is not a good idea to manually free up&nbsp;<strong>Buffer<\/strong>&nbsp;and&nbsp;<strong>Cache<\/strong>&nbsp;in Linux that might be used by the Linux kernel, which is designed to manage these resources efficiently, and manually clearing them can disrupt system performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, there may be rare situations where you need to clear the&nbsp;<strong>Buffer<\/strong>&nbsp;and&nbsp;<strong>Cache<\/strong>, such as if you are experiencing severe memory pressure and cannot free up memory by other means. In these cases, you should proceed with caution and be aware of the potential performance impact.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, clearing swap space in Linux is generally not a routine or advisable practice under normal circumstances.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Clearing RAM memory cache, buffers, and swap space on Linux can sometimes be necessary for specific purposes like testing, debugging,&hellip;<\/p>\n","protected":false},"author":2,"featured_media":45,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":6,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/32\/revisions\/44"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media\/45"}],"wp:attachment":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}