4 Optimized Memory Usage
vBlackOut edited this page 2026-08-15 01:25:32 +02:00

Optimized Memory Usage

This document explains how to optimize ProxyAuth memory usage on Linux systems, with a focus on Transparent Huge Pages (THP).

TL;DR: On some Linux systems, configuring THP as madvise can significantly reduce ProxyAuth's resident memory usage while retaining the ability to use huge pages when beneficial.


Overview

ProxyAuth is a long-running network service and its memory footprint depends on several factors, including:

  • active connections
  • request concurrency
  • TLS connections
  • HTTP buffers
  • sessions
  • caches
  • internal data structures
  • memory allocator behavior
  • Linux kernel memory management
  • Transparent Huge Pages

Therefore, memory usage should always be evaluated under the actual workload.

One Linux configuration that can have a particularly noticeable impact is Transparent Huge Pages.


Understanding ProxyAuth Memory Usage

Linux exposes several memory metrics for every process.

For a ProxyAuth process:

ps -o pid,rss,vsz,%mem,comm -p $(pidof proxyauth)

Example:

PID     RSS      VSZ       %MEM  COMMAND
19824   426852   4497104   0.3   proxyauth

In this example:

  • RSS: ~417 MiB resident memory
  • VSZ: ~4.3 GiB virtual address space
  • %MEM: percentage of physical system memory

The important metric for actual RAM consumption is RSS.


Transparent Huge Pages

Linux normally manages memory using pages of 4 KiB.

Transparent Huge Pages allow the kernel to use larger pages, typically 2 MiB, without requiring applications to explicitly allocate huge pages.

Check the current configuration:

cat /sys/kernel/mm/transparent_hugepage/enabled

Possible output:

always [madvise] never

The value inside [] is the active configuration.

always

[always] madvise never

The kernel aggressively attempts to use Transparent Huge Pages.

madvise

always [madvise] never

Applications must explicitly indicate that a memory region would benefit from huge pages.

This generally provides a more conservative memory policy.

never

always madvise [never]

Transparent Huge Pages are disabled.


Observed Impact

A ProxyAuth process was tested on Alpine Linux with the following configuration:

THP: always

The process reported:

VmRSS:          426852 kB
RssAnon:        410260 kB
AnonHugePages:  397312 kB

This corresponds to approximately:

RSS:            417 MiB
Anonymous RSS:  401 MiB
Anonymous THP:  388 MiB

THP was then changed to:

THP: madvise

ProxyAuth was restarted and reported:

VmRSS:          104756 kB
RssAnon:         91236 kB
AnonHugePages:   71680 kB

Approximately:

RSS:            102 MiB
Anonymous RSS:   89 MiB
Anonymous THP:   70 MiB

Comparison

Metric always madvise Difference
RSS ~417 MiB ~102 MiB ~75% lower
Anonymous RSS ~401 MiB ~89 MiB ~78% lower
AnonHugePages ~388 MiB ~70 MiB ~82% lower

This demonstrates that the THP policy can have a substantial impact on ProxyAuth's observed memory footprint.

This is an observed benchmark, not a guaranteed memory profile. Results will vary depending on workload, kernel version, allocator, CPU architecture, and ProxyAuth configuration.


For memory-sensitive deployments, madvise is recommended as a good starting point:

THP = madvise

Check:

cat /sys/kernel/mm/transparent_hugepage/enabled

Set temporarily:

echo madvise > /sys/kernel/mm/transparent_hugepage/enabled

Then restart ProxyAuth:

service proxyauth restart

Verify:

grep -E 'VmRSS|RssAnon' \
    /proc/$(pidof proxyauth)/status

And:

grep -E 'Rss:|Anonymous:|AnonHugePages:' \
    /proc/$(pidof proxyauth)/smaps_rollup

Alpine Linux

Alpine Linux commonly uses OpenRC rather than systemd.

The THP setting can therefore be applied using an OpenRC local startup script.

Create:

vi /etc/local.d/thp.start

Add:

#!/bin/sh

echo madvise > /sys/kernel/mm/transparent_hugepage/enabled

Make the script executable:

chmod +x /etc/local.d/thp.start

Enable the local service:

