MCPcopy Create free account
hub / github.com/MertJSX/folderhost / getProcessRAM

Function getProcessRAM

utils/service_utils/ram_management.go:16–60  ·  view source on GitHub ↗
(pid int)

Source from the content-addressed store, hash-verified

14)
15
16func getProcessRAM(pid int) (int64, error) {
17 switch runtime.GOOS {
18 case "linux":
19 data, err := os.ReadFile(fmt.Sprintf("/proc/%d/statm", pid))
20 if err != nil {
21 return 0, err
22 }
23 fields := strings.Fields(string(data))
24 if len(fields) < 2 {
25 return 0, fmt.Errorf("invalid statm")
26 }
27 rss, _ := strconv.ParseInt(fields[1], 10, 64)
28 return rss * int64(os.Getpagesize()), nil
29
30 case "windows":
31 cmd := exec.Command("powershell", "-Command", fmt.Sprintf(`
32 $total = 0
33 $pids = [System.Collections.Generic.Queue[int]]::new()
34 $pids.Enqueue(%d)
35 $visited = @{}
36 while ($pids.Count -gt 0) {
37 $current = $pids.Dequeue()
38 if ($visited[$current]) { continue }
39 $visited[$current] = $true
40 try {
41 $p = Get-Process -Id $current -ErrorAction Stop
42 $total += $p.WorkingSet64
43 Get-CimInstance Win32_Process -Filter "ParentProcessId=$current" | ForEach-Object { $pids.Enqueue($_.ProcessId) }
44 } catch {}
45 }
46 $total
47 `, pid))
48 out, _ := cmd.Output()
49 return strconv.ParseInt(strings.TrimSpace(string(out)), 10, 64)
50
51 case "darwin":
52 cmd := exec.Command("ps", "-o", "rss=", "-p", strconv.Itoa(pid))
53 out, _ := cmd.Output()
54 rss, _ := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 64)
55 return rss * 1024, nil
56
57 default:
58 return 0, fmt.Errorf("unsupported platform")
59 }
60}
61
62func getProcessTreeRAM(pid int) (int64, error) {
63 switch runtime.GOOS {

Callers 1

getProcessTreeRAMFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected