Mastering Linux System Performance: A Comprehensive Guide to Memory Management
Mastering Linux System Performance: A Comprehensive Guide to Memory Management |
Mastering Linux System Performance: A Comprehensive Guide to Memory Management - This comprehensive guide aims to provide users with a detailed understanding of managing RAM memory and cache on Linux systems.
From displaying memory information to creating and monitoring swap space, this article covers essential tools and techniques to optimize system performance.
Read on to learn the intricacies of memory management using practical examples and industry-standard tools.
Displaying Memory and Cache Information
Understanding the current state of memory and cache is crucial for effective system management. The /proc/meminfo file provides a wealth of information about the memory on your Linux computer. By using commands like cat /proc/meminfo or free -om, users can easily access critical details such as total physical RAM, unused RAM, buffers, cached RAM, and swap cached. These metrics lay the foundation for making informed decisions regarding system performance.
/proc/meminfo
Displaying /proc/meminfo will tell you a lot about the memory on your Linux computer.
paul@ubu1010:~$ cat /proc/meminfo MemTotal: 3830176 kB MemFree: 244060 kB Buffers: 41020 kB Cached: 2035292 kB SwapCached: 9892 kB ...
The first line contains the total amount of physical RAM, the second line is the unused RAM. Buffers is RAM used for buffering files, cached is the amount of RAM used as cache and SwapCached is the amount of swap used as cache. The file gives us much more information outside of the scope of this course.
Utilizing Free Tool for Readable Memory Information
The free tool enhances the readability of memory information extracted from /proc/meminfo. This command provides a concise summary in megabytes, making it easier for administrators to interpret and act upon. With examples like free -om, users can quickly grasp the current memory usage, both in terms of total, used, and free memory, along with shared, buffered, and cached information.
paul@ubu1010:~$ free -om total used free shared buffers cached Mem: 3740 3519 221 0 42 1994 Swap: 6234 82 6152
Insights from the Top Tool
While commonly used to analyze CPU-intensive processes, the top tool also serves as a valuable resource for displaying memory information. By toggling to the fourth and fifth lines (achieved by pressing 'm'), users gain insights into memory usage alongside CPU statistics. A practical screenshot of top illustrates its functionality, showcasing critical details such as system uptime, load averages, and memory allocation.
Below a screenshot of top on the same ubu1010 from above.
top - 10:44:34 up 16 days, 9:56, 6 users, load average: 0.13, 0.09, 0.12 Tasks: 166 total, 1 running, 165 sleeping, 0 stopped, 0 zombie Cpu(s): 5.1%us, 4.6%sy, 0.6%ni, 88.7%id, 0.8%wa, 0.0%hi, 0.3%si, 0.0%st Mem: 3830176k total, 3613720k used, 216456k free, 45452k buffers Swap: 6384636k total, 84988k used, 6299648k free, 2050948k cached
Managing Swap Space: Understanding the Basics
When the system requires more memory than physically available in RAM, swap space becomes a crucial resource. This section explores concepts like swapping, paging, and virtual memory, emphasizing the role of swap space in preventing system crashes. It highlights that swap space, although commonly stored on slower hard disks, plays a vital role in maintaining system stability.
about swap space
When the operating system needs more memory than physically present in RAM, it can use swap space. Swap space is located on slower but cheaper memory. Notice that, although hard disks are commonly used for swap space, their access times are one hundred thousand times slower.
The swap space can be a file, a partition, or a combination of files and partitions. You can see the swap space with the free command, or with cat /proc/swaps.
paul@ubu1010:~$ free -o | grep -v Mem total used free shared buffers cached Swap: 6384636 84988 6299648 paul@ubu1010:~$ cat /proc/swaps Filename Type Size Used Priority /dev/sda3 partition 6384636 84988 -1
The amount of swap space that you need depends heavily on the services that the computer provides.
Creating and Activating Swap Partition
Detailed steps are provided for creating and activating a swap partition, utilizing commands like fdisk, mkswap, and swapon. The article emphasizes the importance of recognizing swap spaces separately, as displayed in /proc/swaps, compared to the summarized output from free -om. This ensures a comprehensive understanding of the allocated swap resources.
The screenshot below shows the creation and activation of a swap partition.
root@RHELv4u4:~# fdisk -l 2> /dev/null | grep hda Disk /dev/hda: 536 MB, 536870912 bytes /dev/hda1 1 1040 524128+ 83 Linux root@RHELv4u4:~# mkswap /dev/hda1 Setting up swapspace version 1, size = 536702 kB root@RHELv4u4:~# swapon /dev/hda1
Now you can see that /proc/swaps displays all swap spaces separately, whereas the free -om command only makes a human readable summary.
root@RHELv4u4:~# cat /proc/swaps Filename Type Size Used Priority /dev/mapper/VolGroup00-LogVol01 partition 1048568 0 -1 /dev/hda1 partition 524120 0 -2 root@RHELv4u4:~# free -om total used free shared buffers cached Mem: 249 245 4 0 125 54 Swap: 1535 0 1535
Creating a Swap File: An Alternate Approach
The article presents an alternative method for creating a swap file, illustrating the process using commands like dd and mkswap. This flexibility allows users to choose between creating swap partitions or swap files based on specific system requirements.
Here is one more example showing you how to create a swap file. On Solaris you can use mkfile instead of dd.
root@RHELv4u4:~# dd if=/dev/zero of=/smallswapfile bs=1024 count=4096 4096+0 records in 4096+0 records out root@RHELv4u4:~# mkswap /smallswapfile Setting up swapspace version 1, size = 4190 kB root@RHELv4u4:~# swapon /smallswapfile root@RHELv4u4:~# cat /proc/swaps Filename Type Size Used Priority /dev/mapper/VolGroup00-LogVol01 partition 1048568 0 -1 /dev/hda1 partition 524120 0 -2 /smallswapfile file 4088 0 -3
Swap Space in /etc/fstab: Ensuring Persistence
For permanent swap configurations, the article guides users to add entries to the /etc/fstab file. Sample lines demonstrate how to define swap partitions and swap files, ensuring they are activated during system boot. This step enhances system stability by persistently mounting swap spaces across reboots.
If you like these swaps to be permanent, then don't forget to add them to /etc/fstab. The lines in /etc/fstab will be similar to the following.
/dev/hda1 swap swap defaults 0 0 /smallswapfile swap swap defaults 0 0
Monitoring Memory with vmstat
The guide concludes with insights into monitoring swap usage using the vmstat tool. Practical examples demonstrate how to utilize vmstat -S m to display information in megabytes, providing a comprehensive view of system processes, memory usage, and swapping activities.
Below a simple vmstat displaying information in megabytes.
paul@ubu1010:~$ vmstat -S m procs ---------memory-------- ---swap-- -----io---- -system- ----cpu---- r b swpd free buff cache si so bi bo in cs us sy id wa 0 0 87 225 46 2097 0 0 2 5 14 8 6 5 89 1
Below a sample vmstat when (in another terminal) root launches a find /. It generates a lot of disk i/o (bi and bo are disk blocks in and out). There is no need for swapping here.
paul@ubu1010:~$ vmstat 2 100 procs ----------memory---------- ---swap-- -----io---- -system-- ----cpu---- r b swpd free buff cache si so bi bo in cs us sy id wa 0 0 84984 1999436 53416 269536 0 0 2 5 2 10 6 5 89 1 0 0 84984 1999428 53416 269564 0 0 0 0 1713 2748 4 4 92 0 0 0 84984 1999552 53416 269564 0 0 0 0 1672 1838 4 6 90 0 0 0 84984 1999552 53424 269560 0 0 0 14 1587 2526 5 7 87 2 0 0 84984 1999180 53424 269580 0 0 0 100 1748 2193 4 6 91 0 1 0 84984 1997800 54508 269760 0 0 610 0 1836 3890 17 10 68 4 1 0 84984 1994620 55040 269748 0 0 250 168 1724 4365 19 17 56 9 0 1 84984 1978508 55292 269704 0 0 126 0 1957 2897 19 18 58 4 0 0 84984 1974608 58964 269784 0 0 1826 478 2605 4355 7 7 44 41 0 2 84984 1971260 62268 269728 0 0 1634 756 2257 3865 7 7 47 39
Below a sample vmstat when executing (on RHEL6) a simple memory leaking program. Now you see a lot of memory being swapped (si is 'swapped in').
[paul@rhel6c ~]$ vmstat 2 100 procs ----------memory-------- ---swap-- ----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 3 245208 5280 232 1916 261 0 0 42 27 21 0 1 98 1 0 0 2 263372 4800 72 908 143840 128 0 1138 462 191 2 10 0 88 0 1 3 350672 4792 56 992 169280 256 0 1092 360 142 1 13 0 86 0 1 4 449584 4788 56 1024 95880 64 0 606 471 191 2 13 0 85 0 0 4 471968 4828 56 1140 44832 80 0 390 235 90 2 12 0 87 0 3 5 505960 4764 56 1136 68008 16 0 538 286 109 1 12 0 87 0
The code below was used to simulate a memory leak (and force swapping). This code was found on wikipedia without author.
paul@mac:~$ cat memleak.c #include <stdlib.h> int main(void) { while (malloc(50)); return 0; }
Practice: memory
The article encourages hands-on learning through practical exercises, covering tasks such as using dmesg to find total memory, creating swap partitions and files, and verifying swap usage with commands like free and vmstat. Solutions are provided for each exercise, ensuring a thorough understanding of the concepts discussed.
- Use dmesg to find the total amount of memory in your computer.
- Use free to display memory usage in kilobytes (then in megabytes).
- On a virtual machine, create a swap partition (you might need an extra virtual disk for this).
- Add a 20 megabyte swap file to the system.
- Put all swap spaces in /etc/fstab and activate them. Test with a reboot that they are mounted.
- Use free to verify usage of current swap.
- (optional) Display the usage of swap with vmstat and free -s during a memory leak.
Solution: memory
1. Use dmesg to find the total amount of memory in your computer.
dmesg | grep Memory
2. Use free to display memory usage in kilobytes (then in megabytes).
free ; free -m
3. On a virtual machine, create a swap partition (you might need an extra virtual disk for this).
mkswap /dev/sdd1 ; swapon /dev/sdd1
4. Add a 20 megabyte swap file to the system.
dd if=/dev/zero of=/swapfile20mb bs=1024 count=20000 mkswap /swapfile20mb swapon /swapfile20mb
5. Put all swap spaces in /etc/fstab and activate them. Test with a reboot that they are mounted.
root@computer# tail -2 /etc/fstab /dev/sdd1 swap swap defaults 0 0 /swapfile20mb swap swap defaults 0 0
6. Use free to verify usage of current swap.
free -om
7. (optional) Display the usage of swap with vmstat and free -s during a memory leak.
Conclusion
By following this guide, Linux system administrators and enthusiasts can gain proficiency in memory management, optimizing their systems for enhanced performance and stability.
Post a Comment for "Mastering Linux System Performance: A Comprehensive Guide to Memory Management"
Post a Comment