• Follow us

Internet

How to Use the vmstat Command on Linux

Terminal prompt on a Linux desktopFatmawati Achmad Zaenuri/Shutterstock.com

Your Linux or macOS computer is using virtual memory. Discover how it’s affecting your system’s use of physical memory, CPU, and hard disk resources.

What Is Virtual Memory?

Your computer is fitted with a finite amount of physical memory called random access memory (RAM). This RAM needs to be managed by the kernel and shared between the operating system and whatever applications happen to be running. If these combined demands are asking for more memory than is physically installed in your computer, what can the kernel do?

Linux and Unix-like operating systems such as macOS can use space on your hard disk to help them manage memory demands. A reserved area of hard drive space called “swap space” can be used as though it were an extension of RAM. This is virtual memory.

The Linux kernel can write the contents of a block of memory into swap space, and free up that region of RAM for use by another process. The swapped out—also called “paged” out—memory can be retrieved from the swap space and restored to RAM when it is required.

Of course, the speed of access for paged out memory is slower than that of memory held in RAM. And that’s not the only trade-off. Whilst virtual memory does provide a way for Linux to manage its memory demands, using virtual memory places increased burdens elsewhere on the computer.

Your hard drive must perform more reads and writes. The kernel—and hence, the CPU—must do more work as it swaps memory out, swaps memory in, and keeps all the plates spinning to satisfy the memory needs of the different processes.

Linux provides a way for you to monitor all of this activity in the shape of the vmstat command, which reports on virtual memory statistics.

The vmstat Command

If you type vmstat as a command with no parameters, it will show you a set of values. These values are the averages for each of the statistics since your computer was last rebooted. These figures are not a snapshot of the values “right now.”

vmstat

vmstat in a terminal window

A short table of values is displayed.

vmstat output in a terminal window

There are columns headed Procs, Memory, Swap, IO, System, and CPU. The final column (most right-hand column) contains the data relating to the CPU.

vmstat output right-hand columns in a terminal window

Here is a list of the data items in each column.

Proc

r: The number of runnable processes. These are processes that have been launched and are either running or are waiting for their next time-sliced burst of CPU cycles. b: The number of processes in uninterruptible sleep. The process isn’t sleeping, it is performing a blocking system call, and it cannot be interrupted until it has completed its current action. Typically the process is a device driver waiting for some resource to come free. Any queued interrupts for that process are handled when the process resumes its usual activity.

Memory

swpd: the amount of virtual memory used. In other words, how much memory has been swapped out., free: the amount of idle (currently unused) memory. buff: the amount of memory used as buffers. cache: the amount of memory used as cache.

Swap

si: Amount of virtual memory swapped in from swap space. so: Amount of virtual memory swapped out to swap space.

IO

bi: Blocks received from a block device. The number of data blocks used to swap virtual memory back into RAM. bo: Blocks sent to a block device. The number of data blocks used to swap virtual memory out of RAM and into swap space.

System

in: The number of interrupts per second, including the clock. cs: The number of context switches per second. A context switch is when the kernel swaps from system mode processing into user mode processing.

CPU

These values are all percentages of the total CPU time.

us: Time spent running non-kernel code. That is, how much time is spent in user time processing and in nice time processing. sy: Time spent running kernel code. id: Time spent idle. wa: Time spent waiting for input or output. st: Time stolen from a virtual machine. This is the time a virtual machine has to wait for the hypervisor to finish servicing other virtual machines before it can come back and attend to this virtual machine. Using a Time Interval

We can have vmstat provide regular updates to these figures by using a delay value. The delay value is provided in seconds. To have the statistics updated every five seconds, we’d use the following command:

vmstat 5

vmstat 5 in a terminal window

Every five seconds vmstat will add another line of data to the table. You’ll need to hit Ctrl+C to stop this.

output from vmstat 5 in a terminal window

Using a Count Value

Using too low a delay value will put additional strain on your system. If you need to have rapid updates to try to diagnose a problem, it is recommended that you use a count value as well as a delay value.

The count value tells vmstat how many updates to perform before it exits and returns you to the command prompt. If you do not provide a count value, vmstat will run until it is stopped by Ctrl+C.

To have vmstat provide an update every five seconds—but only for four updates—use the following command:

vmstat 5 4

vmstat 5 4 in a terminal window

After four updates vmstat stops of its own accord.

output from vmstat 5 4 in a terminal window

Changing the Units

You can choose to have the memory and swap statistics displayed in kilobytes or megabytes using the -S (unit-character) option. This must be followed by one of k , K , m,  or M. These represent:

k:1000 bytes K: 1024 bytes m: 1000000 bytes M: 1048576 bytes

To have the statistics updated every 10 seconds with the memory and swap statistics displayed in megabytes, use the following command:

