I was using less to examine a rather large file (8GB on a machine with 4GB RAM). I told it to "scroll to the end" (shift+G) and it ran for quite awhile then was killed for using up RAM. I thought less
wouldn't use more than a limited amount of RAM am I missing something?
1 Answer
The default amount of memory used by less
depends on whether it is reading a file or from a pipe; it's not clear what happens when reading redirected input from a file (less < FilePath
instead of less FilePath
).
The following excerpt from the manual page (on Ununtu 16.04) shows how to control the memory which less
allocates:-
-bn or --buffers=n
Specifies the amount of buffer space less will use for each file, in units of kilobytes (1024 bytes). By default 64 K of buffer space is used for each file (unless the file is a pipe; see the -B option). The -b option specifies instead that n kilobytes of buffer space should be used for each file. If n is -1, buffer space is unlimited; that is, the entire file can be read into memory.
-B or --auto-buffers
By default, when data is read from a pipe, buffers are allocated automatically as needed. If a large amount of data is read from the pipe, this can cause a large amount of memory to be allocated. The -B option disables this automatic allocation of buffers for pipes, so that only 64 K (or the amount of space specified by the -b option) is used for the pipe. Warning: use of -B can result in erroneous display, since only the most recently viewed part of the piped data is kept in memory; any earlier data is lost.
I read this as meaning that, when reading from a pipe, -b is not used unless -B is also an option. If you want this specified on every run, then add export LESS='-B'
to your environment (in bash
this would be a line in ~/.bashrc
).
Note that this will limit the memory less
tries to allocate, whether memory is constrained by inadequate swap space or by the 4GB maximum program space on a 32-bit OS.
less
's memory requirements.-B
it doesn't use "unlimited RAM" but if I specify--buffers=64
it uses up all memory in the system (despite the fact it's reading from a file, not a pipe). Anyway-B
fixes it, feel free to create an answer, I'll accept it.