does vim read the whole file into memory
Asked Answered
T

2

8

When I open a file, does vim read it all into memory? I experienced significant slowdowns when I open large files. Or is it busy computing something (e.g., line number)?

Tuttle answered 20/9, 2012 at 18:30 Comment(0)
T
3

Yes, Vim loads the whole file into memory.

If you run htop in another pane you can watch it happen in real time.

If you don't have enough memory available, it will start hitting the swap which makes it take even longer.

You can disable plugins and heavy features (like syntax highlighting) to get improved performance (the -u effectively tells vim not to load your ~/.vimrc):

vim -u NONE mysqldump.sql

However, unless you really need to edit the file, I prefer to just use a different tool. I typically use less. I mostly search the files in vim with / and less supports that just fine.

less mysqldump.sql

Teleutospore answered 9/2, 2021 at 18:16 Comment(1)
Vim does not load the whole file into memory. As of vim 8.2 / nvim 0.6.1, this is not true. While they map the whole file (as seen via pmap <PID>), they do not physically load it into memory unless used. And if used, vim only maps relevant segments of the file. See via valgrind --tool=massif vim ... and ms_print. So vim does not use up physical memory unless needed, and does not do it on it's own after opening large file. (However, certain plugins and features might attempt to index the file or otherwise process it, causing slowdown)Perilymph
S
2

Disabling features like syntax highlighting, cursorline, line numbers and so on will greatly reduce the load and make Vim snappier in these cases.

There's even a plugin to handle that for you and a Vim tip for some background info.

Sinistral answered 20/9, 2012 at 18:46 Comment(2)
Thanks a lot for the advice. It's useful to me. But I still want to know what I asked originally.Tuttle
Yes, the file is loaded in memory.Sinistral

© 2022 - 2024 — McMap. All rights reserved.