vmstat 10 -S M

vmstat 10 -S M ina terminal window

The memory and swap statistics are now shown in megabytes. Note that the -S option does not affect the IO block statistics. These are always displayed in blocks.

output from vmstat 10 -S M ina terminal window

Active and Inactive Memory

If you use the -a (active) option the buff and cache memory columns are replaced by the “inact” and “active” columns. As they would suggest, these show the amount of inactive and active memory.

To see these two columns instead of the buff and cache columns, include the -a option, as shown:

vmstat 5 -a -S M

vmstat 5 -a -S M in a terminal window

The inact and active columns are affected by the -S (unit-character) option.

output from vmstat 5 -a -S M in a terminal window

Forks

The -f switch displays the number of forks that have happened since the computer was booted up.

In other words, this shows the number of tasks that have been launched (and, for the bulk of them, closed again) since the system was booted. Every process launched from the command line would increase this figure. Each time a task or process spawns or clones a new task, this figure will increase.

vmstat -f

vmstat -f in a terminal window

The forks display does not update.

Displaying Slabinfo

The kernel has its own memory management to worry about as well as the memory management for the operating system and all of the applications.

As you might imagine the kernel is allocating and deallocating memory over and over for the many different types of data object that it must handle. To make this as efficient as possible, it uses a system called slabs. This is a form of caching.

Memory allocated, used, and no longer required for a specific type of kernel data object can be re-used for another data object of the same type without the memory being deallocated and reallocated. Think of slabs as pre-allocated, made to measure, segments of RAM for the kernel’s own needs.

To see the statistics for the slabs, use the -m (slabs) option. You will need to use sudo, and you will be prompted for your password. As the output can be quite lengthy, we are piping it through less.

sudo vmstat -m | less

sudo vmstat -m | less in a terminal window

The output has five columns. These are:

Cache: Name of the cache. num:  The number of currently active objects in this cache. total:  The total number of available objects in this cache. size:  The size of each object in the cache. pages:  The total number of memory pages that have (at least) one object currently associated with this cache.

output from sudo vmstat -m | less ina terminal window

Press q to leave less.

Displaying Event Counters and Memory Statistics

To display a page of event counters and memory statistics, use the -s (stats) option. Note that’s a lowercase “s.”

vmstat -s

vmstat -s in a terminal window

Although the statistics that are reported are largely the same as the information that makes up the default vmstat output, some of them are split out in more detail.

For example, the default output combines both the nice and the non-nice user CPU time into the “us” column. The -s (stats) display lists these statistics separately.

output from vmstat -s in aterminal window

Displaying Disk Statistics

You can obtain a similar listing of disk statistics using the -d (disk) option.

vmstat -d | less

vmstat -d | less in a terminal window

For each disk, three columns are displayed, these are Reads, Writes, and IO.

output from vmstat -d | less in a terminal window

IO is the rightmost column. Note that the sec column in IO is measured in seconds but the time-based statistics in the read and write columns are measured in milliseconds.

output vmstat -d | less in a terminal window

This is what the columns mean:

Reads

total: The total count of disk reads. merged: The total count of grouped reads. sectors: The total count of sectors that have been read in. ms: Total count of time in milliseconds that were used reading data from the disk.

writes

total: The total count of disk writes. merged: The total count of grouped writes. sectors: The total count of sectors written to. ms = Total count of time in milliseconds that were used writing data to the disk.

IO

cur: Number of current disk reads or writes. sec: Time spent in seconds for any in-progress reads or writes. Displaying Summary Disk Statistics

To see a quick display of summary statistics for your disk activity, use the -D (disk-sum) option. Note the uppercase “D.”

vmstat -D

vmstat -D in a terminal window

The number of disks might look abnormally high. The computer used to research this article is running Ubuntu. With Ubuntu, each time you install an application from a Snap, a squashfs pseudo-filesystem is created which is attached to a /dev/loop device.

Annoyingly these device entries are counted as hard drive devices by many of the Linux commands and utilities.

output from vmstat -D in a terminal window

Displaying Partition Statistics

To see statistics related to a specific partition, use the -p (partition) option and provide the partition identifier as a command line parameter.

Here we are going to look at the partition sda1. The digit one indicates this is the first partition on device sda, which is the main hard drive for this computer.

vmstat -p sda1

vmstat -p in a terminal window

The information returned shows the total count of disk reads and disk writes to and from that partition, and the number of sectors included in disk read and disk write actions.

output from vmstat -p sda1 in a terminal window

A Peek Under The Hood

It’s always good to know how to lift the hood and see what’s going on underneath. Sometimes you’ll be trying to problem solve, sometimes it’ll be out of interest because you want to know how your computer ticks.

