how to calculate memory available if needed on a linux system?
I have set swappiness to 0:
# sysctl vm.swappiness
vm.swappiness = 0
According to various sources, this should mean that applications have priority over file chaches, and swap should only be used when the applications themselves need more memory than is physically available.
So I naively took the value free provides in the '-/+ buffers/cache' line as 'free' as the amount of memory to be available on the server.
Unfortunately this is not even close to true: On a server with 20GB RAM, memory utilization by this measure never reached 50%, yet the system swaps.
I then figured out that I could use 'sync; echo 3 > /proc/sys/vm/drop_caches' to drop cached stuff.
I was very surprised to still have 12.5 GB cached data after doing that. I am figuring that it's those 12.5 GB which force the system to start swapping.
I also tried to use /proc/meminfo to figure out how that cached memory is used (by comparing its content before and after dropping caches). However, I don't see the correlation between the numbers provided there and what part of the cache can be dropped.
The closest match seems to be the 'Mapped' line, which was 10GB. I am pretty sure that being mmapped keeps the kernel from dropping cache. However, the value is 2.5 GB less than the cache which can't be dropped. So it is not the whole answer.
What I am looking for is some way to determine how much memory the kernel could provide by dropping stuff if he needs to because of memory pressure. Is there maybe a way to simulate drop_caches without actually doing so?
The amount of potentially available memory does not have to be scientifically correct, but the number should at least be always in the right ballpark, which right now, it ain't...
The point here is that it's a productive system. Sy doing stuff like dropping caches or filling memory until the system starts to swap is not a permanent solution to figure out the value.
By the way, it turned out that postgres was the culprit in this concrete case, stopping it made dropping all caches possible, but that does not answer the general question of how to estimate available memory...
1. Is my assumption correct that I can subtract 'Mapped' from the freeable cache memory completely?
2. Where could the other 2.5 GB be used?
3. Is there a way to get a better guess of how much memory the system can free if necessary, before swap has to be used?
System: Debian Lenny
I don't know why some of the things you tried didn't have the expected effects. But I do know those things are unwise and the effects you think you want would actually make performance worse.
To grossly oversimplify, anonymous memory is the heap and stack of your applications (writable data) while non anonymous memory is the code plus read only data.
So setting swappiness to zero gives a higher effective memory priority to the writable data of idle and low priority processes than to the code and read only data of active processes. (Process X is stalled waiting for some external request, while process Y is running. Swappiness of 0 means the system should prefer to keep the writable parts of X in memory rather than the read only parts of Y)
When kicked out of memory, anonymous pages must be written to swap and when accessed again must be read back. But read only non anonymous pages can be kicked out of memory just by discarding them and when accessed again read back from their original source (whatever .so file or executable they were originally read from). So kicking out non anonymous data is less total work and preferred. But for best performance that preference should be moderate, not absolute.
It is much harder to estimate the system's effective need for physical ram used for non anonymous pages. How much file cache memory must be used for decent performance vs. how much more is being used simply because it is there and slightly better to use it for file cache vs. not use it for file cache.
|All times are GMT -5. The time now is 04:20 AM.|