分类: LINUX
2014-02-10 14:17:32
When it comes to how OS's manage their memory, it can be kind of unclear for some people. The reason "top" and "free -m" often dont seem to line up is because of the different states of memory. There are two states of "used" memory, "inactive" and "active".
Active memory is memory that is being used by a particular process. Inactive memory is memory that was allocated to a process that is no longer running. "top" and "free -m" do not differentiate between these two types of memory. This is why "free -m" may show only 2% memory not being used. while "top" may only show a few processes using 1 or 2%.
To better understand the differences between inactive and active memory, think of it this way. If you were to write something on a white board starting from the top left, and then you no-longer needed the information, you probably wouldn't erase that sentence and start in the top left again. You would probably drop to the next line and write in the next available space and only erase the information when you run out of available surface area.
The same concept holds true for memory in a computer. Instead of cleaning the "dirty" memory the computer just lets it sit there because it's not causing any detrimental effects or costing the computer anything in terms of performance or useability. The computer simply waits untill the memory is needed again before it clears it up and writes the new data to that section. Over time this can cause the levels of inactive memory to climb and therefore the level of "used" memory, and eventually, trigger threshold alarms, especially if you run memory intensive applications or scripts.
To determine whether or not you have a lot of inactive memory on a Linux system, look at /proc/meminfo
cat /proc/meminfo
On about the 6th and 7th lines, you should see two fields sowing how much memory is active and how much is inactive.
If you have a large amount of inactive memory, there are two ways that you can clear it up to make the threshold alarm go away. Either reboot the system, or simply run the following command.
free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free
This commands shows you the current state of the memory according to "free" and then clears the memory buffers/cache and then shows the new details as reported by "free". Keep in mind though that if you still have high levels of usage after running this command, it is probably actually being used by a process and as such can not be cleared without killing the process or restarting the box.
也可以用vmstat查看