Limit RAM Usages

Is there any kernel tune parameters available to limit RAM usages at certain level .

EG .
RAM: 4 GB
Swap: 2 GB

I Need if my RAM usages reached 3 GB Kernel will start swaping new pages . ..

--Shirish Shukla

Why do you need this?

I suppose you could use the badram module, and tell it that vast swaths of memory are bad...

I can't see the use of this except for testing purposes, to see what happens when you start swapping...

Linux: Tuning Swappiness | KernelTrap might shed some light on it.

Yes Corona...
We have a test lab RHEL6 with 4GB RAM and have about 3GB of Swap .. that will be soon go for development phase .. before we want to test the functionality by creating high load and swap usages and test the same with monitoring tools and tuned parameters .

Thanks for your valuable inputs ..

--Shirish Shukla

Shirish,

For test purposes, the easiest way to effectively reduce the amount of physical RAM available for use by processes is to crank up the kernel parameter vm.min_free_kbytes. This sets the low water mark for the amount of memory held as free for new processes and is normally set to something like 50MB. Turning this up to 3.5GB in your example will mean the system will run out of physical memory and start actively paging very quickly. See the man pages for sysctl and sysctl.conf.

This parameter is also very useful for effectively eliminating cache and buffer space when testing I/O throughput.

Of course, the whole point of designing a system configuration is to ensure that the system would only ever actively page as an absolute last resort, one small step better than crashing. Systems should NEVER actively page in normal operation. Disk I/O (milliseconds) is literally a million times slower than RAM operations (nanoseconds), so system performance will immediately become completely unacceptable as soon as the system starts to actively page.

Active paging (shown by "sar -B" or "vmstat") is completely different from swap space usage (shown by "sar -S" or "free"). It's OK for a system to statically park pages of memory for long inactive processes on the swap partition. But, if the system is actively paging, it's reading and writing thousand of blocks to and from swap space per second.

If you even suspect that this system will start actively paging under heavy load, BUY MORE RAM.

Remember to reset vm.min_free_kbytes to the original value when your tests are complete...

1 Like

Thanks for clearing this . can you pls let me confirm ..

 
RAM: 1GB
Swap: 2GB
Kernel: 2.6.3
 
AND 
# cat /proc/sys/vm/min_free_kbytes
3794  

So what basis this calculated and am missing logic behind same ..some docx state that it's number of pages of physical RAM available .

What logic used to calculate this value .. ??

Shirish,

The amount of RAM set aside by min_free_kbytes cannot be used for processes, cache or buffers. It's "reserved" for use by new processes that are starting up. By reserving it, you're effectively setting it aside so the system can't use it. So, if you had a system with 1GB of RAM and you set min_free_kbytes to 750MB, the system would behave as though it had only 250MB to work with.

The simplest way is to boot the system, let it run with the application started but with no real load. Use "free" to see how much memory the system is currently using, not including cache and buffers.

For example, here is a system with 2GB of RAM. It happens to be a Fedora 16 VM, but the virtual memory model is the same:

# cat /proc/sys/vm/min_free_kbytes
45056
# free -k
             total       used       free     shared    buffers     cached
Mem:       2053048    1524288     528760          0     112112     895452
-/+ buffers/cache:     516724    1536324
Swap:      4128764          0    4128764
 
# sar -B 10 1
Linux 3.1.0-7.fc16.x86_64 (fed16) 04/26/2012 _x86_64_ (2 CPU)
10:27:14 AM pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff
10:27:14 AM  pgpgin/s pgpgout/s   fault/s  majflt/s  pgfree/s pgscank/s pgscand/s pgsteal/s    %vmeff
10:27:24 AM      0.00      0.00      6.00      0.00     19.40      0.00      0.00      0.00      0.00
Average:         0.00      0.00      6.00      0.00     19.40      0.00      0.00      0.00      0.00
 

So, it's currently using about 516MB for processes and has about 1.5GB unused. There is no active paging activity. The min_free_bytes parameter is set to about 45MB. If we increase min_free_kbytes to 1500000 (1.5GB), then the system is forced to reserve that 1.5GB for new processes and acts as though it only has 500MB of RAM. If we start to stress the system, it can't use the 1.5GB you've told it to reserve, so it has no choice but to start actively paging to the swap area to find the memory to continue to run.

Using this method, you can put any system into a condition where it thinks it's almost out of memory and needs to start paging.

If you reserve so much memory that the system doesn't have enough to run, it will start paging immediately without any load. Here's what happens if we reserve 1.7GB:

# echo 1700000 >/proc/sys/vm/min_free_kbytes
# cat /proc/sys/vm/min_free_kbytes
1700000
# free -k
             total       used       free     shared    buffers     cached
Mem:       2053048     349968    1703080          0       1332      87004
-/+ buffers/cache:     261632    1791416
Swap:      4128764     265864    3862900

I pushed this system to use 256MB of the swap space. It immediately started actively paging and performance is horrible. Very slow response to commands. You can see the active paging activity:

# sar -B 10 1
Linux 3.1.0-7.fc16.x86_64 (fed16)       04/26/2012      _x86_64_        (2 CPU)
10:29:14 AM  pgpgin/s pgpgout/s   fault/s  majflt/s  pgfree/s pgscank/s pgscand/s pgsteal/s    %vmeff
10:29:24 AM   4176.62    815.18    772.33    220.88   2023.98   3435.26      0.00   1992.91     58.01
Average:      4176.62    815.18    772.33    220.88   2023.98   3435.26      0.00   1992.91     58.01

If we return min_free_bytes to it's inital value, the system quickly returns to normal:

# echo 45000 >/proc/sys/vm/min_free_kbytes
# sar -B 10 1
Linux 3.1.0-7.fc16.x86_64 (fed16)       04/26/2012      _x86_64_        (2 CPU)
10:33:27 AM  pgpgin/s pgpgout/s   fault/s  majflt/s  pgfree/s pgscank/s pgscand/s pgsteal/s    %vmeff
10:33:37 AM      0.00      0.00      2.50      0.00      5.60      0.00      0.00      0.00      0.00
Average:         0.00      0.00      2.50      0.00      5.60      0.00      0.00      0.00      0.00
# free
             total       used       free     shared    buffers     cached
Mem:       2053048     358248    1694800          0       5044     149616
-/+ buffers/cache:     203588    1849460
Swap:      4128764     375632    3753132
 

Notice that the last free command shows 375MB of the swap area still used, which is OK, since it's static usage. The system will go get those pages out of swap if and when it needs them. The important point is that there isn't any active paging going on anymore.

See how this works?

Thanks a LOT !!! It;s clear my need ...

--Shirish Shukla