vimpmap

Vim memory usage


I generated 100 files and open them in two cases as following:

#!/bin/bash

    N=$1
    N=${N:=10}
    
    for x in $(seq $N); do
            x=$(printf "%03d" $x)
            filename="w$x.txt"
    
            cat /dev/urandom | tr -dc '0-9a-zA-Z' | fold -w80 | head -n 10000 > $filename
    done;


1- vim -O w*txt
2- vim -o w*txt

After each case I execute pmap $(pidof vim) to see memory usage of vim, but I see different values. In first case pmap shows 67368K and in second case pmap show 38952K value.

My question is: what causes these differences? How can they be explained?


Solution

  • When you attempt to open more buffers than fit on the screen, Vim will not load all of them. Only as many as fit on the screen.

    On my machine, when I start Vim as vim -o sample?.txt from an 80×24 terminal emulator window, it will load only the first 11 files. When I make my terminal take up the entire screen, it loads 28 buffers.

    This is easily verified by using the :ls command, where the a flag indicates active (loaded and visible) buffers. Note the flag is missing from a certain item in the buffer list onward.

    80×24 terminal:

    :ls
      1 %a   "sample1.txt"                  line 1
      2  a   "sample2.txt"                  line 0
    <snip>
     11  a   "sampleb.txt"                  line 0
     12      "samplec.txt"                  line 0
    <and so on>
    

    fullscreen:

    :ls
      1 %a   "sample1.txt"                  line 1
      2  a   "sample2.txt"                  line 0
    <snip>
     28  a   "samplet.txt"                  line 0
     29      "sampleu.txt"                  line 0
    <and so on>
    

    I repeated the experiment with vim -O sample?.txt and got 30 and 34 (all files in my test), respectively, as results.

    To answer the question: only files that can be and are displayed are actually loaded into buffers. Only loaded buffers take up memory.

    The best documentation for this behavior that I managed to find (there may be better places) is below :help :al which states (among other things):

    [N] is the maximum number of windows to open. 'winheight' also limits the number of windows opened ('winwidth' if :vertical was prepended).

    Unfortunately, neither :help -o nor :help of the mentioned options clearly state that buffers may remain unloaded if they can't be displayed.

    Thinking of it, it's an optimization that does make sense. Why waste RAM and CPU to load a file that cannot be displayed?