Lock Ecasound Audio Buffers in Memory

To prevent Ecasound audio buffers from being paged out to disk swap, you must grant the process memory locking privileges at the operating system level, configure POSIX real-time scheduling within Ecasound, and optimize system swappiness. Because Ecasound relies on standard POSIX system calls and underlying audio engines (such as ALSA or JACK) to handle low-latency I/O, memory locking is enforced by combining PAM memlock limits, real-time runtime flags, and audio server lock directives.

1. Configure OS-Level Memory Locking Limits

Before Ecasound or its audio backends can pin buffer memory into physical RAM via mlock() or mlockall(), the user running the process must have adequate memlock privileges configured in PAM.

Edit /etc/security/limits.conf (or create a file such as /etc/security/limits.d/99-audio.conf) and add the following lines for your audio group or user:

@audio   soft   memlock   unlimited
@audio   hard   memlock   unlimited
@audio   soft   rtprio    95
@audio   hard   rtprio    95

Ensure your user is a member of the target group (e.g., sudo usermod -a -G audio $USER), then log out and log back in. If Ecasound is managed by a systemd service, specify the resource limit directly inside the service unit file:

[Service]
LimitMEMLOCK=infinity
LimitRTPRIO=95

2. Enable Ecasound Real-Time Priority

Ecasound supports POSIX real-time scheduling (SCHED_FIFO), which works in tandem with locked memory to prevent page faults and buffer underruns during processing.

To enable real-time scheduling from the command line, pass the -r option:

ecasound -r -i:input.wav -o:alsa

To set a specific scheduling priority between 1 and 99 (for example, priority 50):

ecasound -r:50 -i:somefile.wav -o:alsa

When run with -r and valid PAM rtprio permissions, Ecasound prioritizes audio processing threads, reducing the risk of page out events during heavy I/O.

3. Use JACK with Real-Time Memory Locking

When ultra-low latency and guaranteed unpaged memory are required, route Ecasound through the JACK Audio Connection Kit. JACK explicitly calls mlockall(MCL_CURRENT | MCL_FUTURE), ensuring all client buffers—including those instantiated by Ecasound—remain locked in physical RAM.

  1. Start the JACK server with memory locking enabled (-R or --realtime):
    jackd -R -d alsa -p 128 -r 48000
  2. Connect Ecasound as a JACK client using the -i:jack and -o:jack options:
    ecasound -i:jack,system:capture_1 -o:jack,system:playback_1

Because JACK holds an active global memory lock on its shared audio ringbuffers, Ecasound's streaming buffers within the JACK graph cannot be swapped to disk.

4. Reduce Kernel Swappiness

To further ensure the Linux kernel does not aggressively target audio process memory during background memory pressure, lower the kernel swappiness value.

Run the following command to temporarily lower swappiness:

sudo sysctl vm.swappiness=10

To make this permanent, add vm.swappiness = 10 to /etc/sysctl.conf or /etc/sysctl.d/99-audio.conf and reload with sudo sysctl --system. This discourages anonymous memory pages from being pushed to swap while audio streams are active.