vmstat can provide you with a ton of useful information. Now you know how to access it and what it means. And forewarned is forearmed—when you do need to roll your sleeves up and do some diagnostics, you’ll know you’ve got vmstat on your side.

Read More



Leave A Comment

More News

Latest ITProPortal news

Redefining the term engineer in the workplace. 2019-07-01 05:00:10Lots of people have 'engineer' in their job title, but what is engineering?

The predictive art of retention – using data 2019-07-01 04:30:34As the number of people voluntarily leaving their job roles continues to rise, it is now harder than ever to attract and retain talent. More companies

Vital points to consider while choosing the platform 2019-07-01 04:00:27Selecting the best platform for your website is an absolute requirement for your business, but choosing the right platform isn’t that easy.

Modernising mission critical communications 2019-07-01 04:00:18The modernisation of mission critical communications with intelligent push-to-talk broadband solutions.

Wi-Fi 6 vs. 5G: Key advantages for businesses 2019-06-28 07:00:222019 will be a landmark year for wireless connectivity and the Internet of Things (IoT), with the launches of Wi-Fi 6 and 5G making headlines across t

Why UK government organisations are a key target 2019-06-28 06:30:44Why aren’t government organisations doing more to protect their systems, the services they manage and the citizens they serve?

Somerville bans facial recognition in public 2019-06-28 06:30:30After San Francisco comes Somerville, and after that - possibly Oakland

Russia's Google breached, users spied upon 2019-06-28 06:00:29Yandex says the attack was spotted at an early stage

Boards have woken up to the value of 2019-06-28 06:00:01Security is now a priority for boards but managing security is as complex as ever.

Seven reasons asset compliance blows-up 2019-06-28 05:30:32How TAM can help businesses stay compliant and avoid the most common pitfalls.

GDPR one year on: where do we stand? 2019-06-28 05:00:24As we pass the GDPR one-year anniversary, how has the data landscape changed?

Best practices for preventing and recovering from a 2019-06-28 04:00:29How to defend and recover from ransomware attacks.

TechRadar: Internet news

Top Tech Conferences: The ultimate B2B tech events 2019-07-15 04:45:08We've compiled the ultimate list of technology events so you don't have to.

UAE consumers can experience 10GHz speed and 1ms 2019-07-15 04:41:16UAE consumers can experience 10GHz speed and 1ms latency in 5G network in 2020 through millimetre wave [26GHz-84Ghz]

The best Amazon Prime Day deals 2019: US 2019-07-15 04:37:26Prime Day deals include 4K TVs, Kindle, cheap laptops, PS4 consoles and the iPad and iPhone on sale. Here's what to buy on Amazon today.

Amazon Prime Day 2019: all the best tech 2019-07-15 04:36:11Amazon Australia's second Prime Day is here and we've got all the best deals listed right here.

iPhone XS vs iPhone XS Max vs iPhone 2019-07-15 04:24:20Is the iPhone XR, iPhone XR or iPhone XS Max better? Here's our expert advice having reviewed them all.

Amazon Prime Day 2019: the best deals on 2019-07-15 04:09:48Our Amazon Prime Day deals list is growing, with plenty of savings on TVs, Kindle, and headphones.

Realme X and Realme 3i launched in India 2019-07-15 03:49:30The much-awaited Realme X has been announced alongside the company's budget entry, Realme 3i in India.

The best wireless headphones available in India for 2019-07-15 03:31:49Want the best headphones sans cables? Here are the best ones.

Dell's Black Friday in July Mega Deals starts 2019-07-15 03:00:06Dell doesn't need Amazon Prime Day to bring the deals.

The best Amazon Prime Day deals 2019: UK 2019-07-15 02:29:50Prime Day deals include TVs, Kindles, laptops, Fire Sticks, Echo smart speakers and loads more super cheap offers

The best smartphone of 2019: 15 top mobile 2019-07-15 02:17:53The Samsung Galaxy S10 Plus tops our best smartphone 2019 rundown, but the Huawei P30 and P30 Pro have joined the fray.

The best cheap smart home devices and gadget 2019-07-15 00:33:32The best cheap smart home devices that include speakers, light bulbs, security cameras and more.

TechCrunch » Enterprise

48-hour, buy-one-get-one free — TC Sessions: Enterprise 2019 2019-07-15 12:00:57Every startupper we’ve ever met loves a great deal, and so do we. That’s why we’re celebrating Prime day with a 48-hour flash sale o

Judge dismisses Oracle lawsuit over $10B Pentagon JEDI 2019-07-12 14:13:11Oracle has been complaining about the procurement process around the Pentagon’s $10 billion, decade-long JEDI cloud contract, even before the Do

