Skip to content

Optimize memory usage on Mac by pooling memory passed to sysctl #41

Open
@cmacknz

Description

@cmacknz

The sysctl implementation allocates 256 KiB for every invocation.

In go-sysinfo a sync.Pool is used to minimize the amount of memory that is allocated for each call. It uses a buffer from the pool to make the call then allocates only the size required rather than ARG_MAX.

Port this same optimization to the system metrics package. Here is one example where it would apply:

func getProcArgs(pid int, filter func(string) bool) ([]string, string, mapstr.M, error) {
mib := []C.int{C.CTL_KERN, C.KERN_PROCARGS2, C.int(pid)}
argmax := uintptr(C.ARG_MAX)
buf := make([]byte, argmax)
err := sysctl(mib, &buf[0], &argmax, nil, 0)
if err != nil {
return nil, "", nil, fmt.Errorf("error in sysctl: %w", err)
}

We could avoid allocating a new slice for every process with this optimization.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions