JDK-8293114 : JVM should trim the native heap
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: gc
  • Affected Version: 17,21,22
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • Submitted: 2022-08-30
  • Updated: 2025-02-28
  • Resolved: 2023-07-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17 JDK 21 JDK 22
17.0.9Fixed 21.0.1Fixed 22 b08Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
In 2021 we discussed the possibility of letting the JVM auto-trim the native heap to combat Glibc memory retention.

The Glibc allocator is reluctant to return memory to the OS, much more so than other allocators. Temporary malloc spikes often carry over as permanent RSS increase.

Note that C-heap retention is difficult to observe. Since it is freed memory, it won't show up in NMT, it is just a part of private RSS.

Theoretically, retained memory is not lost since it will be reused by future mallocs. Retaining memory is therefore a bet on the future behavior of the app. The allocator bets on the application needing memory in the near future, and to satisfy that need via malloc.

But an app's malloc load can fluctuate wildly, with temporary spikes and long idle periods. And if the app rolls its own custom allocators atop of mmap, as hotspot does, a lot of that memory cannot be reused even though it counts toward its memory footprint.

To help, Glibc exports an API to trim the C-heap: malloc_trim(3). It would be good if we were to integrate it into the JVM as an optional GC step.


[1] https://mail.openjdk.org/pipermail/hotspot-dev/2021-August/054323.html
Comments
Fix Request (17u) Same reason as for 21u. 17u PR requires adaptations, acked by Thomas. The adaptations do not affect the exposure surface: the feature is still well isolated.
16-08-2023

Fix Request (21u) Enables the experimental feature to avoid apparent memory leaks due to native allocator overprovision. Applies cleanly. The feature is well-isolated from the rest of the code, and disabled by default. Tests pass. Needs JDK-8312525 testbug fix as the follow-up.
09-08-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk17u-dev/pull/1616 Date: 2023-07-21 17:55:38 +0000
21-07-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk21u/pull/13 Date: 2023-07-21 16:15:18 +0000
21-07-2023

I am going to line up some backports to 17u and 21u, so we can review and tests them while this feature is being soaked in mainline. Don't be spooked :)
21-07-2023

Changeset: 9e4fc568 Author: Thomas Stuefe <stuefe@openjdk.org> Date: 2023-07-21 12:22:03 +0000 URL: https://git.openjdk.org/jdk/commit/9e4fc568a6f1a93c84a84d6cc5220c6eb4e546a5
21-07-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/14781 Date: 2023-07-06 06:54:22 +0000
06-07-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/10085 Date: 2022-08-30 14:35:26 +0000
31-08-2022