Skip to content
photostructurePublic

About

No description, website, or topics provided.

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@photostructure/heap-trim

Returns free C heap pages to the operating system from Node.js. Built for and supported by PhotoStructure.

npm version Build Node-API v9 Badge View on GitHub

Quick start

npm install @photostructure/heap-trim
import { heapTrim } from "@photostructure/heap-trim";

// After a burst of native work has freed its memory, while the process is idle:
heapTrim();

heapTrim(): boolean

glibc's free() shrinks the heap only from its top, so pages freed below a live allocation stay in the process's RSS. Native addons such as SQLite, image decoders, and Buffer backing stores allocate from that heap. heapTrim() calls glibc's malloc_trim(0), which releases those free pages in every malloc arena.

It locks each arena while it walks it, so call it when the process is idle, not after every request.

Returns: true if glibc found whole free pages and told the kernel to drop them. That doesn't mean RSS fell: glibc doesn't check whether those pages were still resident, so repeated calls keep returning true. false if it found none, or the process doesn't use glibc.

The function is on both the named and the default export, from CommonJS and ESM.

Supported platforms

Platform Architecture Node.js What heapTrim() does
Linux (glibc) x64, arm64 22+ Calls malloc_trim(0)
Linux (musl) x64, arm64 22+ Nothing; returns false
macOS x64, arm64 22+ Nothing; returns false
Windows x64, arm64 22+ Nothing; returns false

The package ships a native addon only for glibc Linux, which requires GLIBC 2.31+ (Debian 11+, Ubuntu 20.04+). On musl, macOS, and Windows it builds and loads no native code.

musl has no trim call. On macOS and Windows the allocators have one, but in the test this package uses (128 MiB of 64 KiB Buffers freed below a live one), neither released anything:

  • On macOS 27.2 (arm64), malloc_zone_pressure_relief(NULL, 0) returned 0 bytes, and the physical footprint stayed at 145 MB after the call.
  • On Windows 11 (x64), the heap had already decommitted the freed pages before any call, and _heapmin() changed private bytes by 0.1 MB.

Documentation

Development

Development of this library was assisted by AI coding tools. All changes are human-reviewed and tested.

About

No description, website, or topics provided.

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

⌂ 换网址