JDK-8318817 : Could not reserve enough space in CodeHeap 'profiled nmethods' (0K)
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 22
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2023-10-25
  • Updated: 2023-11-27
  • Resolved: 2023-11-14
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 22
22 b24Fixed
Related Reports
Blocks :  
Relates :  
Relates :  
Relates :  
Description
### Failure analysis ###
Two components in `CodeCache::initialize_heaps` may cause the error:
1. The minimum size (`min_size = os::vm_page_size();`) for profiled and non-profiled code heaps is sometimes not large enough. Specifically, when `profiled_size` and/or `non_profiled_size` are set to `min_size` and `alignment > os::vm_page_size()`, the `align_down` below (from `CodeCache::initialize_heaps`) sets the size to 0.
      ```
      const size_t alignment = MAX2(ps, os::vm_allocation_granularity());
      non_nmethod_size = align_up(non_nmethod_size, alignment);
      profiled_size    = align_down(profiled_size, alignment);
      non_profiled_size = align_down(non_profiled_size, alignment);
      ```
2. The calculation for default code heap sizes does not consider the edge case when `cache_size > non_nmethod_size > cache_size - 2 * min_size`.

### Original report ###
Working on JDK-8316653 exposed a code cache allocation bug. The bug appears for large values of `-XX:NMethodSizeLimit` (requires a debug build) but also depends on the underlying system.

Initially, I encountered the bug on a Windows server 2016 (x64) machine, running `java -XX:NMethodSizeLimit=351658240` (but any value larger than ReservedCodeCacheSize, approximately, will do). Output:
```
Error occurred during initialization of VM
Could not reserve enough space in CodeHeap 'profiled nmethods' (0K)
```

You can also run `java -XX:NMethodSizeLimit=351658240 -XX:+UseTransparentHugePages` to reproduce on Linux.

By careful tweaking of the flag, I managed to also trigger a separate but related edge case and reproduce the bug on my Linux system. Output for `java -XX:NMethodSizeLimit=224001703` on my Linux system:
```
Error occurred during initialization of VM
Could not reserve enough space in CodeHeap 'profiled nmethods' (0K)
```

The expected behavior is to gracefully handle all possible values of the flag and not exit early on VM initialization.

Comments
Changeset: 7df73a23 Author: Daniel Lundén <daniel.lunden@oracle.com> Committer: Roberto Castañeda Lozano <rcastanedalo@openjdk.org> Date: 2023-11-14 07:49:35 +0000 URL: https://git.openjdk.org/jdk/commit/7df73a23d3b459b3358c460466ebfe0fd942c99d
14-11-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/16373 Date: 2023-10-26 09:33:16 +0000
26-10-2023

ILW = Error during VM init (with debug builds), with extreme value of debug flag, no workaround but lower flag value = MLH = P4
26-10-2023