With $34B Red Hat deal closed, IBM needs 2019-07-12 14:02:35In a summer surprise this week, IBM announced it had closed its $34 billion blockbuster deal to acquire Red Hat. The deal, which was announced in Octo

Attend TC Sessions: Enterprise and score a free 2019-07-11 16:00:31We can’t wait to dig into the competitive, high-stakes world of enterprise software at TC Sessions: Enterprise 2019 on September 5 at the Yerba

Microsoft says Teams now has 13M daily active 2019-07-11 15:00:14Teams, Microsoft’s two-year-old Slack competitor, is the company’s fastest-growing application in its history. That’s something Micr

Andrew Ng to talk about how AI will 2019-07-11 12:49:37When it comes to applying AI to the world around us, Andrew Ng has few if any peers. We are delighted to announce that the renowned founder, investor,

Swit, a collaboration suite that offers ‘freedom from 2019-07-11 09:00:53A marketplace dominated by Slack and Microsoft Teams, along with a host of other smaller workplace communication apps, might seem to leave little room

OneTrust raises $200M at a $1.3B valuation to 2019-07-11 08:17:13GDPR, and the newer California Consumer Privacy Act, have given a legal bite to ongoing developments in online privacy and data protection: it’s

Signavio raises $177M at a $400M valuation for 2019-07-11 07:11:05Robotic Process Automation has been the name of the game in enterprise software lately — with organizations using advances in machine learning a

SAP CEO Bill McDermott will join us at 2019-07-10 12:15:07You can’t talk about enterprise software without talking about SAP, the German software giant that now has a market cap of more than $172 billio

Anvyl, looking to help D2C brands manage their 2019-07-10 08:30:52Growing D2C brands face an interesting challenge. While they’ve eliminated much of the hassle of a physical storefront, they must still deal wit

Visa funds $40M for no-password crypto vault Anchorage 2019-07-10 08:00:12Visa and Andreessen Horowitz are betting even bigger on cryptocurrency, funding a big round for fellow Facebook Libra Association member Anchorage&rsq

Digital Trends

Best Prime Day deals 2019: 4K TV, wireless 2019-07-15 19:46:17The best Amazon Prime Day deals have finally arrived. 4K TVs, Nintendo Switch bundles, iPads, and more are all on sale during this 48-hour stretch. No

Best Prime Day mattress sales: Tempur-Pedic, Sealy, and 2019-07-15 19:38:08The best Prime Day deals are here, and some of the very best discounts this year are on mattresses. With price cuts from Tempur-Pedic, Cocoon, and Hel

The best stand-up comedy on Netflix right now 2019-07-15 19:31:22Feeling a little funny? There are hundreds of hilarious comedy specials out there, and you can't be expected to comb through them all. Lucky for you,

Amazon cuts 33% off on the HP Sprocket 2019-07-15 19:30:47Amazon Prime Day sale is officially live. The HP Sprocket Plus Instant Photo Printer normally sells for $150, but Amazon cuts $50 off from its origina

Digital Trends Live: Our comprehensive coverage of Amazon 2019-07-15 19:23:13It's DT Live coverage of Amazon Prime Day. From TVs to computers, gaming, smart home devices, electronics, wearables, and much more, take a walk thro

This Prime Day Sony PS4 bundle deal comes 2019-07-15 19:17:00Amazon is currently offering a PS4 Pro bundle with the games God of War and Days Gone at a substantial discount. God of War was one of our favorite ga

Amazon Prime Day discounts makes upgrading to RTX 2019-07-15 19:12:56Looking to upgrade your graphics card? Today may be the day to do it, thanks to Amazon's Prime Day shopping event. Amazon is offering Prime members s

SpaceX finally knows what caused its Crew Dragon 2019-07-15 18:55:17SpaceX and NASA say they finally know what caused its Crew Dragon capsule to explode during an April test of the spacecraft’s thruster system. I

The best Amazon Prime Day deals on dashboard 2019-07-15 18:41:06The dash cam market grows annually, so these devices are rarely on sale. Prime Day 2019 is a stellar opportunity to modernize your car, keep yourself

This Prime Day Lightning Deal on Neato’s D4 2019-07-15 18:35:54Prime Day 2019 is shaping up to be a banner year for sales on smart home products, including robot vacuums. However, the deal we uncovered for Ne

The best mouse deals for Prime Day 2019: 2019-07-15 18:30:45This year's big shopping event, Amazon Prime Day, offers some fantastic discounts on mice. We've scoured this year's sales and made picks for what

Google Home Mini vs. Amazon Echo Dot: Which 2019-07-15 18:09:34We put the two most popular smart home speakers — the Google Home Mini and the third-generation Amazon Echo Dot — together and tested them


Disclaimer and Notice:WorldProNews.com is not responsible of these news or any information published on this website.