Skip to content

8364248: Separate memory limit detection #26530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jsikstro
Copy link
Member

@jsikstro jsikstro commented Jul 29, 2025

The function os::has_allocatable_memory_limit() is intended to determine whether there is a system-imposed limit on how much memory can be committed, and if so, what that limit is. On POSIX systems, limiting committable memory is typically enforced by restricting the available virtual address space, such as via RLIMIT_AS. As a result, os::has_allocatable_memory_limit() tells us both how much memory can be committed and how much virtual address space is available. On Windows however, os::has_allocatable_memory_limit() always returns true, along with the size of the available virtual address space. This is misleading because it is not possible to limit how much memory can be committed via virtual address space, and also the virtual address space cannot be limited.

ZGC currently uses os::has_allocatable_memory_limit() to check if the virtual address space is limited. To make it clear that the virtual address space cannot be limited on Windows, I propose that we create a new function called os::has_limited_virtual_address_space() which simply returns false on Windows, since the virtual address space cannot be limited there.

As a follow-up, I think it is reasonable to re-visit the implementation of os::has_allocatable_memory_limit() on Windows, since it doesn't follow any user-set limits, apart from how much virtual memory is available. Perhaps looking at limit(s) set by Job Objects could be more fruitful, and would improve the support for native Windows containers (Hyper-V).

Testing:

  • Oracle's tier1-2
  • Manual testing on Linux by limiting the virtual address space:
