How To Free Memory Linux Command

When running a Linux system, you may at times find yourself in a situation where your system memory is full, and you need to free up some space. This is particularly important in handling resource-intensive tasks. This blog post will guide you on how to free memory using Linux commands.

Understanding your Memory Usage

Before you proceed with freeing memory, it’s crucial to first check your system’s memory usage. The free command provides this information. When executed, it displays the total amount of free and used physical and swap memory in the system.

    free -h
    

Freeing Up the Memory

The two primary ways to free up memory in Linux are by using either the sync command or the echo command. Here is how to use them:

1. The Sync Command

The sync command, when combined with the drop_caches command, can help to free up used memory. Essentially, the sync command flushes the file system buffer, and the drop_caches command clears out the cache.

    sync; echo 3 > /proc/sys/vm/drop_caches
    

You need to run this command as root or with sudo privileges because of its potential to affect the system’s processes.

2. The Echo Command

The echo command can also be used to free up memory. This command allows you to control how much memory you would like to free up.

    echo 1 > /proc/sys/vm/drop_caches
    

In this case, 1 stands for freeing up the page cache. You can replace it with 2 to free up dentries and inodes, or 3 to free up the page cache, dentries, and inodes.

Conclusion

Linux provides various commands that can help you manage your system’s memory effectively. The key is to understand the memory usage in your system and then use the appropriate commands to free up memory as and when needed. Remember, freeing up memory can at times disrupt running processes, so always ensure you know what you’re doing before executing these commands.