JDK-8268893 : jcmd to trim the glibc heap
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 18
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • Submitted: 2021-06-16
  • Updated: 2024-02-08
  • Resolved: 2021-07-22
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 11 JDK 17 JDK 18
11.0.18Fixed 17.0.2Fixed 18 b08Fixed
Related Reports
Blocks :  
CSR :  
Relates :  
Relates :  
Relates :  
Description
The glibc is somewhat notorious for retaining released C Heap memory: calling free(3) returns memory to the glibc, and most libc variants will return at least a portion of it back to the Operating System, but the glibc often does not.

This depends on the granularity of the allocations and a number of other factors, but we found that many small allocations in particular may cause the process heap segment (hence RSS) to get bloaty. This can cause the VM to not recover from C-heap usage spikes.

The glibc offers an API, "malloc_trim", which can be used to cause the glibc to return free'd memory back to the Operating System.

This may cost performance, however, and therefore I hesitate to call malloc_trim automatically. That may be an idea for another day.

Instead of an automatic trim I propose to add a jcmd which allows to manually trigger a libc heap trim. Such a command would have two purposes:

- when analyzing cases of high memory footprint, it allows to distinguish "real" footprint, e.g. leaks, from a cases where the glibc just holds on to memory
- as a stop gap measure it allows to release pressure from a high footprint scenario.
- Note that this command also helps with analyzing libc peaks which had nothing to do with the VM - e.g. peaks created by customer code which just happens to share the same process as the VM. Such memory does not even have to show up in NMT.

In a way, this would be similar to the GC.run jcmd.

I propose to introduce this command for Linux only. Other OSes (apart maybe AIX) do not seem to have this problem, but Linux is arguably important enough in itself to justify a Linux specific jcmd.


Comments
Fix Request We should have this command in 11u too. It turned out to be very useful, and customers get confused because our downstream SapMachine11 has the command while OpenJDK proper doesn't. The risk is small. The associated downport CSR (https://bugs.openjdk.java.net/browse/JDK-8273602) had been approved for 11. Fix: https://github.com/openjdk/jdk11u-dev/pull/1354 does not apply cleanly to 11u because https://bugs.openjdk.org/browse/JDK-8282382 has been down ported out-of-order. After manually resolving, the affected code now looks identical to main JDK. Tests: Fix was tested manually on Linux x64, x86, both with glibc 2.31 and 2.35. Also, GHAs ran.
03-09-2022

A pull request was submitted for review. URL: https://git.openjdk.org/jdk11u-dev/pull/1354 Date: 2022-09-02 18:30:11 +0000
03-09-2022

Fix Request I'd like to backport this patch to jdk17. In the short time it existed, the command turned out to be very useful to us when analyzing suspected memory leaks. The risk is small (it adds a command which, effectively, causes `malloc_trim(3)` to be called in the target VM process, motivating the glibc to free lingering resources. The associated dowport CSR (https://bugs.openjdk.java.net/browse/JDK-8273602) has been provisionally approved by Joe. The patch applies cleanly. The functionality was unchanged compared to JDK 18. Thank you, Thomas
22-09-2021

Changeset: 6096dd97 Author: Thomas Stuefe <stuefe@openjdk.org> Date: 2021-07-22 04:07:10 +0000 URL: https://git.openjdk.java.net/jdk/commit/6096dd9765eaf280890f65c0ff1ab64864b9316a
22-07-2021