리눅스에서 버퍼와 캐시 메모리의 차이점은 무엇입니까?
나에게 두 개의 리눅스 메모리 개념의 차이 무엇 분명하지 않다 : buffer
와 cache
. 이 게시물을 읽었 으며 그들 사이의 차이점은 만료 정책 인 것 같습니다.
- 버퍼 정책은 선입 선출입니다
- 캐시 정책은 가장 최근에 사용한 것입니다.
내가 맞아?
특히, 나는 두 명령에서 찾고 : free
및vmstat
james@utopia:~$ vmstat -S M
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
5 0 0 173 67 912 0 0 19 59 75 1087 24 4 71 1
james@utopia:~$ free -m
total used free shared buffers cached
Mem: 2007 1834 172 0 67 914
-/+ buffers/cache: 853 1153
Swap: 2859 0 2859
"버퍼"는 캐시 디스크 블록 전용 RAM의 양을 나타냅니다. "캐시 됨"은 "버퍼"와 유사하지만 이번에는 파일 읽기에서 페이지를 캐시합니다.
인용 :
버퍼는 특정 블록 장치와 관련이 있으며 파일 시스템 메타 데이터의 캐싱과 기내 페이지 추적을 포함합니다. 캐시에는 파킹 된 파일 데이터 만 포함됩니다. 즉, 버퍼는 디렉토리의 내용, 파일 사용 권한을 기억하고 특정 블록 장치에 대해 쓰거나 읽은 메모리를 추적합니다. 캐시에는 파일 자체의 내용 만 포함됩니다.
짧은 대답 : 캐시는 페이지 캐시의 크기입니다. 버퍼는 메모리 내 블록 I / O 버퍼의 크기입니다. 캐시 된 사항; 버퍼는 크게 관련이 없습니다.
긴 답변 : 캐시 됨은 스왑 캐시의 메모리를 뺀 Linux 페이지 캐시의 크기이며, 스왑 캐시 (SwapCached)로 표시됩니다 (따라서 총 페이지 캐시 크기는 캐시 된 + 스왑 캐시 됨). Linux는 페이지 캐시를 통해 모든 파일 I / O를 수행합니다. 쓰기는 단순히 페이지 캐시에서 해당 페이지를 더티로 표시하는 것으로 구현됩니다. 그런 다음 플러 셔 스레드는 정기적으로 더티 페이지를 디스크에 다시 씁니다. 읽기는 페이지 캐시에서 데이터를 반환하여 구현됩니다. 데이터가 아직 캐시에 없으면 먼저 채워집니다. 최신 Linux 시스템에서 캐시는 쉽게 몇 기가 바이트가 될 수 있습니다. 메모리 압력에 따라 축소됩니다. 시스템은 페이지 캐시를 제거하고 필요에 따라 더 많은 메모리를 사용할 수 있도록 데이터를 디스크로 스왑 아웃합니다.
버퍼는 메모리 내 블록 I / O 버퍼입니다. 상대적으로 수명이 짧습니다. Linux 커널 버전 2.4 이전에는 Linux에 별도의 페이지 및 버퍼 캐시가있었습니다. 2.4 이후로, 페이지 및 버퍼 캐시가 통합되고 버퍼는 페이지 캐시에 표시되지 않은 원시 디스크 블록 (파일 데이터가 아님)입니다. 따라서 버퍼 메트릭은 매우 중요합니다. 대부분의 시스템에서 버퍼는 종종 수십 메가 바이트에 불과합니다.
이처럼 간단하지는 않지만 이해하는 데 도움이 될 수 있습니다.
버퍼는 파일 메타 데이터 (권한, 위치 등)를 저장하기위한 것입니다. 모든 메모리 페이지는 여기에서 추적됩니다.
캐시는 실제 파일 내용을 저장하기위한 것입니다.
버퍼 및 캐시.
버퍼는 아직 디스크에 "쓰기"되지 않은 것입니다.
캐시는 디스크에서 "읽고"나중에 사용하기 위해 저장 한 것입니다.
RedHat에 의해 설명 :
캐시 페이지 :
캐시는 메모리의 일부로서 데이터를 투명하게 저장하여 향후 해당 데이터에 대한 요청을 더 빨리 처리 할 수 있습니다. 이 메모리는 커널에서 디스크 데이터를 캐시하고 i / o 성능을 향상시키는 데 사용됩니다.
Linux 커널은 로컬 및 원격 파일 시스템 및 디스크에서 정보를 캐시하기 위해 가능한 한 많은 RAM을 사용할 수 있도록 구축되었습니다. 시간이 시스템에서 다양한 읽기 및 쓰기가 수행됨에 따라 커널은 시스템에서 실행중인 다양한 프로세스 또는 가까운 미래에 사용될 관련 프로세스의 데이터를 위해 메모리에 저장된 데이터를 유지하려고합니다. 프로세스가 중지 / 종료 될 때 캐시는 회수되지 않지만, 다른 프로세스에 사용 가능한 메모리보다 많은 메모리가 필요한 경우 커널은 캐시 데이터를 저장하고 해당 메모리를 새 프로세스에 할당하여 메모리를 회수하기 위해 휴리스틱을 실행합니다.
모든 종류의 파일 / 데이터가 요청되면 커널은 사용자가 작업중인 파일의 일부 복사본을 찾고, 그러한 복사본이 존재하지 않으면 캐시 메모리의 새 페이지를 하나 할당하여 채 웁니다. 디스크에서 적절한 내용을 읽습니다.
캐시에 저장된 데이터는 이전에 계산 된 값이거나 디스크의 다른 곳에 저장된 원래 값의 복제본 일 수 있습니다. 일부 데이터가 요청되면 캐시에 먼저 해당 데이터가 포함되어 있는지 확인합니다. 데이터는 소스 출처보다 캐시에서 더 빨리 검색 할 수 있습니다.
SysV 공유 메모리 세그먼트는 디스크의 데이터를 나타내지 않지만 캐시로 간주됩니다. ipcs -m 명령을 사용하고 bytes 열을 확인하여 공유 메모리 세그먼트의 크기를 확인할 수 있습니다.
버퍼 :
Buffers are the disk block representation of the data that is stored under the page caches. Buffers contains the metadata of the files/data which resides under the page cache. Example: When there is a request of any data which is present in the page cache, first the kernel checks the data in the buffers which contain the metadata which points to the actual files/data contained in the page caches. Once from the metadata the actual block address of the file is known, it is picked up by the kernel for processing.
I think this page will help understanding the difference between buffer and cache deeply. http://www.tldp.org/LDP/sag/html/buffer-cache.html
Reading from a disk is very slow compared to accessing (real) memory. In addition, it is common to read the same part of a disk several times during relatively short periods of time. For example, one might first read an e-mail message, then read the letter into an editor when replying to it, then make the mail program read it again when copying it to a folder. Or, consider how often the command ls
might be run on a system with many users. By reading the information from disk only once and then keeping it in memory until no longer needed, one can speed up all but the first read. This is called disk buffering, and the memory used for the purpose is called the buffer cache.
Since memory is, unfortunately, a finite, nay, scarce resource, the buffer cache usually cannot be big enough (it can't hold all the data one ever wants to use). When the cache fills up, the data that has been unused for the longest time is discarded and the memory thus freed is used for the new data.
Disk buffering works for writes as well. On the one hand, data that is written is often soon read again (e.g., a source code file is saved to a file, then read by the compiler), so putting data that is written in the cache is a good idea. On the other hand, by only putting the data into the cache, not writing it to disk at once, the program that writes runs quicker. The writes can then be done in the background, without slowing down the other programs.
Seth Robertson's Link 2 said "For thorough understanding of those terms, refer to Linux kernel book like Linux Kernel Development by Robert M. Love."
I found some contents about 'buffer' in the 2nd edition of the book.
Although the physical device itself is addressable at the sector level, the kernel performs all disk operations in terms of blocks.
When a block is stored in memory (say, after a read or pending a write), it is stored in a 'buffer'. Each 'buffer' is associated with exactly one block. The 'buffer' serves as the object that represents a disk block in memory.
A 'buffer' is the in-memory representation of a single physical disk block.
Block I/O operations manipulate a single disk block at a time. A common block I/O operation is reading and writing inodes. The kernel provides the bread() function to perform a low-level read of a single block from disk. Via 'buffers', disk blocks are mapped to their associated in-memory pages. "
Buffer contains metadata which helps improve write performance
Cache contains the file content itself (sometimes yet to write to disk) which improves read performance
Quote from the book: Introduction to Information Retrieval
Cache
We want to keep as much data as possible in memory, especially those data that we need to access frequently. We call the technique of keeping frequently used disk data in main memory caching.
Buffer
Operating systems generally read and write entire blocks. Thus, reading a single byte from disk can take as much time as reading the entire block. Block sizes of 8, 16, 32, and 64 kilobytes (KB) are common. We call the part of main memory where a block being read or written is stored a buffer.
A buffer is a region of memory used to temporarily hold data while it is being moved from one place to another within a computer.while a cache is a temporary storage area where frequently accessed data can be stored for rapid access. Once the data is stored in the cache, future use can be made by accessing the cached copy rather than re-fetching the original data, so that the average access time is shorter.
'IT' 카테고리의 다른 글
부트 스트랩이있는 고정 너비 버튼 (0) | 2020.05.31 |
---|---|
Bash 스크립트에 전달 된 인수 수를 어떻게 찾습니까? (0) | 2020.05.29 |
더 우아한“ps aux | (0) | 2020.05.29 |
Java String-문자열에 문자가 아닌 숫자 만 포함되어 있는지 확인 (0) | 2020.05.29 |
문자열의 마지막 문자는 어떻게 얻습니까? (0) | 2020.05.29 |