RFR: 8357086: os::xxx functions returning memory size should return size_t [v18]

Anton Artemov duke at openjdk.org
Fri Aug 1 09:11:55 UTC 2025


On Thu, 31 Jul 2025 15:23:51 GMT, Stefan Karlsson <stefank at openjdk.org> wrote:

>> Okay, makes sense, it looks like a failure of `os::physical_memory()` is something very unlikely to happen. So I made this function `void`, but, to be consistent with the rest, it provides the value by writing to the input parameter.
>
> 1) I had hoped for some kind of sanity check in the initialization code that sets up _physical_memory. We can handle that as a separate RFE.
> 
> 2) I really would prefer if os::physical_memory() returned size_t via the normal means and only use output parameters for the functions that require more than one return value. And on that note ...
> 
> 3) Do the other functions (free_memory and available_memory) ever fail or could we fix those as well? Windows and AIX are using the same APIs that are used for os::physical_memory. Linux uses sysinfo, which only seems to fail if we pass in an incorrect value. BSD has an assert that would trigger if this fails.

1) As for sanity checks:

- In `os::init()` in os_linux.cpp there is a sanity check for sysconf. It is done just before calling` Linux::initialize_system_info()` where `_physical_memory` is set. 

- In `os::Aix::initialize_system_info()` there is already a sanity check.

- In `os::Bsd::initialize_system_info()` there is a check for success of `sysctl`, but a default value 256 * 1024 * 1024 is assigned to `_physical_memory`. I do not know why it is done this way. Maybe this needs to be addressed separately.

- In `os::win32::initialize_system_info()` there is a call to `GlobalMemoryStatusEx`, but above we've came to conclusion that it never fails. 

So all OS sysinfo init routines have a sort of sanity check, except BSD, but I guess it was done intentionally. 

2) Addressed.

3) As for the rest of the methods:

- `available_memory()` can fail on Linux only if sysinfo fails, which can happen. Since it can fail on one OS, then the function should return both success/failure and the resulting value.
- `used_memory()` is never used in the code. Maybe it should be completely removed? 
- `free_memory()`: the same argument applies as to  `available_memory()`.
- `total_swap_space()`: the same argument applies as to  `available_memory()`.
- `free_swap_space()`: the same argument applies as to  `available_memory()`.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/25450#discussion_r2247391028


More information about the hotspot-dev mailing list