The free command is one of the easiest ways one can learn about their system memory (RAM) consumption and if you have swap enabled.
But the question is: How can you use the free command? Well, it is pretty easy and does not require any complex steps.
So in this tutorial, I will walk you through using the free command with examples along with exercise questions for you to practice.
How to use the free command in Linux
To use free or any other command, it is a good idea to start with the command syntax, and I’ll start with the same.
Here’s the syntax for the free command:
free [options]
You are given multiple options which are supposed to be used in the [options]
field to change the default behavior of the free command.
Now, allow me to share some commonly used options with the free command:
Option | Description |
---|---|
-h |
Print information in human-readable form by adjusting data units in KB, GB, etc. |
-s |
Update the free output after a given seconds of interval. |
-t |
Shows the total amount of system and swap memory. |
-g |
Display data in gigabytes. |
-m |
Print information in megabytes. |
-k |
Shows output in kilobytes. |
But what if you execute the free command without options? Here’s what you can expect:
Here,
total
: it indicates the total amount of storage.used
: shows the utilized storage by your system.free
: the amount of free memory that is available for new processes.shared
: amount of memory used by tmpfs (temporary filesystem).buff/cache
: represents the sum of the memory used by buffers and cache.available
: it estimates how much memory is available to start a new application without swapping. It is a sum of thefree
memory and a part of thebuff/cache
which can be immediately used.
So, if you want all kinds of information, just type in the command with no flags.
Now, let’s look at some examples of the free command.
1. Display information in human-readable form
By default, the free command will display the data in kibibytes which is not the most human-readable form you can have.
That’s where the -h
options come into play which prints values in human-readable form such as in KB or GBs.
free -h
2. Show statistics continuously
Once you execute the free command, it will only show you stats of the time when you executed the command. For example, if I executed the free
command at 12:45
then, it will display the stats of that time only.
So the question is: How you can achieve a similar behavior like htop which shows the live stats? Not exactly the same, but you can refresh the stats at specific time intervals using the -s
flag as shown here:
free -s <seconds>
For example, if I want to refresh the free command every second, then I will use the following command:
free -s 1
Suggested Read 📖
3. Define how many times to display the stats
In a previous example, I explained how you can use the free command to show stats continuously, but you may not want it to refresh endlessly instead, a few times only.
For that purpose, you can use the -c
flag as shown here:
free -c <times_to_show_stats>
Let’s say I want to see the stats 3 times, then I will be using the following:
free -c 3
By default, it will refresh the data every second but you can use the -s
flag to specify the interval time:
free -c <times_to_show_stats> -s <seconds>
For example, here, if I would like to display stats 3 times with intervals of 2 seconds, then I will be using the following command:
free -c 3 -s 2
4. Specify the output datatype
While for most users, using the -h
flag to display data in human-readable form would get the job done, but what if you want to specify the data type by yourself?
Well, you can specify the datatype using the following flags:
- How to build a website with WordPress and what are the best plugins to use Building a website with WordPress is an excellent choice due to its versatility, ease of use, and a vast array of plugins that enhance functionality. Here’s a comprehensive guide to building a WordPress website, along with recommendations for the best plugins
- Top WordPress Plugins for Managing Ads and Monetizing Your Website Effectively: Why is Ads Management Important for Website Monetization? Strategic ad placement throughout the website enables publishers to maximize ad revenue while ensuring a positive user experience. The positioning of ads is critical in capturing users’ attention without being intrusive or disruptive. By understanding user behavior and preferences, publishers can make informed decisions regarding ad placement to ensure that the ads are relevant and engaging.
- Top Directory Plugins for WordPress to Create Professional Listings and Directories: If you are interested in establishing professional listings and directories on your WordPress website, the following information will be of value to you. This article will present the top directory plugins available for WordPress, which include GeoDirectory, Business Directory Plugin, Sabai Directory, Connections Business Directory, and Advanced Classifieds & Directory Pro.
- The Most Important Stages and Plugins for WordPress Website Development: Developing a WordPress website requires careful planning, execution, and optimisation to ensure it is functional, user-friendly, and effective. The process can be broken into key stages, and each stage benefits from specific plugins to enhance functionality and performance. Here’s a detailed guide to the most important stages of WordPress website development and the essential plugins for each stage.
- .org vs .com: A Top Guide to the Differences in Domain Extension
When you set up a website for a business or a non-profit organisation, you might think the most important part of the address is the actual name. But the domain extension (the bit that comes after the dot) is just as important for telling people what your site is all about. - The Best WordPress Plugins for Image Optimization to Improve Load Times and SEO. The pivotal element lies in image optimization. This discourse delves into the significance of image optimization for websites and its impact on load times. Furthermore, we will delve into the advantages of leveraging WordPress plugins for image optimization, such as streamlined optimization processes, enhanced SEO, expedited load times, and an enriched user experience.
- What is a data center or Internet data center? The term “data center” has become very common due to the role it plays in many of our daily activities. Most of the data we receive and send through our mobile phones, tablets and computers ends up stored in these data centers — which many people refer to as “the Cloud”, in a more generic way.
Flag | Description |
---|---|
--kilo |
Display memory in kilobytes. |
--mega |
Display memory in megabytes. |
--giga |
Display memory in gigabytes. |
--tera |
Display memory in terabytes. |
You can use the above flags in the following manner:
free --<kilo/mega/giga/tera>
For example, if I wish to display memory stats in megabytes, then, I will use the following:
free --mega
5. Get the sum of physical and swap memory
By default, the free memory will show the row of physical and swap memory, but what if you want to get the sum of them?
Well, you can use the -t
flag:
free -t
Exercise questions 👨💻
Here are some questions that you can practice to get better at using the free command:
- Print memory stats continuously for 3 times, with an interval of 3 seconds in between.
- Get the sum of physical and swap memory but in human-readable form.
- Redirect the output of the free command in the text file.
- How to print only one column from the output of the free command? (Hint: use awk command and specify column number).
If you have any doubts regarding the questions, you can reach out to us in our community forum or leave a comment here.
Wrapping Up
Every single command comes in handy in one way or the other. The free command also has one such underrated use-case to get useful information.
If you are just getting started with a Linux terminal, we made a series for users like you who are eager to learn but can’t find a place to start:
💬 I hope you find this guide useful! Please let me know your thoughts on what should I cover next?