cache linux system is too big, solutions for Linux in Cache memory usage is too high solution

linux buff / cache is too large, clear script

Three instructions:

sync

echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches

After you perform these three instructions to see through free -m command, free memory available immediately increased, buff / cache column values ​​decreased, indicating that memory is freed, but not always manually execute the three instructions, so I finally wrote a shell script, Linux open the regular tasks crond, check once free memory every morning, which execute three commands (Note: The system content 20G) when less than 4G.

Note: be sure to execute sync command before executing these three commands (Description:. Sync command to run the sync subroutine If you must stop the system, run the sync command to ensure the integrity of the file system .sync command of all unwritten system buffer is written to disk, comprising the modified I- the Node , the delayed block I / O read and write, and mapping file)

 

Solution (manual)

1. Modify the / proc / sys / vm / drop_caches, release Slab occupied cache memory space (refer drop_caches official documents):

 

[plain]  view plain  copy
 
 
 
 
  1. Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.  
  2. To free pagecache:  
  3. * echo 1 > /proc/sys/vm/drop_caches  
  4. To free dentries and inodes:  
  5. * echo 2 > /proc/sys/vm/drop_caches  
  6. To free pagecache, dentries and inodes:  
  7. * echo 3 > /proc/sys/vm/drop_caches  
  8. As this is a non-destructive operation, and dirty objects are notfreeable, the user should run "sync" first in order to make sure allcached objects are freed.  
  9. This tunable was added in 2.6.16.  

Note: The first sync command before executing these three commands

 

 

Solutions (automatic)

1, regular tasks to write shell scripts freemem.sh

 

[plain]  view plain  copy
 
 
 
 
  1. #! /bin/sh  
  2. used=`free -m | awk 'NR==2' | awk '{print $3}'`  
  3. free=`free -m | awk 'NR==2' | awk '{print $4}'`  
  4. echo "===========================" >> /app/memory/logs/mem.log  
  5. date >> /app/memory/logs/mem.log  
  6. echo "Memory usage before | [Use:${used}MB][Free:${free}MB]" >> /app/memory/logs/mem.log  
  7. if [ $free -le 4000 ] ; then  
  8.                 sync && echo 1 > /proc/sys/vm/drop_caches  
  9.                 sync && echo 2 > /proc/sys/vm/drop_caches  
  10.                 sync && echo 3 > /proc/sys/vm/drop_caches  
  11.                 used_ok=`free -m | awk 'NR==2' | awk '{print $3}'`  
  12.                 free_ok=`free -m | awk 'NR==2' | awk '{print $4}'`  
  13.                 echo "Memory usage after | [Use:${used_ok}MB][Free:${free_ok}MB]" >> /app/memory/logs/mem.log  
  14.                 echo "OK" >> /app/memory/logs/mem.log  
  15. else  
  16.                 echo "Not required" >> /app/memory/logs/mem.log  
  17. be  
  18. exit 1  

2, using the crontab -e command to edit the current user's crontab

 

 

[plain]  view plain  copy
 
 
 
 
  1. 0 6 * * * /usr/local/tomcat/sztFileFront/bin/freemem.sh  

 

Timing task of writing reference: http://www.jb51.net/article/15008.htm


3, restart the crond service

 

[plain]  view plain  copy
 
 
 
 
  1. /sbin/service crond restart  

4, to see whether the service crond restart successfully

 

 

[plain]  view plain  copy
 
 
 
 
  1. /sbin/service crond status  

 


Finally, the problem is solved. Regular tasks I set the 6 o'clock every morning to perform a freemem.sh script.
 
 

Linux in Cache memory usage is too high solution

 

 

On Linux systems, we often use the free command to view the system memory use. On a system of RHEL6, as Free command displays the contents of a state of something like this:

1.png

Here is the default display units kb, my server is 128G memory, so the numbers appear to be relatively large. This command is almost every person must be used Linux commands, but more so the command, the less it seems really understand people (I mean the less proportion). In general, the understanding of this command's output can be divided into several levels:

  1. Do not understand. The first reaction is that such people: God, with a lot of memory, more than 70 G, but I have almost no big running ah? Why is this so? Linux accounted for a good memory!

  2. I think very understanding. Such people are generally assessed will say: ah, my professional eye to see it, only with a memory about 17G, there are a lot of free memory available. buffers / cache occupied large, indicating that the system has the process been used to read and write files, but it does not matter, this memory when idle to use.

  3. Really understand. Such people's reaction but people feel most do not understand Linux, their reaction was: is this free show, well I know. What? You ask me these memory enough, I certainly do not know it! I know how special it how to write your program?

