GolangNote

Golang笔记

Golang 进程管理工具: gopsutil

Permalink

gopsutil 是用go 实现python 写的psutil 的功能,并尝试在多平台上完整psutil 的函数。

下面是对内存的显示:

Go: 显示内存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
	"fmt"
	"github.com/shirou/gopsutil/mem"
)

func main(){
	v, _ := mem.VirtualMemory()

	// almost every return value is a struct
	fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)

	// convert to JSON. String() is also implemented
	fmt.Println(v)
}

在我的电脑上输出:

JSON: 内存状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
	"total": 17179869184,
	"available": 9234153472,
	"used": 7945715712,
	"usedPercent": 46.250152587890625,
	"free": 5201149952,
	"active": 3655434240,
	"inactive": 4033003520,
	"wired": 2986549248,
	"laundry": 0,
	"buffers": 0,
	"cached": 0,
	"writeback": 0,
	"dirty": 0,
	"writebacktmp": 0,
	"shared": 0,
	"slab": 0,
	"pagetables": 0,
	"swapcached": 0,
	"commitlimit": 0,
	"committedas": 0,
	"hightotal": 0,
	"highfree": 0,
	"lowtotal": 0,
	"lowfree": 0,
	"swaptotal": 0,
	"swapfree": 0,
	"mapped": 0,
	"vmalloctotal": 0,
	"vmallocused": 0,
	"vmallocchunk": 0,
	"hugepagestotal": 0,
	"hugepagesfree": 0,
	"hugepagesize": 0
}

对进程的管理:

Go: 管理进程
1
2
3
4
5
6
7
8
9
10
p, _ := process.NewProcess(pid) // Specify process id of parent

ps, _ := p.Children()
for _, v := range ps{
	err = v.Kill()  // Kill each child
	// handle error
}

p.Kill() // Kill the parent process

还有其它各种系统信息。。。。。。参见 gopsutil https://github.com/shirou/gopsutil

本文网址: https://golangnote.com/topic/244.html 转摘请注明来源

Related articles

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

golang Selenium WebDriver 使用记录

Selenium WebDriver 直接通过浏览器自动化的本地接口来调用浏览器,以达到模拟浏览器行为的操作,如点击、选择、鼠标移动等。下面是记录个人使用golang 驱动的记录。...

Golang 泛型性能初识

编程时,我们通常需要编写“泛型”函数,其中确切的数据类型并不重要。例如,您可能想编写一个简单的函数来对数字进行求和。Go 直到最近才有这个概念,但最近才添加了它(从1.18版开始)。...

Golang quicktemplate 模版快速入门

Golang 有很多的模版引擎,自带的 `html/template` 也很好,大多数情况都能满足需求,只是有些逻辑、条件判断不好在模版里实现, `quicktemplate` 是个很好的选择。...

golang snappy 的使用场合

google 自家的 snappy 压缩优点是非常高的速度和合理的压缩率。压缩率比 gzip 小,CPU 占用小。...

Write a Comment to "Golang 进程管理工具: gopsutil"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms