ÉîÈë̽ÌÖLinuxµÄ»º´æ»úÖÆ£ºÌæ»»Ëã·¨ºÍÐÔÄÜÓÅ»¯Õ½ÂÔÏê½â
LinuxÊÇÒ»ÖÖÆÕ±éÓ¦ÓõIJÙ×÷ϵͳ£¬ÆäÇ¿Ê¢µÄÐÔÄÜÌåÏֹ鹦ÓÚÆ仺´æ»úÖÆ¡£±¾ÎĽ«ÏêϸÏÈÈÝLinuxµÄ»º´æ»úÖÆ£¬°üÀ¨»º´æÌæ»»Ëã·¨ºÍÐÔÄÜÓÅ»¯Õ½ÂÔ£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢»º´æÌæ»»Ëã·¨
»º´æÌæ»»Ëã·¨¾öÒéÁ˵±»º´æÈÝÁ¿È±·¦Ê±£¬ÔõÑùÑ¡Ôñ±»Ìæ»»µÄ»º´æ¿é¡£Linux³£ÓõĻº´æÌæ»»Ëã·¨Ö÷ÒªÓÐÒÔϼ¸ÖÖ£º
×î¾ÃδʹÓã¨LRU£©
×î¾ÃδʹÓÃËã·¨ÊÇÒ»ÖÖ³£¼ûµÄ»º´æÌæ»»Ëã·¨£¬ËüÒÔΪ×î½üûÓб»Ê¹ÓõĻº´æ¿éÔÚδÀ´Ò²²»Ì«¿ÉÄܱ»Ê¹Óõ½£¬Òò´ËÑ¡Ôñ×î¾ÃδʹÓõĻº´æ¿é¾ÙÐÐÌæ»»¡£LinuxÄÚºËÖеÄLRUËã·¨ÊÇͨ¹ýË«Á´±íʵÏֵģ¬Ã¿´Î»á¼û»º´æ¿éʱ£¬»á½«ÆäÒƶ¯µ½Á´±íÍ·²¿£¬×î¾ÃδʹÓõĻº´æ¿éÔòλÓÚÁ´±íβ²¿¡£
×î²»¾³£Ê¹Óã¨LFU£©
×î²»¾³£Ê¹ÓÃËã·¨ÊÇƾ֤ÿ¸ö»º´æ¿éµÄʹÓÃƵÂʾÙÐÐÌæ»»¡£Ê¹ÓÃƵÂʵ͵Ļº´æ¿é±»Ìæ»»µÄ¸ÅÂʸü´ó¡£LFUËã·¨ÐèÒªÔÚÿ¸ö»º´æ¿éÖмͼʹÓôÎÊý£¬Òò´ËÏà¹ØÓÚLRUËã·¨¶øÑÔ£¬ÊµÏÖÆðÀ´¸üΪÖØ´ó¡£
Ëæ»úËã·¨
Ëæ»úËã·¨ÊÇÒ»ÖÖ¼òÆÓÖ±¹ÛµÄ»º´æÌæ»»Ëã·¨£¬ËüËæ»úÑ¡ÔñÒ»¸ö»º´æ¿é¾ÙÐÐÌæ»»¡£ÕâÖÖËã·¨²»Ë¼Á¿»º´æ¿éµÄʹÓÃÇéÐΣ¬¿ÉÄܵ¼Ö»º´æÖÀÖÐÂʽϵ͡£
¶þ¡¢ÐÔÄÜÓÅ»¯Õ½ÂÔ
ΪÁËÌá¸ßLinuxµÄ»º´æÐÔÄÜ£¬»¹¿ÉÒÔ½ÓÄÉÒÔÏÂÕ½ÂÔ¾ÙÐÐÓÅ»¯£º
Ìá¸ß»º´æÖÀÖÐÂÊ
Ìá¸ß»º´æÖÀÖÐÂÊÊÇÌá¸ßLinux»º´æÐÔÄܵÄÒªº¦¡£¿ÉÒÔͨ¹ýµ÷½â»º´æ¾Þϸ¡¢ÓÅ»¯»º´æÌæ»»Ëã·¨¡¢ÔöÌí»º´æ¿éµÄԤȡµÈ·½·¨À´Ìá¸ß»º´æÖÀÖÐÂÊ¡£
ÀýÈ磬ÔÚLinuxÄÚºËÖпÉÒÔͨ¹ýÐÞ¸Ä/proc/sys/vm/dirty_ratioºÍ/proc/sys/vm/dirty_background_ratio²ÎÊýÀ´µ÷½âÔàÒ³£¨ÒÑÐ޸ĵ«Î´Ð´»Øµ½´ÅÅ̵ÄÒ³Ã棩µÄ±ÈÀý£¬ÒÔÌá¸ß»º´æµÄ¿ÉÓÿռ䡣
×èֹƵÈԵĻº´æʧЧ
ƵÈԵĻº´æʧЧ»áµ¼Ö½ϵ͵Ļº´æÖÀÖÐÂÊ£¬´Ó¶øÓ°ÏìϵͳÐÔÄÜ¡£¿ÉÒÔͨ¹ýÌáÇ°¼ÓÔس£ÓõÄÊý¾Ý¡¢ºÏÀíʹÓÃËøÀ´ïÔÌƵÈԵĻº´æʧЧ¡£
ÀýÈ磬ÔÚÎļþϵͳÖпÉÒÔʹÓÃÒ»ÖÂÐÔ¹þÏ£Ëã·¨À´ÂþÑÜÊý¾Ý£¬ÒÔ×èÖ¹Òò½ÚµãÀ©³ä»òËõ¼õµ¼ÖµĻº´æʧЧ¡£
ÕûÀíÓâÆڵĻº´æ
ÓâÆڵĻº´æÕ¼ÓÃÁËÃû¹óµÄÄÚ´æ×ÊÔ´£¬½µµÍÁË»º´æÖÀÖÐÂÊ¡£¿ÉÒÔʹÓð´ÆÚÕûÀíʹÃü»òÕßƾ֤ÄÚ´æѹÁ¦ÇéÐÎÀ´ÕûÀíÓâÆڵĻº´æ¡£
ÀýÈ磬ÔÚ×Öµä½á¹¹ÖпÉÒÔΪÿ¸ö»º´æ¿éÉèÖÃÒ»¸öÓâÆÚʱ¼ä£¬²¢ÔÚ»á¼û»º´æ¿éʱ¼ì²âÊÇ·ñÒÑÓâÆÚ£¬ÈôÓâÆÚÔòɾ³ý¡£
Èý¡¢Ïêϸ´úÂëʾÀý
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ÑÝʾÁËÔõÑùʹÓÃLRUË㷨ʵÏÖÒ»¸ö»º´æÌæ»»¹¦Ð§µÄ´úÂ룺
#include <stdio.h> #include <stdlib.h> typedef struct Node { int key; int value; struct Node* prev; struct Node* next; } Node; typedef struct LRUCache { int capacity; int size; Node* head; Node* tail; } LRUCache; LRUCache* createCache(int capacity) { LRUCache* cache = (LRUCache*)malloc(sizeof(LRUCache)); cache->capacity = capacity; cache->size = 0; cache->head = (Node*)malloc(sizeof(Node)); cache->tail = (Node*)malloc(sizeof(Node)); cache->head->prev = NULL; cache->head->next = cache->tail; cache->tail->prev = cache->head; cache->tail->next = NULL; return cache; } void deleteNode(LRUCache* cache, Node* node) { node->next->prev = node->prev; node->prev->next = node->next; free(node); } void addToHead(LRUCache* cache, Node* node) { node->next = cache->head->next; node->prev = cache->head; cache->head->next->prev = node; cache->head->next = node; } int get(LRUCache* cache, int key) { Node* node = cache->head->next; while (node != cache->tail) { if (node->key == key) { // hit, move to head node->prev->next = node->next; node->next->prev = node->prev; addToHead(cache, node); return node->value; } node = node->next; } return -1; // cache miss } void put(LRUCache* cache, int key, int value) { Node* node = cache->head->next; while (node != cache->tail) { if (node->key == key) { // hit, update value and move to head node->value = value; node->prev->next = node->next; node->next->prev = node->prev; addToHead(cache, node); return; } node = node->next; } if (cache->size >= cache->capacity) { // cache is full, remove least recently used item Node* tailNode = cache->tail->prev; tailNode->prev->next = cache->tail; cache->tail->prev = tailNode->prev; free(tailNode); cache->size--; } Node* newNode = (Node*)malloc(sizeof(Node)); newNode->key = key; newNode->value = value; addToHead(cache, newNode); cache->size++; } int main() { LRUCache* cache = createCache(3); put(cache, 1, 100); put(cache, 2, 200); put(cache, 3, 300); printf("%d ", get(cache, 2)); // Output: 200 put(cache, 4, 400); printf("%d ", get(cache, 1)); // Output: -1 printf("%d ", get(cache, 3)); // Output: 300 printf("%d ", get(cache, 4)); // Output: 400 return 0; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëʵÏÖÁËÒ»¸öLRU»º´æ£¬Í¨¹ýputºÍgetº¯Êý¿ÉÒÔÍù»º´æÖдæÈëºÍ¶ÁÈ¡Êý¾Ý¡£µ±»º´æÈÝÁ¿È±·¦Ê±£¬»áÑ¡Ôñ×î¾ÃδʹÓõĻº´æ¿é¾ÙÐÐÌæ»»¡£
½áÂÛ£º
LinuxµÄ»º´æ»úÖÆÊÇÌá¸ßϵͳÐÔÄܵÄÖ÷Òª×é³É²¿·Ö¡£ºÏÀíÑ¡Ôñ»º´æÌæ»»Ëã·¨ºÍ½ÓÄÉÐÔÄÜÓÅ»¯Õ½ÂÔ£¬¿ÉÒÔÌá¸ßLinux»º´æµÄÖÀÖÐÂʺÍÊÂÇéЧÂÊ¡£Í¨¹ý´úÂëʾÀý£¬ÎÒÃÇÏàʶÁËÔõÑùʹÓÃLRUË㷨ʵÏÖÒ»¸ö»º´æÌæ»»¹¦Ð§¡£²î±ðµÄÓ¦Óó¡¾°ºÍÐèÇó¿ÉÒÔÑ¡ÔñÊʺϵĻº´æËã·¨ºÍÓÅ»¯Õ½ÂÔ£¬ÒÔµÖ´ï×î¼ÑµÄÐÔÄÜÌåÏÖ¡£
ÒÔÉϾÍÊÇÉîÈë̽ÌÖLinuxµÄ»º´æ»úÖÆ£ºÌæ»»Ëã·¨ºÍÐÔÄÜÓÅ»¯Õ½ÂÔÏê½âµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