diff --git a/component/memory/memory.go b/component/memory/memory.go index 26c764a9..4ae7d75d 100644 --- a/component/memory/memory.go +++ b/component/memory/memory.go @@ -1,5 +1,5 @@ // Package memory return MemoryInfoStat -// modify from https://github.com/shirou/gopsutil/tree/v4.25.1/process +// modify from https://github.com/shirou/gopsutil/tree/v4.25.8/process package memory type MemoryInfoStat struct { diff --git a/component/memory/memory_darwin.go b/component/memory/memory_darwin.go index d067c91b..3e9e6971 100644 --- a/component/memory/memory_darwin.go +++ b/component/memory/memory_darwin.go @@ -83,30 +83,37 @@ const ( ProcPidInfoSym = "proc_pidinfo" ) -var ( - procPidInfo ProcPidInfoFunc -) +type dlFuncs struct { + lib *Library -func registerFuncs() (*Library, error) { + procPidInfo ProcPidInfoFunc +} + +func loadProcFuncs() (*dlFuncs, error) { lib, err := NewLibrary(System) if err != nil { return nil, err } - procPidInfo = GetFunc[ProcPidInfoFunc](lib, ProcPidInfoSym) + return &dlFuncs{ + lib: lib, + procPidInfo: GetFunc[ProcPidInfoFunc](lib, ProcPidInfoSym), + }, nil +} - return lib, nil +func (f *dlFuncs) Close() { + f.lib.Close() } func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) { - lib, err := registerFuncs() + funcs, err := loadProcFuncs() if err != nil { return nil, err } - defer lib.Close() + defer funcs.Close() var ti ProcTaskInfo - procPidInfo(pid, PROC_PIDTASKINFO, 0, uintptr(unsafe.Pointer(&ti)), int32(unsafe.Sizeof(ti))) + funcs.procPidInfo(pid, PROC_PIDTASKINFO, 0, uintptr(unsafe.Pointer(&ti)), int32(unsafe.Sizeof(ti))) ret := &MemoryInfoStat{ RSS: uint64(ti.Resident_size),