Article Title: How do beginners get familiar with Linux memory management. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Memory is one of the most important resources managed by the Linux kernel, and the memory management system is the most important part of the operating system. For Linux beginners, it is very important to be familiar with Linux memory management.
A process is a program running in a virtual address space. It can be said that any program running in Linux is a process. Linux systems include interactive processes and batch processing processes. Interactive processes are controlled and run by Shell, which can be run either on the foreground or in the background. The batch processing process does not belong to a terminal and is submitted to a queue for sequential execution. Most processes require virtual memory.
Memory size required
For a typical Linux application system, MB memory is a reasonable choice. If you do not run the X-Window system, you can only use 8 MB of memory on a special-purpose machine (for example, the "crash and burn" system used to debug the device driver.
I have done some experiments. It takes almost the same time to compile the kernel at 9.1 MB and MB, with less than 3 minutes and a half (the author's Linux release version is Mandrake Linux and kernel 2.4.21 ). In a system with 8 MB memory, compilation takes a longer time. Multimedia Applications such as Web browsers run smoothly when the memory is sufficient, especially when the program is compiled and browsed online. Therefore, if you only have MB of memory, the expected performance will be reduced. Similarly, if you want to develop applications that consume a large amount of memory, more memory may be required. Therefore, the amount of memory required is determined by the work requirements.
Monitor memory usage in real time
1. Use the "Free" command on the command line to monitor memory usage # free
Total used free shared buffers cached
Mem: 256024 192284 63740 0 10676 101004
-/+ Buffers/cache: 80604 75420
Swap: 522072 0 522072
The above Code provides a mb ram and a MB swap space system. The third row of output (Mem :) displays the physical memory. The Total column does not display the core physical memory (usually about 1 MB ). The Used column shows the total memory Used (the second row does not count as a buffer ). The Free column shows all unused memory. The Shared column displays the total memory Shared by multiple processes. The Buffers column displays the current disk cache size. The fifth line (Swap :) is used to Swap the space. The displayed information is similar to the preceding one. If the behavior is 0, no swap space is used. By default, the free command displays memory usage in kilobytes (that is, 1024 bytes. Use the-h parameter to Display memory usage in bytes, or use the-m parameter to Display memory usage in MB. You can also use the-s parameter to continuously monitor memory usage:
# Free-B-s5
This command continuously reports memory usage in the terminal window and updates every 5 seconds.
2. Run the vmstat command to monitor virtual memory usage # vmstat
Procs ----------- memory ---------- --- swap -- ----- io ---- system -- ---- cpu ----
R B swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 63692 10704 101008 0 239 42 126 48 45 7 0
The vmstat () command is a common monitoring program. It is short for Virtual Meomory Statistics (Virtual Memory Statistics. If you do not use any command line parameters when using the vmstat command, a one-time report will be generated. The vmstat Command reports the main activity types: Process (procs), memory (in kilobytes), swap partition (in kilobytes), from Block devices (hard drive) the input output, system interruption (the number of times each second occurs), and the proportion allocated by the central processing unit (CPU) to the user, the system, and the idle time.
Virtual Memory Implementation Mechanism
The storage management subsystem is one of the most important components of the operating system. In the early computing era, because people needed much more memory than physical memory, a variety of strategies were designed to solve this problem. The most successful one was the virtual memory technology, it satisfies the memory space required by the system's limited physical memory competition process. By sharing memory among processes, the virtual memory seems to have more memory than the actual memory. Linux supports Virtual Memory, that is, the disk is used as the RAM Extension, so that the available memory can be effectively expanded accordingly. The core stores unused memory blocks on the hard disk to free up memory for other purposes. When the original content is used again, it is read back to the memory. Programs running on Linux only view a large amount of available memory, regardless of which part is on the disk. Of course, the read/write hard disk is slower than the real memory (about times), so the program runs slowly. This part of the hard disk used for virtual memory is called swap space.
The virtual memory technology not only allows us to use more memory, but also provides the following features:
1. Huge addressing space
The operating system makes the system look much larger than the actual memory. The virtual memory can be many times the actual physical space in the system. Each process runs in its own virtual address space. These virtual spaces are completely isolated from each other, so they do not affect each other. At the same time, the hardware Virtual Memory organization can set some areas of the memory as non-writable, which can protect code and data from malicious programs.
2. Fair physical memory allocation
The memory management subsystem allows every running process in the system to share the physical memory fairly.
3. shared virtual memory
Although the virtual memory allows the process to have its own virtual address space, it sometimes needs to share the memory between processes. For example, there may be several processes running the BASH command shell program at the same time. To avoid the existence of BASH program copies in the virtual memory space of each process, a good solution is that only one BASH copy exists in the physical memory of the system, and shared among multiple processes. The dynamic library is another way to share and execute code between processes. Shared memory can be used as a means of inter-process communication (IPC). Multiple processes exchange information through shared memory. Linux supports the shared memory IPC Mechanism of system v.
[1] [2] Next page