Search This Blog

2012-01-27

OS: Setting Virtual Memory the Right Way

OS: Windows, HP-UX, AIX, Solaris, Linux, RedHat, Ubuntu, Fedora, CentOS

PC used to rely on virtual memory (called swap in UNIX) many years ago, when RAM was expansive, or vendor worry that the application/daemon runs out of RAM and would like it to continue running against the virtual memory/swap (this is the main reason).

Using hardware RAID to group 10 - 30 hard disk to increase IOPS, and bandwidth was a common practice for swap area (virtual group, or LUN) for server, but this technology was not available or too expansive for desktop PC. Eventually users are suffering when Windows (any OS) paging to virtual memory.

Many users, especially Windows, blindly follow Microsoft's recommendation to configure 2 x RAM, or 1.5 x RAM.

My rule of thumb is
1. Virtual memory or swap is only used to handle exceptional case where application (including web server, database server, ERP) used up all memory, but would like to keep it running, so that downtime can be scheduled
2. If debugging is required to determine BSOD (Windows) or crash, configure virtual memory as 1 x RAM + 1 MB. After debugging, disable the virtual memory
3. If during normal usage, you used 8 GB RAM (commit change for Windows), then install extra 2 GB, i.e. 10 GB. Don't rely on virtual memory or swap to run any application
4. Check memory usage during peak, or after long period of use, so that appropriate RAM will be installed
5. Never examine RAM usage after reboot, as there is no load, or insufficient time to identify memory leaking application

Following is my recommendation to configure virtual memory for any operating system
1. Configure virtual memory to 1 GB or less
2. Used your favorite applications as long as possible
3. For those who does not shutdown the computer, leave it on
4. Prior of shutdown the PC, or at the peak usage, monitor Task Manager, or vmstat for Unix
5. For Windows, check Commit Change to confirm the value is less than RAM. This is the total RAM + virtual memory usage. If it is more or near to RAM size, then it is using virtual memory
6. For Windows again, run perfmon.exe and choose Performance object = Paging File, counter = % Usage Peak, instance = _Total. If the usage is mroe than 0, then virtual memory is in used
7. For Unix (AIX, Solaris, RedHat, Ubuntu, etc), run swap, or free command. Check the swap size. If it is more than 0 byte, then swap is in use
8. Increase the RAM to the max size (RAM + virtual memory) + 2 GB, by installing more RAM
9. If need to take more sample, reboot Windows or Unix to free up the virtual memory or swap
10. Monitor again after more RAM installed, to ensure not using virtual memory
11. Reduce virtual memory to 32 MB or 0 (for home PC) if does not need to determine virtual memory usage. Reboot is require, especially for Unix swap
12. For Linux programmer, don't use fork() or exec() command to call another sub-process or process. Use posix_spawn(3C) function call. Both fork() and exec() will create child process which will need to allocate the same memory size as the parent. If parent process used 6 GB, then child will need 6 GB even it does not use so much
13. For Linux admin, configure % pagecache in RAM to as small as possible, when RAM is big. Estimate to allocate the percentage to 128 MB (regular usage) - 4 GB (video). For example, 32 GB RAM, the value should be "0 0 1" where, minimum = 0%, borrow = 1%, max = 1%
14. For Linux with Oracle (or any application support hugetlb_pool), configure hugetlb_pool as the max RAM that application can allocate in future. E.g. if has 128 GB RAM, and currently has 1 database instance with 2 GB RAM, don't just allocate 2 GB for hugetlb_pool. Adjust it to be n_database_instance * memory_per_each_db
15. For Linux admin, configure /proc/sys/vm/swappiness to as small as possible, especially for high performance application/database. My recommendation is 0, while Oracle 11g is 10 with swap size 0.75 x RAM (for HP rp8640 512 GB RAM, that is 384 GB swap which is ridiculously stupid)

A side note for people with lots of unused RAM. www.SuperSpeed.com has 2 products which provide disk cache for USD$79.95

1. SuperCache - Similar to disk cache software in DOS, i.e. smartdrv.exe, but it provide mirror as well
2. RamDisk - Configure RAM as a hard disk. Similar to ramdisk.sys in DOS, or rdisk.sys in FreeDOS

These software will give you RAM speed (500x hard disk performance) of 4 GB/sec throughput, or higher, depending of your RAM speed (DDR3-13333, DDR3-16000, DDR2-667), and chipset (Intel P67, Z68, X79).

If you have 256 GB RAM, I will highly recommend to create a 128 GB disk partition, and mirror with SuperCache for server application (database, web). This will be thousands time better than system cache provided by Windows

1 comment:

OpenTeQ said...

Your blog has wonderful information regarding Guidewire Services, I also have some valuable information regarding the Guidewire Testing in USA, Canada hopefully, this will be very helpful for you.