Memory usage when reading large files

I’m reading a file from disk(~570 MB) using NewScanner() function from bufio package and appending lines returned by Text() to a string slice. I wanted to see the memory consumption of my program so I ran the top command in linux and to my surprise the resident memory taken by my code hovers around 950MB-1GB. I understand that since I’ve read the contents of the whole file into a string slice, the compiler has to allocate memory equal to that of the file to hold the contents in RAM, but where has the additional ~400MB memory gone to?
My function-

This is probably how slices grow. But that’s just an assumption, also this could be because some stuff not yet collected by the GC.

If you know in advance the number of lines, try to set the initial capacity of your slice, perhaps that helps?

This question is a copy of
Memory usage when reading large files
That was posted 17 days ago.

Huh… That is very odd :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.