rc-update add local default

You can test the script without rebooting:

/etc/local.d/thp.start

Verify:

cat /sys/kernel/mm/transparent_hugepage/enabled

Expected:

always [madvise] never

Making the Configuration Persistent

The following command is not persistent:

echo madvise > /sys/kernel/mm/transparent_hugepage/enabled

The setting may be lost after a reboot.

Using /etc/local.d/thp.start ensures that Alpine reapplies the configuration during boot.

Alternatively, THP can be configured through the Linux kernel boot parameters.

For systems using GRUB, for example:

transparent_hugepage=madvise

The exact bootloader configuration depends on the distribution and boot setup.


Complementary Tuning: mimalloc's Purge Delay

THP policy is a system-wide setting. ProxyAuth itself uses mimalloc as its memory allocator, which has its own, independent knob for how aggressively it returns freed memory back to the OS: MIMALLOC_PURGE_DELAY.

This is a complementary setting to THP=madvise, not a replacement for it. THP controls how the kernel backs anonymous memory; MIMALLOC_PURGE_DELAY controls how quickly ProxyAuth's own allocator decommits memory it isn't using anymore after a traffic spike.

What it does

MIMALLOC_PURGE_DELAY is the delay, in milliseconds, before mimalloc purges (via madvise/munmap) memory pages that have been freed but not yet reused. A lower delay reclaims memory faster after a load spike; a higher delay keeps pages around longer in case they're needed again soon, avoiding repeated purge/recommit cycles.

  • Default (unset): mimalloc's built-in default, a short delay (on the order of ~10ms in recent versions).
  • Lower value / 0: RSS drops back down faster after a spike, at the cost of more frequent madvise/munmap syscalls under bursty traffic.
  • Higher value: fewer purge syscalls, but memory freed during a spike stays resident longer before being returned to the OS.
  • -1: disables purging entirely. Not recommended for memory-sensitive deployments.

Setting it

MIMALLOC_PURGE_DELAY must be set as an environment variable before ProxyAuth starts — mimalloc reads it during allocator initialization, which happens before ProxyAuth's own startup code runs. It cannot be set from within ProxyAuth's configuration files.

systemd — edit the unit file directly, or use an override:

sudo systemctl edit proxyauth
[Service]
Environment=MIMALLOC_PURGE_DELAY=0
sudo systemctl daemon-reload
sudo systemctl restart proxyauth

Alpine / OpenRC — export it from the same startup script used for THP, or from the service's environment file:

export MIMALLOC_PURGE_DELAY=0

Suggested testing steps

As with THP, don't jump straight to the most aggressive value in production. Test incrementally and measure both memory and performance at each step:

Step Value Purpose
1 unset (default) current baseline
2 1000 (1s) moderate — purges reasonably fast without excessive syscalls
3 0 most aggressive — compare against step 2 under real load

At each step, restart ProxyAuth and compare:

grep -E 'VmRSS|RssAnon' /proc/$(pidof proxyauth)/status

...alongside throughput and latency under representative traffic, not RSS alone — a lower MIMALLOC_PURGE_DELAY trades a small amount of CPU/syscall overhead for faster memory reclaim, and that trade-off is only worth it if it doesn't measurably hurt request latency under your actual load.

This, too, is workload-dependent. A deployment with steady, constant traffic has less to gain from a low purge delay than one with bursty traffic and infrequent spikes.


Monitoring Memory Usage

Basic Process Information

ps -o pid,rss,vsz,%mem,comm -p $(pidof proxyauth)

Detailed Process Memory

grep -E 'VmRSS|RssAnon|RssFile|RssShmem' \
    /proc/$(pidof proxyauth)/status

Example:

VmRSS:      104756 kB
RssAnon:     91236 kB
RssFile:     13520 kB
RssShmem:        0 kB

THP Usage

grep -E 'Rss:|Anonymous:|AnonHugePages:' \
    /proc/$(pidof proxyauth)/smaps_rollup

Example:

Rss:              104756 kB
Anonymous:         91236 kB
AnonHugePages:     71680 kB

Understanding RSS and VSZ

Do not use VSZ as the primary measurement of RAM consumption.

Example:

