Skip to content

Add examples/psutils/mem/exmem #919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/psutil/mem/exmem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app
14 changes: 14 additions & 0 deletions examples/psutil/mem/exmem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# これは何?

[gopsutil](https://github.com/shirou/gopsutil)を用いてメモリ消費量を取得するサンプルです。

v4.24.5から追加された Ex構造体 を利用するサンプルです。

```sh
$ task
task: [clean] rm -f ./app
task: [build] go build -o app .
task: [run] ./app
07:40:14 [NORMAL] {"total":67421265920,"available":52788764672,"used":13726756864,"usedPercent":20.359684257913145,"free":31725797376,"active":6271885312,"inactive":21599387648,"wired":0,"laundry":0,"buffers":1032192,"cached":21967679488,"writeBack":0,"dirty":3129344,"writeBackTmp":0,"shared":158375936,"slab":5851881472,"sreclaimable":3740106752,"sunreclaim":2111774720,"pageTables":174186496,"swapCached":22228992,"commitLimit":436363812864,"committedAS":31354429440,"highTotal":0,"highFree":0,"lowTotal":0,"lowFree":0,"swapTotal":402653179904,"swapFree":402577944576,"mapped":1446465536,"vmallocTotal":35184372087808,"vmallocUsed":204656640,"vmallocChunk":0,"hugePagesTotal":0,"hugePagesFree":0,"hugePagesRsvd":0,"hugePagesSurp":0,"hugePageSize":2097152,"anonHugePages":12582912}
07:40:14 [EX ] {"activefile":5806538752,"inactivefile":12259991552,"activeanon":465346560,"inactiveanon":9339396096,"unevictable":87674880}
```
19 changes: 19 additions & 0 deletions examples/psutil/mem/exmem/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# https://taskfile.dev

version: '3'

tasks:
default:
cmds:
- task: clean
- task: build
- task: run
build:
cmds:
- go build -o app .
run:
cmds:
- ./app
clean:
cmds:
- rm -f ./app
41 changes: 41 additions & 0 deletions examples/psutil/mem/exmem/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build linux

package main

import (
"log"

"github.com/shirou/gopsutil/v4/mem"
)

func main() {
log.SetFlags(log.Ltime)

if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
var (
vmem *mem.VirtualMemoryStat
exVmem *mem.ExVirtualMemory
err error
)
// マルチプラットフォームで利用出来る方法
vmem, err = mem.VirtualMemory()
if err != nil {
return err
}

// 各OSごとに特化している情報を取得する方法
exVmem, err = mem.NewExLinux().VirtualMemory()
if err != nil {
return err
}

log.Printf("[NORMAL] %s", vmem)
log.Printf("[EX ] %s", exVmem)

return nil
}
Loading