Skip to content

fix: try to remove Wired memory for MacOS sizing #2010

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 2 commits into from
May 28, 2025
Merged
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
58 changes: 10 additions & 48 deletions pkg/container/qemu_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,60 +1307,22 @@ func getAvailableMemoryKB() int {
return memFree + buffers + cached
}
case "darwin":
// Use vm_stat to get available memory on macOS
cmd := exec.Command("vm_stat")
output, err := cmd.CombinedOutput()
var memSize int64

cmd := exec.Command("sysctl", "-n", "hw.memsize_available")
sysctlOut, err := cmd.Output()
if err != nil {
return mem
}

// Parse vm_stat output
scanner := bufio.NewScanner(bytes.NewReader(output))

var pageSize int64
var pagesFree, pagesInactive int64

for scanner.Scan() {
line := scanner.Text()

// Parse page size from header
if strings.Contains(line, "page size of") {
if _, err := fmt.Sscanf(line, "Mach Virtual Memory Statistics: (page size of %d bytes)", &pageSize); err != nil {
return 0
}
}

// Parse memory values using a more flexible approach
fields := strings.Fields(line)
if len(fields) >= 3 && strings.HasSuffix(fields[1], ":") {
// Remove trailing colon and period from the value
valueStr := strings.TrimSuffix(fields[2], ".")
value, err := strconv.ParseInt(valueStr, 10, 64)
if err != nil {
continue
}

switch fields[0] + " " + strings.TrimSuffix(fields[1], ":") {
case "Pages free":
pagesFree = value
case "Pages inactive":
pagesInactive = value
}
}
sysctlOutStr := strings.TrimSpace(string(sysctlOut))
memSize, err = strconv.ParseInt(sysctlOutStr, 10, 64)
if err != nil {
return mem
}

if pageSize > 0 && (pagesFree > 0 || pagesInactive > 0) {
// Calculate available memory in KB
// Available = (free + inactive) * pageSize / 1024
// Note: speculative pages are excluded as they are not guaranteed to be available
availableBytes := (pagesFree + pagesInactive) * pageSize
availableKB := availableBytes / 1024

// Ensure we return a reasonable value
if availableKB > 0 {
return int(availableKB)
}
}
// use at most 50% of total ram, in kb
return int(memSize) / 2 / 1024
}

return mem
Expand Down
Loading