Metal cache admission can exceed the configured cache limit

Open
#4,841 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@tijs is already working on this.

Since Nov 6, 2026.

  • #4842 by @tijs — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
cpp

Research direction

Start by locating MetalAllocator::free() and the cache limit checks described in the issue. Reproduce the oversized and mixed allocation cases with the allocator tests, then inspect the linked pull request #4842 for the two-file fix and regression coverage. Done means freed buffers respect the configured limit, including zero, and the reported reuse cases are covered.

Written by the indexing model from the issue text.

Description

MetalAllocator::free() checks the current cache size before admitting the incoming buffer, without including that buffer's length. This can exceed set_cache_limit() as soon as a live allocation is released.

On Apple M1 Max, using current main 9f806b3606eaa9f7c807238f6bd465185cd94d6d:

  1. Set the cache limit to 1 MiB and clear it.
  2. Allocate and release a 2 MiB buffer: get_cache_memory() reports 2,097,152 bytes.
  3. Clear it, hold a 512 KiB allocation while allocating/releasing a 768 KiB allocation, then release the 512 KiB allocation: the cache reaches 1,310,720 bytes with a 1,048,576-byte limit.

No later allocation is made before inspecting each overshoot. The checks use allocator::malloc directly, so shader execution and asynchronous command completion are not involved in the reproduction.

Expected: an incoming buffer larger than the allowance should not enter the cache. A smaller incoming buffer should be admitted only after enough older cached bytes are released. Zero should continue to disable reuse. This concerns the freed-buffer cache, not a limit on active allocations or total process memory.

I have a small two-file fix and regression test covering oversized admission, mixed shapes, recent-shape reuse and zero allowance.

Dominant language
C++
Stars
29.1k
Forks
2.4k
Avg merge
3d 20h
Merged PRs (30d)
103

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ml-explore/mlx

All issues in ml-explore/mlx

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.