Project

General

Profile

Actions

Feature #22704

open

Report IO::Buffer allocations to the GC

Feature #22704: Report IO::Buffer allocations to the GC

Added by himura467 (Akito Shitara) about 21 hours ago. Updated about 2 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:127798]

Description

Description

IO::Buffer allocates its memory with calloc or mmap, 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.

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:

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); 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), 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). Windows charges commit for the whole view of a FILE_MAP_COPY mapping (MapViewOfFile), which 2519b093fb recently stopped requesting for read-only private buffers.

Actions

Also available in: PDF Atom