Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

IO::Buffer::Storage#free

free → self

If the buffer references memory, release it back to the operating system.

  • for a mapped buffer (e.g. from file): unmap.

  • for a buffer created from scratch: free memory.

  • for a buffer created from string: undo the association.

After releasing any referenced memory, the buffer is reset to a valid, empty, null state. It has no backing storage and its size is zero. Zero-length operations remain valid, while operations requiring bytes fail normal bounds checking.

Repeated calls on an unlocked, unfrozen Storage are harmless and return self. You can resize the buffer to allocate new storage. This method is not available on Slice, which never manages storage.

Example
buffer = IO::Buffer.for('test')
buffer.free
# => #<IO::Buffer 0x0000000000000000+0 NULL>

buffer.null?      # => true
buffer.empty?     # => true
buffer.valid?     # => true
buffer.get_string # => ""

buffer.get_value(:U8, 0) # raises ArgumentError

A frozen buffer cannot be freed, as that would release the memory its contents live in:

Example
buffer = IO::Buffer.for('test').freeze
buffer.free
# in `free': can't modify frozen IO::Buffer (FrozenError)