VmSize: 4497104 kB
VmRSS:   426852 kB

This means:

Virtual address space:  ~4.3 GiB
Resident memory:        ~417 MiB

VSZ includes virtual mappings and address space that may not currently occupy physical RAM.

RSS represents pages currently resident in physical memory.

For monitoring ProxyAuth's actual memory footprint, RSS is the more useful metric.


Understanding Anonymous Memory

You can inspect:

grep -E 'RssAnon|RssFile|RssShmem' \
    /proc/$(pidof proxyauth)/status

The main categories are:

RssAnon

Anonymous memory allocated by the process.

This can include:

  • heap allocations
  • runtime structures
  • buffers
  • caches
  • application state
  • allocator-managed memory

RssFile

Memory backed by files or shared libraries.

This can include:

  • executable mappings
  • shared libraries
  • memory-mapped files

RssShmem

Shared memory mappings.


Diagnosing High Memory Usage

If ProxyAuth appears to consume excessive memory, do not immediately assume a memory leak.

First collect:

grep -E 'VmRSS|RssAnon|RssFile|RssShmem' \
    /proc/$(pidof proxyauth)/status

Then:

cat /proc/$(pidof proxyauth)/smaps_rollup

Pay particular attention to:

Anonymous
AnonHugePages
Private_Dirty

Example

Anonymous:        410260 kB
AnonHugePages:    397312 kB
Private_Dirty:    410256 kB

This indicates that most of the process's resident memory is anonymous private memory and that a large portion is backed by Transparent Huge Pages.

Changing THP from always to madvise and restarting ProxyAuth can help determine whether THP is contributing significantly to the observed memory footprint.


Memory Growth vs. Initial Footprint

A high initial RSS does not necessarily indicate a memory leak.

The important measurement is how memory behaves over time.

Monitor ProxyAuth:

watch -n 1 'grep -E "VmRSS|RssAnon" /proc/$(pidof proxyauth)/status'

Or:

while true; do
    date
    grep -E 'VmRSS|RssAnon' /proc/$(pidof proxyauth)/status
    sleep 5
done

Stable memory

100 MiB
101 MiB
100 MiB
102 MiB
101 MiB

This is generally normal.

Continuously increasing memory

100 MiB
130 MiB
180 MiB
250 MiB
350 MiB
500 MiB

This requires further investigation.

Memory should be evaluated under a representative workload before concluding that a leak exists.


Production Considerations

Changing THP configuration affects the entire system, not only ProxyAuth.

Before changing the production configuration:

  1. Measure the current memory usage.
  2. Benchmark ProxyAuth under representative traffic.
  3. Change THP to madvise.
  4. Restart ProxyAuth.
  5. Measure RSS again.
  6. Compare throughput and latency.
  7. Monitor memory over time.

Do not assume that the lowest RSS configuration is automatically the fastest configuration. The same principle applies to MIMALLOC_PURGE_DELAY: the lowest value isn't automatically the best choice for every workload.

For high-throughput deployments, benchmark at least:

  • requests per second
  • latency
  • p95/p99 latency
  • CPU utilization
  • RSS
  • connection count
  • TLS workload
  • concurrent requests

Summary

For Linux deployments where ProxyAuth exhibits unexpectedly high memory usage:

cat /sys/kernel/mm/transparent_hugepage/enabled

If THP is:

[always] madvise never

consider testing:

echo madvise > /sys/kernel/mm/transparent_hugepage/enabled

Then restart ProxyAuth and compare:

grep -E 'VmRSS|RssAnon' \
    /proc/$(pidof proxyauth)/status

and:

grep -E 'Rss:|Anonymous:|AnonHugePages:' \
    /proc/$(pidof proxyauth)/smaps_rollup

In the tested Alpine Linux environment, switching from always to madvise reduced ProxyAuth's RSS from approximately 417 MiB to 102 MiB.

For memory-sensitive deployments, madvise is therefore a recommended starting configuration, followed by workload-specific benchmarking.

Once THP is tuned, MIMALLOC_PURGE_DELAY is a reasonable next lever to test — it operates independently of THP and targets how fast ProxyAuth's own allocator gives memory back after a spike, rather than how the kernel backs it.