According to the contents of the current technical documentation on the Web, I believe most people should understand that Linux is in the second level. It was widely felt, buffers and cached memory occupied space can be released as a free space in the larger memory pressure when used. But really the case? Prior to demonstrate this problem, we first briefly describe buffers and cached what it means:

What is the buffer / cache?

buffer and cache are have different meanings under two terms are used excessive and unreasonable placed in the context of computer technology. In the Linux memory management, buffer here refers to the Linux memory: Buffer cache. Cache here refers to the Linux memory: Page cache. Translated into Chinese can be called the buffer cache and page cache. Historically, they are a (buffer) is used as a cache to write io equipment, while the other (cache) is used as read caching devices io, io equipment here, mainly referring to the block device file and ordinary file on the file system. But now, they have not the same meaning. In the current kernel, the name suggests is for the cache page cache memory page, it means that if there is a memory page allocation management, you can use the page cache as a cache to manage its use. Of course, not all memory is in pages (page) managed, many are managed for the block (block), this part of the memory usage If you want to cache function, are concentrated in the buffer cache to use. (From this point of view, is not called a block cache buffer cache renamed better?) However, not all blocks (block) has a fixed length, the length of the block on the system mainly based on block devices used by the decision, and the page X86 length in either 32-bit or 64-bit is 4k.

Understand the difference between these two sets of system cache, we can understand what exactly they can be used to do.

What is the page cache?

Page cache is mainly used as a cache file data on the file system to use, especially when the process there is a time for file read / write operations. If you think about it, you can map the file into memory as a system call: mmap is not very natural should also use page cache? In the current system implementation where, as a page cache is also another type of file caching device to use, so in fact the cache page cache is also responsible for most of the work block device file.

What is the buffer cache

Buffer cache is mainly used when the system is designed to block read and write device, the block data cache system to use. This means that some of the operations will use the buffer cache block cache, such as when we are in the format of the file system. Under normal circumstances, together with two cache system is used, such as when we write to a file, the contents page cache will be changed, and the buffer cache can be used to mark different page buffers, and which is a record buffer was changed. In this way, the kernel in the subsequent execution of dirty data write-back (writeback), would not the entire page write back, but just need to write back the modified part can be.

How to recycle cache?

Linux kernel when the memory will be depleted, triggering garbage collection work in order to release the much-needed memory to the memory used by the process. In general, this operation comes from the main memory to the release of the release buffer / cache is. Especially by using more cache space. Since it is mainly used for caching, just speed up the process of file read and write speeds at the time of memory enough, then in larger memory pressure, of course, it is necessary to empty the release of cache, and give the relevant process used as a free space. Therefore, under normal circumstances, we believe buffer / cache space can be released, this understanding is correct.

But this clear the cache work is not without cost. Understand the cache is doing can understand the clear cache must be consistent with the data in the cache data corresponding to the file, can the cache be released . So along with the cache cleared of behavior are generally system IO skyrocketed. Because data on whether the kernel cache in comparison to the corresponding data and hard disk file, and if not need to write back before you can recover.

Except when memory is exhausted outside can clear the cache, we can use the following file system cache clearing operation to manually trigger:

[root@tencent64 ~]# cat /proc/sys/vm/drop_caches 
1

the way is:

echo 1 > /proc/sys/vm/drop_caches

Of course, the value of this file can be set 1,2,3, respectively. Meaning that they represent are:

echo 1> / proc / sys / vm / drop_caches: Clears pagecache. 
echo 2> / proc / sys / vm / drop_caches: represents scavenged slab allocator object (including a cache directory entry and the inode cache). The slab allocator is a mechanism in the kernel memory management, cache data to achieve many of which are used in pagecache. 
echo 3> / proc / sys / vm / drop_caches: clears the cache object and pagecache slab dispenser.

After optimization shots are as follows:

2.png

Learn computer, all over the world are not afraid

 

On Linux systems, we often use the free command to view the system memory use. On a system of RHEL6, as Free command displays the contents of a state of something like this:

1.png

