Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about Linux Memory Types

Tags:

linux

memory

I have the following questions regarding Linux memory:

  1. I know that the active memory is the portion of memory which is most frequently accessed. But can someone explain me how linux considers a memory location to be utilised for active memory or inactive memory.

  2. What all components does active memory comprises of ? Is the disk/file cache considered as part of active memory.

  3. What is the difference between Buffers and Cached memory ?

like image 970
pradeepchhetri Avatar asked Mar 18 '13 21:03

pradeepchhetri


People also ask

How does Linux handle memory?

Linux uses demand paging to load executable images into a processes virtual memory. Whenever a command is executed, the file containing it is opened and its contents are mapped into the processes virtual memory.

What memory allocation does Linux use?

Linux provides a variety of APIs for memory allocation. You can allocate small chunks using kmalloc or kmem_cache_alloc families, large virtually contiguous areas using vmalloc and its derivatives, or you can directly request pages from the page allocator with alloc_pages .

How virtual memory is managed on Linux?

Linux supports virtual memory, that is, using a disk as an extension of RAM so that the effective size of usable memory grows correspondingly. The kernel will write the contents of a currently unused block of memory to the hard disk so that the memory can be used for another purpose.


1 Answers

I try to answer your questions:

(1)Basically speaking, your thoughts are correct. But the page cache implementation is complicated in Linux Kernel. The Linux Kernel uses LRU(Least Recently Used) algorithm to manage the page cache lists. There may be different memory zones in one Linux system, each zone maintains several LRU list, such as LRU_INACTIVE_ANON, LRU_ACTIVE_ANON(these two lists are for anonymous page caches), LRU_INACTIVE_FILE , LRU_ACTIVE_FILE(these two lists are for file page caches), LRU_UNEVICTABLE. These lists are maintained using LRU algorithm(added to the tail, remove from the head). And pages are transited between active list and inactive list according to the access frequency. Pages are added to the active list tail only when the page is accessed and it is residing in the inactive list. And if the active list becomes too larger, the pages which at the head of the active list will be moved to the inactive list tail. The page reclaiming happen on the inactive list, start from the head of the inactive list.

(2) Regular files read/write, block deivce files access, and memory-mapped files can all trigger Linux Kernel to generate the page caches, active or inactive. Also the malloc used in user-space process and user-space stack can trigger Linux Kernel to generate the page caches.

(3) Maybe I misunderstand your question, I guess you mean difference between buffer cache and page cache: Older version kernel use both buffer cache and page cache. Page cache are for file accesses(such as regular file accesses, memory-map file, block device file accesses), and buffer cache are for physical disk blocks accesses(normally the size of the physical disk block is less than one page, so several physical disk blocks can fill into one page). Although new version kernel is still using the buffer cache concept, new kernel implements the buffer cache based on page cache.

Or you mean difference between memory buffer and cpu cache, if so, the memory buffer is to speed up the disk/peripheral accesses, and the cpu cache is to speed up the memory accesses.

like image 196
tian_yufeng Avatar answered Oct 28 '22 02:10

tian_yufeng