The free command is without doubt one of the best methods one can find out about their system reminiscence (RAM) consumption and you probably have swap enabled.
However the query is: How are you going to use the free command? Properly, it’s fairly straightforward and doesn’t require any advanced steps.
So on this tutorial, I’ll stroll you thru utilizing the free command with examples together with train questions so that you can apply.
How one can use the free command in Linux
To make use of free or every other command, it’s a good suggestion to start out with the command syntax, and I am going to begin with the identical.
This is the syntax for the free command:
free [options]
You might be given a number of choices that are supposed for use within the [options] discipline to alter the default habits of the free command.
Now, enable me to share some generally used choices with the free command:
Possibility
Description
-h
Print data in human-readable kind by adjusting knowledge models in KB, GB, and so on.
-s
Replace the free output after a given seconds of interval.
-t
Reveals the whole quantity of system and swap reminiscence.
-g
Show knowledge in gigabytes.
-m
Print data in megabytes.
-k
Reveals output in kilobytes.
However what should you execute the free command with out choices? This is what you possibly can count on:
Right here,
complete: it signifies the whole quantity of storage.used: reveals the utilized storage by your system.free: the quantity of free reminiscence that’s accessible for brand new processes.shared: quantity of reminiscence utilized by tmpfs (short-term filesystem).buff/cache: represents the sum of the reminiscence utilized by buffers and cache.accessible: it estimates how a lot reminiscence is out there to start out a brand new software with out swapping. It’s a sum of the free reminiscence and part of the buff/cache which may be instantly used.
So, if you’d like every kind of knowledge, simply kind within the command with no flags.
Now, let us take a look at some examples of the free command.
1. Show data in human-readable kind
By default, the free command will show the information in kibibytes which isn’t essentially the most human-readable kind you possibly can have.
That is the place the -h choices come into play which prints values in human-readable kind comparable to in KB or GBs.
free -h
2. Present statistics repeatedly
When you execute the free command, it’s going to solely present you stats of the time while you executed the command. For instance, if I executed the free command at 12:45 then, it’s going to show the stats of that point solely.
So the query is: How one can obtain the same habits like htop which reveals the reside stats? Not precisely the identical, however you possibly can refresh the stats at particular time intervals utilizing the -s flag as proven right here:
free -s <seconds>
For instance, if I wish to refresh the free command each second, then I’ll use the next command:
free -s 1
Urged Learn 📖
3. Outline what number of instances to show the stats
In a earlier instance, I defined how you should use the free command to point out stats repeatedly, however it’s possible you’ll not need it to refresh endlessly as a substitute, just a few instances solely.
For that goal, you should use the -c flag as proven right here:
free -c <times_to_show_stats>
For instance I wish to see the stats 3 instances, then I will probably be utilizing the next:
free -c 3
By default, it’s going to refresh the information each second however you should use the -s flag to specify the interval time:
free -c <times_to_show_stats> -s <seconds>
For instance, right here, if I wish to show stats 3 instances with intervals of two seconds, then I will probably be utilizing the next command:
free -c 3 -s 2
4. Specify the output datatype
Whereas for many customers, utilizing the -h flag to show knowledge in human-readable kind would get the job performed, however what if you wish to specify the information kind by your self?
Properly, you possibly can specify the datatype utilizing the next flags:
Flag
Description
–kilo
Show reminiscence in kilobytes.
–mega
Show reminiscence in megabytes.
–giga
Show reminiscence in gigabytes.
–tera
Show reminiscence in terabytes.
You should utilize the above flags within the following method:
free –<kilo/mega/giga/tera>
For instance, if I want to show reminiscence stats in megabytes, then, I’ll use the next:
free –mega
5. Get the sum of bodily and swap reminiscence
By default, the free reminiscence will present the row of bodily and swap reminiscence, however what if you wish to get the sum of them?
Properly, you should use the -t flag:
free -t
Train questions 👨💻
Listed here are some questions that you could apply to get higher at utilizing the free command:
Print reminiscence stats repeatedly for 3 instances, with an interval of three seconds in between.Get the sum of bodily and swap reminiscence however in human-readable kind.Redirect the output of the free command within the textual content file. How one can print just one column from the output of the free command? (Trace: use awk command and specify column quantity).
When you have any doubts relating to the questions, you possibly can attain out to us in our neighborhood discussion board or depart a remark right here.
Wrapping Up
Each single command is useful in somehow. The free command additionally has one such underrated use-case to get helpful data.
In case you are simply getting began with a Linux terminal, we made a collection for customers such as you who’re desperate to be taught however cannot discover a place to start out:
💬 I hope you discover this information helpful! Please let me know your ideas on what ought to I cowl subsequent?