AutoMemoryLimit computes a DuckDB memory_limit based on system memory. Formula: totalMem * 0.75, with a floor of 256MB. Every session gets the full budget — DuckDB will spill to disk/swap if aggregate usage exceeds physical RAM. Returns "4GB" as a safe default if system memory cannot be detected. Th
()
| 62 | // Returns "4GB" as a safe default if system memory cannot be detected. |
| 63 | // The result is computed once and cached since system memory doesn't change. |
| 64 | func AutoMemoryLimit() string { |
| 65 | autoMemoryLimitOnce.Do(func() { |
| 66 | totalBytes := SystemMemoryBytes() |
| 67 | if totalBytes == 0 { |
| 68 | autoMemoryLimitValue = "4GB" |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | const mb = 1024 * 1024 |
| 73 | const gb = 1024 * mb |
| 74 | |
| 75 | limitBytes := totalBytes * 3 / 4 // 75% of system RAM |
| 76 | if limitBytes < 256*mb { |
| 77 | limitBytes = 256 * mb |
| 78 | } |
| 79 | |
| 80 | // Format as human-readable: use GB if >= 1GB, else MB. |
| 81 | if limitBytes >= gb { |
| 82 | limitGB := limitBytes / gb |
| 83 | autoMemoryLimitValue = fmt.Sprintf("%dGB", limitGB) |
| 84 | } else { |
| 85 | limitMB := limitBytes / mb |
| 86 | autoMemoryLimitValue = fmt.Sprintf("%dMB", limitMB) |
| 87 | } |
| 88 | }) |
| 89 | return autoMemoryLimitValue |
| 90 | } |
| 91 | |
| 92 | // validMemoryLimit matches DuckDB memory limit values like "4GB", "512MB", "1.5GB", "1TB". |
| 93 | var validMemoryLimit = regexp.MustCompile(`(?i)^\d+(\.\d+)?\s*(KB|MB|GB|TB)$`) |