1

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?

6
  • 1
    Have you got an adequate swap partition (or file)? Are you using a 32-bit OS?
    – AFH
    Mar 23, 2018 at 18:05
  • Basically no swap. But shouldn't less...you know...not need to read the whole file into memory, that's my question mark here... :)
    – rogerdpack
    Mar 23, 2018 at 18:17
  • I think it probably does read the whole file into memory, which is obviously impossible with your system. Try creating and enabling a swap file as described here, but don't bother if you have a 32-bit OS.
    – AFH
    Mar 23, 2018 at 18:20
  • See this link for information on less's memory requirements.
    – AFH
    Mar 23, 2018 at 18:29
  • Interestingly, if I use -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.
    – rogerdpack
    Mar 23, 2018 at 19:19

1 Answer 1

2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .