HeadlinesBriefing favicon HeadlinesBriefing.com

Linux Kernel Rewrites AF_UNIX Garbage Collector for Better Memory Management

Hacker News •
×

Linux kernel developers rewrote the AF_UNIX garbage collector to tackle memory leaks caused by sockets spread through SCM_RIGHTS. The new subsystem replaces a fragile graph walk that locked each receive queue with a lightweight graph model that tracks sockets in flight. By counting inflight sockets, the collector triggers when the global counter exceeds 16000 or on socket close immediately efficiently.

When a file descriptor of an AF_UNIX socket is sent, the kernel increments an inflight counter and decrements it when accepted. If a socket’s file reference count equals its inflight count, no user‑space handle can reach it, yet the kernel still holds the struct file. The collector marks such cycles for release, preventing memory use today.

The new collector builds a directed graph where each inflight socket is a vertex and each SCM_RIGHTS transfer is an edge. Tarjan’s algorithm partitions this graph into strongly‑connected components; only components with more than one vertex can contain cycles. By focusing on cycles, the kernel frees only truly unreachable sockets, keeping the kernel lean without locking queues to.