Here is the default display units kb, my server is 128G memory, so the numbers appear to be relatively large. This command is almost every person must be used Linux commands, but more so the command, the less it seems really understand people (I mean the less proportion). In general, the understanding of this command's output can be divided into several levels:

  1. Do not understand. The first reaction is that such people: God, with a lot of memory, more than 70 G, but I have almost no big running ah? Why is this so? Linux accounted for a good memory!

  2. I think very understanding. Such people are generally assessed will say: ah, my professional eye to see it, only with a memory about 17G, there are a lot of free memory available. buffers / cache occupied large, indicating that the system has the process been used to read and write files, but it does not matter, this memory when idle to use.

  3. Really understand. Such people's reaction but people feel most do not understand Linux, their reaction was: is this free show, well I know. What? You ask me these memory enough, I certainly do not know it! I know how special it how to write your program?

According to the contents of the current technical documentation on the Web, I believe most people should understand that Linux is in the second level. It was widely felt, buffers and cached memory occupied space can be released as a free space in the larger memory pressure when used. But really the case? Prior to demonstrate this problem, we first briefly describe buffers and cached what it means:

What is the buffer / cache?

buffer and cache are have different meanings under two terms are used excessive and unreasonable placed in the context of computer technology. In the Linux memory management, buffer here refers to the Linux memory: Buffer cache. Cache here refers to the Linux memory: Page cache. Translated into Chinese can be called the buffer cache and page cache. Historically, they are a (buffer) is used as a cache to write io equipment, while the other (cache) is used as read caching devices io, io equipment here, mainly referring to the block device file and ordinary file on the file system. But now, they have not the same meaning. In the current kernel, the name suggests is for the cache page cache memory page, it means that if there is a memory page allocation management, you can use the page cache as a cache to manage its use. Of course, not all memory is in pages (page) managed, many are managed for the block (block), this part of the memory usage If you want to cache function, are concentrated in the buffer cache to use. (From this point of view, is not called a block cache buffer cache renamed better?) However, not all blocks (block) has a fixed length, the length of the block on the system mainly based on block devices used by the decision, and the page X86 length in either 32-bit or 64-bit is 4k.

Understand the difference between these two sets of system cache, we can understand what exactly they can be used to do.

What is the page cache?

Page cache is mainly used as a cache file data on the file system to use, especially when the process there is a time for file read / write operations. If you think about it, you can map the file into memory as a system call: mmap is not very natural should also use page cache? In the current system implementation where, as a page cache is also another type of file caching device to use, so in fact the cache page cache is also responsible for most of the work block device file.

What is the buffer cache

Buffer cache is mainly used when the system is designed to block read and write device, the block data cache system to use. This means that some of the operations will use the buffer cache block cache, such as when we are in the format of the file system. Under normal circumstances, together with two cache system is used, such as when we write to a file, the contents page cache will be changed, and the buffer cache can be used to mark different page buffers, and which is a record buffer was changed. In this way, the kernel in the subsequent execution of dirty data write-back (writeback), would not the entire page write back, but just need to write back the modified part can be.

How to recycle cache?

Linux kernel when the memory will be depleted, triggering garbage collection work in order to release the much-needed memory to the memory used by the process. In general, this operation comes from the main memory to the release of the release buffer / cache is. Especially by using more cache space. Since it is mainly used for caching, just speed up the process of file read and write speeds at the time of memory enough, then in larger memory pressure, of course, it is necessary to empty the release of cache, and give the relevant process used as a free space. Therefore, under normal circumstances, we believe buffer / cache space can be released, this understanding is correct.

But this clear the cache work is not without cost. Understand the cache is doing can understand the clear cache must be consistent with the data in the cache data corresponding to the file, can the cache be released . So along with the cache cleared of behavior are generally system IO skyrocketed. Because data on whether the kernel cache in comparison to the corresponding data and hard disk file, and if not need to write back before you can recover.

Except when memory is exhausted outside can clear the cache, we can use the following file system cache clearing operation to manually trigger:

[root@tencent64 ~]# cat /proc/sys/vm/drop_caches 
1

the way is:

echo 1 > /proc/sys/vm/drop_caches

Of course, the value of this file can be set 1,2,3, respectively. Meaning that they represent are:

echo 1> / proc / sys / vm / drop_caches: Clears pagecache. 
echo 2> / proc / sys / vm / drop_caches: represents scavenged slab allocator object (including a cache directory entry and the inode cache). The slab allocator is a mechanism in the kernel memory management, cache data to achieve many of which are used in pagecache. 
echo 3> / proc / sys / vm / drop_caches: clears the cache object and pagecache slab dispenser.

After optimization shots are as follows:

2.png

Guess you like

Origin www.cnblogs.com/vana/p/10981366.html