我很好奇。
您可以less
毫無問題地讀取 100 GB 檔案。但是如果你cat
將一個 100 GB 的文件放入less
,我認為less
必須緩衝整個文件,因為文件和 less 之間有一個管道(是 64K 嗎?)。
那麼,有人知道 中的緩衝策略嗎less
,它會從頭開始緩衝直到記憶體耗盡嗎?
答案1
的線上說明頁less
涵蓋了以下主題:
-bn or --buffers=n
Specifies the amount of buffer space less will use for each file,
in units of kilobytes (1024 bytes). By default 64K 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 buf‐
fer 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 64K (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.
線上說明頁暗示,如果不透過 -B 和 -b 選項限制緩衝區,緩衝區最終將成長到與整個輸入一樣大。