How to Limit Max Network Cache Size in mpv?

To prevent mpv from consuming excessive RAM or buffering too far ahead on slow or metered internet connections, you can restrict its maximum network cache size using specific command-line flags or configuration file settings. By default, mpv uses a generous cache buffer that can overwhelm slower connections or lower-spec devices. Adjusting parameters like --demuxer-max-bytes allows you to cap the memory allocated for lookahead buffering, ensuring a more stable playback experience without unnecessary data usage.

Using Command-Line Arguments

If you want to apply the cache limit on a case-by-case basis, you can pass the cache size options directly when launching mpv from your terminal or command prompt.

Making the Limits Permanent via Config File

To avoid typing these options every time, you can add them to your mpv.conf configuration file. This file is typically located at ~/.config/mpv/mpv.conf on Linux/macOS, or %APPDATA%\mpv\mpv.conf on Windows.

Open your mpv.conf file in a text editor and add the following lines to optimize for a slow connection:

# Limit network cache size to 64 Megabytes
demuxer-max-bytes=64MiB

# Disable or heavily limit the backbuffer to save memory
demuxer-max-back-bytes=0MiB

# Force mpv to use the cache for all network protocols
cache=yes

Additional Cache Tweaks for Slow Connections

While limiting the maximum cache size protects your system resources, you may also want to tweak how mpv behaves when the cache runs dry on a slow connection.

Adjusting Secs to Buffer

Instead of just limiting bytes, you can control how many seconds of video mpv tries to read ahead using demuxer-readahead-secs. Setting this to a lower value prevents it from buffering too far into the future.

demuxer-readahead-secs=20

Pausing for Cache Refills

To prevent choppy playback on highly unstable connections, you can tell mpv to wait until a certain percentage of the cache is filled before it resumes playing.

# Pause playback if the cache drops below this threshold (in seconds)
cache-pause-initial=yes