$ ulimit -v 8388608 && java -XX:+UseZGC -Xlog:gc+init -version

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8364248: Separate memory limit detection (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26530/head:pull/26530
$ git checkout pull/26530

Update a local copy of the PR:
$ git checkout pull/26530
$ git pull https://git.openjdk.org/jdk.git pull/26530/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26530

View PR using the GUI difftool:
$ git pr show -t 26530

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26530.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 29, 2025

👋 Welcome back jsikstro! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jul 29, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 29, 2025
@openjdk
Copy link

openjdk bot commented Jul 29, 2025

@jsikstro The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@jsikstro
Copy link
Member Author

/cc hotspot-gc

@openjdk
Copy link

openjdk bot commented Jul 29, 2025

@jsikstro
The hotspot-gc label was successfully added.

@mlbridge
Copy link

mlbridge bot commented Jul 29, 2025

Webrevs

Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally good. But why not simply return the limit. Unlimited = size_max.

Oh, I see ZGC does that already. I would adopt that for has_limited_virtual_address_space. Then you can remove the ZGC implementation completely.

Side note, to be unbearably nitpicky, it should theoretically be "address space limit - os::vm_min_address()" since the OS does not allow low-address mappings below that.

@jsikstro
Copy link
Member Author

Generally good. But why not simply return the limit. Unlimited = size_max.

Oh, I see ZGC does that already. I would adopt that for has_limited_virtual_address_space. Then you can remove the ZGC implementation completely.

This would make the ZGC could read more nicely I think. I like it!

Side note, to be unbearably nitpicky, it should theoretically be "address space limit - os::vm_min_address()" since the OS does not allow low-address mappings below that.

Do you think it would be prudent to add this now? To clarify, if there is a limit on POSIX, return (size_t)rlim.rlim_cur - vm_min_address()?

@tstuefe
Copy link
Member

tstuefe commented Jul 29, 2025

Generally good. But why not simply return the limit. Unlimited = size_max.
Oh, I see ZGC does that already. I would adopt that for has_limited_virtual_address_space. Then you can remove the ZGC implementation completely.

This would make the ZGC could read more nicely I think. I like it!

Side note, to be unbearably nitpicky, it should theoretically be "address space limit - os::vm_min_address()" since the OS does not allow low-address mappings below that.

Do you think it would be prudent to add this now? To clarify, if there is a limit on POSIX, return (size_t)rlim.rlim_cur - vm_min_address()?

Nah, this was a half joke tbh. I don't think this fidelity is needed, and it may confuse people if we get a weirdly unaligned size back. E.g. if on Linux, the min map is configured to be just one page, you could get something like "4PB - 4K".

Also, you won't ever be able to use the full extend of that size anyway, since of course it is pre-populated with all kind of mappings, is fragmented, etc.

Side note, it would be valuable to have a sister function to os::vm_min_address() that returns the maximum mappable user address. I did hard code values at various places, but I believe ZGC has some function like this that is more elegant and more truthful. Would be nice to have this in os space.

@jsikstro
Copy link
Member Author

@stefank suggested to rename has_allocatable_memory_limit() to allocatable_memory_limit() while we're on this. This is in line with what @toxaart is doing for multiple os:: functions in #25450.

@tstuefe
Copy link
Member

tstuefe commented Jul 29, 2025

I have some second thoughts about this. Sorry for the bikeshedding, but naming is important, especially since we never bother with comments on prototypes.

"allocatable_memory_limit" implies much more than it delivers. How much memory you can allocate depends on many factors, only one of which being the user-addressable address space size.

For example, how much you can commit depends on OS, swap space size, OS-specific overcommit behavior etc. How much you can really use depends on how much memory you really have and how the OS is willing to give you. Etc.

I think it would be easier to clearly name this function as "address_space_limit" or similar, since that's what it is doing.

@jsikstro
Copy link
Member Author

Just want to start of by saying thank you for taking the time with this.

I have some second thoughts about this. Sorry for the bikeshedding, but naming is important, especially since we never bother with comments on prototypes.

"allocatable_memory_limit" implies much more than it delivers. How much memory you can allocate depends on many factors, only one of which being the user-addressable address space size.

For example, how much you can commit depends on OS, swap space size, OS-specific overcommit behavior etc. How much you can really use depends on how much memory you really have and how the OS is willing to give you. Etc.

I think it would be easier to clearly name this function as "address_space_limit" or similar, since that's what it is doing.

I agree that the naming is strong, but I don't see an issue with that. Like you're saying, on Linux (maybe all POSIX systems?), you can never commit more memory than you have virtual address space, regardless of OS, swap space size, and overcommit behavior. So a strong name for a strong upper-bound seems reasonable to me.

On POSIX, we only really need an os::address_space_limit(), since it answers both how much memory we can commit and the size of the virtual address space. On Windows, we need an os::allocatable_memory_limit(), which would answer how much memory we can commit, which can be adjusted by the user, unlike the size of the virtual address space, which cannot be artificially limited. So I think we need both, so that both can be called from both OS's.

bool os::allocatable_memory_limit(size_t* limit) {
// On POSIX systems, the amount of allocatable memory is limited by the
// size of the virtual address space.
*limit = address_space_limit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In @toxaart's PR the out parameter is never set if the return value is false. I think the reason why that was done was so that code could be written like this:

size_t value = 0;
if (os::memory_function(value)) {
  // Succeeded - do stuff
} else {
  // Failed - do other stuff
}

// Use 0 if the function returned an error
use(value);

I think we should strive for unification around that in the various os:: memory functions. I also think that he used & in his patch and this function uses a pointer with the output parameter. Unification around would be good as well.

@tstuefe
Copy link
Member

tstuefe commented Jul 29, 2025

Just want to start of by saying thank you for taking the time with this.

I have some second thoughts about this. Sorry for the bikeshedding, but naming is important, especially since we never bother with comments on prototypes.
"allocatable_memory_limit" implies much more than it delivers. How much memory you can allocate depends on many factors, only one of which being the user-addressable address space size.
For example, how much you can commit depends on OS, swap space size, OS-specific overcommit behavior etc. How much you can really use depends on how much memory you really have and how the OS is willing to give you. Etc.
I think it would be easier to clearly name this function as "address_space_limit" or similar, since that's what it is doing.

I agree that the naming is strong, but I don't see an issue with that. Like you're saying, on Linux (maybe all POSIX systems?), you can never commit more memory than you have virtual address space, regardless of OS, swap space size, and overcommit behavior. So a strong name for a strong upper-bound seems reasonable to me.

On POSIX, we only really need an os::address_space_limit(), since it answers both how much memory we can commit and the size of the virtual address space. On Windows, we need an os::allocatable_memory_limit(), which would answer how much memory we can commit, which can be adjusted by the user, unlike the size of the virtual address space, which cannot be artificially limited. So I think we need both, so that both can be called from both OS's.

Maybe it depends on what you need and what question it should answer. Which I don't know. If the question is "what do you think I could allocate at most, were I to try now?"), then yes, allocatableMemory would be good name. It will always be a guess anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants