Home  >  Article  >  Backend Development  >  Extract memory and swap information from /proc/meminfo in Golang

Extract memory and swap information from /proc/meminfo in Golang

王林
王林forward
2024-02-12 18:24:09331browse

从 Golang 中的 /proc/meminfo 中提取内存和交换信息

php editor Strawberry will introduce to you today a very practical method, which is to extract memory and exchange information from /proc/meminfo in Golang. During the Golang development process, we often need to obtain the system's memory and exchange information in order to perform some performance optimization or resource management operations. /proc/meminfo is a file that saves system memory and swap information. We can obtain the required information by reading this file. Next, I will introduce in detail how to use Golang to extract the memory and swap information in /proc/meminfo. I hope it will be helpful to you.

Question content

I want to extract the values ​​of memtotal, memfree, memavailable, swaptotal and swapfree from /proc/meminfo in golang. The closest I've gotten so far is using fmt.sscanf() which will give me the value I want one at a time, but I also get a lot of output lines with zeros. This is the code I'm using:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    f, e := os.open("/proc/meminfo")
    if e != nil {
        panic(e)
    }
    defer f.close()
    s := bufio.newscanner(f)
    for s.scan() {
        var n int
        fmt.sscanf(s.text(), "memfree: %d kb", &n)
        fmt.println(n)
    }
}

This gives me the following results:

0
11260616
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

So first question, is there a way to limit the results to the one value I want (non-zero)? Or, is there a better way to solve this problem?

My /proc/meminfo file looks like this:

MemTotal:       16314336 kB
MemFree:        11268004 kB
MemAvailable:   13955820 kB
Buffers:          330284 kB
Cached:          2536848 kB
SwapCached:            0 kB
Active:          1259348 kB
Inactive:        3183140 kB
Active(anon):       4272 kB
Inactive(anon):  1578028 kB
Active(file):    1255076 kB
Inactive(file):  1605112 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       4194304 kB
SwapFree:        4194304 kB
Dirty:                96 kB
Writeback:             0 kB
AnonPages:       1411704 kB
Mapped:           594408 kB
Shmem:              6940 kB
KReclaimable:     151936 kB
Slab:             253384 kB
SReclaimable:     151936 kB
SUnreclaim:       101448 kB
KernelStack:       17184 kB
PageTables:        25060 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    12351472 kB
Committed_AS:    6092984 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       40828 kB
VmallocChunk:          0 kB
Percpu:             5696 kB
AnonHugePages:    720896 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      230400 kB
DirectMap2M:    11235328 kB
DirectMap1G:    14680064 kB

Solution

Note that s.Scan() reads the input line by line. If a line does not match the format string given to fmt.Sscanf, the program will output 0 as var n int declared inside the loop. My suggestion is to check the first result returned by fmt.Sscanf`, which is the number of matching items. So if the first result is 1, that means there is a match and that value can be output. See a working example here: https://www.php.cn/link/25d0a45ccd9e33b6b1ef8760801b6841 p>

Edit: I tried to get as close to your code as possible. There may be other issues as the measurement units used may vary according to the Man Page. However, if the relevant value on your system is always output in "kB", then it may be enough for your use case.

The above is the detailed content of Extract memory and swap information from /proc/meminfo in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete