The reason is simple, readFileAlloc() does not querry the file size when reading.
It just initializes arraylist and then streams the data into it. However this arraylist grows independant to the file size (it will overgrow it).
Here is the growing logic for arraylist:
/// Called when memory growth is necessary. Returns a capacity larger than
/// minimum that grows super-linearly.
pub fn growCapacity(minimum: usize) usize {
if (@sizeOf(T) == 0) return math.maxInt(usize);
const init_capacity: comptime_int = @max(1, std.atomic.cache_line / @sizeOf(T));
return minimum +| (minimum / 2 + init_capacity);
}
If you need to have the allocation consume exactly the file size memory, querry the file size, allocate buffer of that size and then read into it.
If you need anything more explained, feel free to ask.
Robert ![]()