Feature #22704
Updated by himura467 (Akito Shitara) about 22 hours ago
## Description
`IO::Buffer` allocates its memory with [`calloc`](https://github.com/ruby/ruby/blob/215a554542bc7835cd72d966a7de380f048bebb8/io_buffer.c#L262) or [`mmap`](https://github.com/ruby/ruby/blob/215a554542bc7835cd72d966a7de380f048bebb8/io_buffer.c#L265), and never calls `rb_gc_adjust_memory_usage`. A buffer object takes one slot, so dropping buffers adds almost no GC pressure, and their memory comes back only when a GC happens to run for other reasons.
```ruby
500.times do
buffer = IO::Buffer.new(4 * 1024 * 1024)
buffer.clear(1) # touches every page
buffer.free if ARGV[0] == "free"
end
puts "GC.count=#{GC.count}"
```
```
$ /usr/bin/time -l ruby io_buffer_new_gc.rb
GC.count=0
...
2112880640 maximum resident set size
...
$ /usr/bin/time -l ruby io_buffer_new_gc.rb free
GC.count=0
...
19988480 maximum resident set size
...
```
Measured with `ruby 4.1.0dev (2026-12-02T19:57:20Z master b4c3350db2) +PRISM [arm64-darwin27]`.
Strings of the same size are allocated through `ruby_xmalloc`, count toward the malloc limit, and are collected:
```ruby
500.times { "\x01".b * (4 * 1024 * 1024) }
puts "GC.count=#{GC.count}"
```
```
$ /usr/bin/time -l ruby string_gc.rb
GC.count=63
...
88080384 maximum resident set size
...
```
`ObjectSpace.memsize_of` already counts the allocation ([`rb_io_buffer_type_size`](https://github.com/ruby/ruby/blob/215a554542bc7835cd72d966a7de380f048bebb8/io_buffer.c#L369)); only the GC's trigger does not see it.
## Proposal
Report the memory each buffer owns with `rb_gc_adjust_memory_usage` (file mappings are discussed below): its size when it is allocated, its negated size when it is released, and the difference when it is resized in place (`realloc`, `mremap`). `#transfer` moves an allocation between buffers without changing the total, so it needs nothing.
## Which file mappings to report
Anonymous mappings (`IO::Buffer.new`) are reported in full. File mappings (`IO::Buffer.map`) need a decision:
- A shared mapping, or a read-only private one, maps the file's pages. They belong to the page cache, which the kernel can reclaim.
- A writable private mapping copies each page it writes. The copy is visible only to the process and does not change the file ([POSIX `mmap`](https://pubs.opengroup.org/onlinepubs/9799919799/functions/mmap.html)), so the kernel cannot reclaim it without swap. The buffer cannot count the copies: writes also come through pointers taken with the C API and from `read(2)`.
The options (as far as I came up with) are:
- **A.** Report no file mapping. A writable private mapping that is written heavily stays invisible to the GC.
- **B.** Report a writable private mapping in full, and no other file mapping. A mapping with few writes is overstated.
- **C.** Report every file mapping in full. Reclaimable page cache is counted.
I propose B, as it matches what the operating systems charge as commit. Linux charges a writable private file mapping for its size and a shared or read-only one nothing ([overcommit accounting](https://www.kernel.org/doc/html/latest/mm/overcommit-accounting.html)). Windows charges commit for the whole view of a `FILE_MAP_COPY` mapping ([`MapViewOfFile`](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile)), which [2519b093fb](https://github.com/ruby/ruby/commit/2519b093fbed018079b220d3d94908cea62d9090) recently stopped requesting for read-only private buffers.