ollama - ✅(Solved) Fix Ollama incorrectly detects available memory when running in containers [1 pull requests, 4 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#15474Fetched 2026-04-11 06:12:41
View on GitHub
Comments
4
Participants
2
Timeline
6
Reactions
0
Author
Timeline (top)
commented ×4cross-referenced ×1labeled ×1

Root Cause

When running on bare metal, Ollama uses MemAvailable from /proc/meminfo. This corresponds to an estimate of how much memory applications have to operate with. Available memory doesn't include the used page cache because page cache can be instantly freed.

Fix Action

Fixed

PR fix notes

PR #15476: discover: fix available memory calculation for cgroups

Description (problem / solution / changelog)

Fixes #15474

Please, keep in mind that this code was generated by an LLM. I am not a Go programmer.

That being said, it seems to work quite well and fixes my issue. I have tested on

  1. Bare metal without a memory limit
  2. Running in a Docker container without a memory limit
  3. Running in a Docker container with a memory limit

Changed files

  • discover/cpu_linux.go (modified, +66/-6)
RAW_BUFFERClick to expand / collapse

What is the issue?

When Ollama runs in a container with a memory limit (this is very important), it incorrectly detects available memory.

When running on bare metal, Ollama uses MemAvailable from /proc/meminfo. This corresponds to an estimate of how much memory applications have to operate with. Available memory doesn't include the used page cache because page cache can be instantly freed.

When running in a container with a memory limit, Ollama reads the available memory from cgroups. But cgroups don't expose a metric comparable to MemAvailable from /proc/meminfo. The closest there is is /sys/fs/cgroup/memory.current but that also includes the used page cache. What this means in practice is that after Ollama loads a model once, Linux caches it into page cache and even after the model gets unloaded, the page cache stays filled and since the page cache is included in memory.current, Ollama thinks that there isn't enough available memory.

TLDR; mem.FreeMemory = /sys/fs/cgroup/memory.max - /sys/fs/cgroup/memory.current is too naive and causes Ollama to think it has less memory available than what it has in reality.

Relevant lines: https://github.com/ollama/ollama/blob/main/discover/cpu_linux.go#L69-L79

I'll have an LLM-generated PR ready to fix this soon. It's a fairly minimal change and it looks good to me but I'm not a Go programmer.

Relevant log output

OS

Docker, Linux

GPU

No response

CPU

Intel, Other

Ollama version

0.20.5

extent analysis

TL;DR

  • Adjust the memory calculation in Ollama to account for page cache when running in a container with a memory limit.

Guidance

  • Review the calculation of available memory in the cpu_linux.go file, specifically lines 69-79, to ensure it accurately reflects the available memory when running in a container.
  • Consider using a more accurate metric, such as one that estimates the amount of memory that can be freed by the page cache, to improve the calculation of available memory.
  • Test the updated calculation with different scenarios, including loading and unloading models, to verify its accuracy.
  • Evaluate the proposed PR fix to ensure it correctly addresses the issue and does not introduce any new problems.

Example

No code example is provided due to the uncertainty of the exact fix, but the relevant lines of code are in cpu_linux.go lines 69-79.

Notes

The current implementation of available memory calculation in Ollama is too naive and does not account for the page cache, leading to incorrect estimates of available memory when running in a container.

Recommendation

  • Apply workaround: Adjust the memory calculation to account for page cache, as the exact fix is not yet confirmed and may require further testing and verification.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING