(Reproduced) How do initiators become familiar with Linux memory management?

xiaoxiao2021-03-06  76

How do beginners become familiar with Linux Memory Management

Summary

Memory is one of the most important resources managed by Linux kernels, and the memory management system is the most important part of the operating system. For beginners of Linux, it is very important to familiarize with Linux. (2004-10-25 11:47:45)

forward from:

http://www.linuxaid.com.cn/

By lanf, source: http://tech.ccidnet.com/pub/Article/c302_a166530_p1.html

Author: HE Qin Tong Source: SEOUL - open system memory is one of the world's most important resources managed by the Linux kernel, memory management system is the most important part of the operating system. For beginners of Linux, it is very important to familiarize with Linux. The process is a program that runs in virtual address space. It can be said that any programs running under the Linux system are processes. Interactive processes and batch processes are included in the Linux system. The interactive process is controlled and running by Shell, which can be run in the front desk or in the background. The batch process is not part of a terminal, which is submitted to a queue to order in order. Most processes require virtual memory. How much memory usually requires a typical Linux application system, 128MB memory is a reasonable choice. If you do not run the X-WINDOW system, you can work only on a special purpose machine (such as "crash and burning" system) that uses the "" system for debugging device drivers). The author has done experiments, and the time required to compile the kernel under 128MB and 256MB is almost 3 minutes and a half minutes (the author's Linux release is Mandrake Linux 9.1, the kernel 2.4.21). On a system with only 8MB memory, compiling the time will be longer. Similar to multimedia applications such as a web browser, it will run more smooth when they are sufficient, especially when compiling the program, while online browsing. Therefore, if only 128MB of memory, the expected performance will be reduced. Similarly, if you want to develop a large amount of memory applications, you may require more memory. Therefore, how much memory needs to be determined by work needs. Real-time monitoring memory usage 1. Use the "free" command in 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 gives a 256MB of RAM and 512MB swappable systems. The third line output (MEM :) shows the physical memory. The Total column does not display the physical memory used by the core (usually approximately 1MB). The Used column shows the total memory used (the second line is not counted). Free columns show all unused memory. Shared column shows the total amount of memory shared by multiple processes. The buffers column shows the current size of the disk cache. The fifth line (SWAP :) is used to change space, and the information displayed is similar to. If this behavior is all 0, then there is no use of the intercom space. In the default state, the free command displays memory usage at kilobytes (that is, 1024 bytes). Use the -h parameter, in bytes, in-bytes, use -m parameters, display memory usage in megabytes. You can also pass the -s parameter, use the command uninterrupted memory usage: #free -b -s5 This command will continue to report the memory usage in the terminal window, updated every 5 seconds. 2. Make the VMSTAT command to monitor virtual memory usage

# Vmstat

Procs ----------- Memory ---------- --- Swap - ----- o ---- --system - ---- CPU- --- RB SWPD Free Buff Cache Si So Bo in CS US Sy WA

1 0 0 63692 10704 101008 0 0 239 42 126 105 48 45 7 0VMSTAT () command is a universal monitor that is an abbreviation for Virtual Meomory Statistics (virtual memory statistics). If you do not use any command line parameters when using the vmstat command, you will get a one-time report. The VMSTAT command reports the main activity type with process (procs), memory (in kilobytes), swap partition (in kilobytes), input and output from block devices (hard drive), system interruption (each The number of seconds occurred in seconds), and the proportion of the central processing unit (CPU) is assigned to the user, the system, and idle. Mechanism storage management subsystem implemented by virtual memory is one of the most important components in the operating system. In the early calculation era, because people needed to be much greater than physical memory, therefore designed a variety of strategies to solve this problem, the most successful virtual memory technology, which makes limited physical memory in the system The memory space required for the competition process is satisfied. Virtual Memory is more than the actual memory capacity by sharing memory between the various processes. Linux supports virtual memory, which uses disk as an extension of RAM, so that the available memory is effectively expanded. The core deposited the current memory block to the hard disk, and makes memory to other purposes. When the original content is used, read back memory. Programs running on Linux only see a large number of available memory, not what part is on disk. Of course, reading and writing hard drives are slower than true memory (approximately less than a thousand times), so the program runs slower. This part of this part of the hard disk is used to change space. Virtual memory technology not only allows us to use more memory, it also provides these features: 1. The huge addressing space operating system allows the system to have much more memory space than actual memory. Virtual memory can be a number of times the actual physical space in the system. Each process runs in its stand-alone virtual address space, which are completely separated from each other, so the process does not affect each other. At the same time, hardware virtual memory mechanisms can set certain locations of memory to be unwritable, so that code and data are not subject to malicious programs. 2. Fair Physical Memory Allocation Memory Management subsystem allows each running process to share physical memory in the system. 3. Sharing virtual memory although virtual memory allows the process to have its independent virtual address space, sometimes you need to share memory between processes. For example, there is a possibility that there are several processes in the system to run the Bash command shell. In order to avoid the copy of the Bash program in the virtual memory space of each process, the preferred solution is that there is only one BASH copy in the physical memory, and is shared between multiple processes. Dynamic library is another way to share execution code between processes. Shared memory can be used as a means of inter-process communication (IPC), and multiple processes exchange information through shared memory. Linux supports the shared memory IPC mechanism for System V. 4. Process protection systems have their own virtual address space. These virtual address spaces are completely separated, such a process does not affect other processes, and the virtual memory mechanism on the hardware is protected, and the memory cannot be written. This prevents the lost application to overwrite the data of the code. 5.Linux virtual memory implementation mechanism Linux virtual memory implementation requires 6 mechanisms: address mapping mechanism, memory allocation recycling mechanism, cache, and refresh mechanism, request page mechanism, switching mechanism, and memory sharing mechanism. The memory management program maps the logical address of the user program to the physical address by the mapping mechanism. When the user program is running, if the virtual address in the program does not have the corresponding physical memory, the request page requirement is issued. If there is an idle memory available to allocate, request allocation of memory (which uses memory allocation and recycling), and records the physical page that is in use in the cache (using the cache mechanism). If there is not enough memory to be available, then call the switching mechanism; a part of the memory.

In addition, in the address map to find the physical page through the TLB (translation of the reheem memory); in the switching mechanism, swap cache is swapped in the switching of the physical page, and the page table is also modified to map the file address. The Linux virtual memory implementation principle is shown in Figure 1. Figure 1 Linux virtual memory implementation principle 6. Virtual memory capacity setting may say that the virtual memory capacity setting should be assigned twice the physical memory, but this is just a law. If the physical memory is relatively small, it can be set. If there is 256MB of physical memory or more, you can narrow the virtual memory. Linux will use a lot of memory to make cache, but will be retracted when the resource is tight. Just see SWAP is 0, or if the number is small, you can rest assured that the memory is not used is the biggest waste. Memory leaks and recycling methods 1. Definition of memory leaks Generally, memory leaks are leakage of the reactor memory. The stack memory is that the program is allocated from the heap, the size is arbitrary (the size of the memory block can be determined in the program running period), and the released memory must be displayed after use. The application generally uses Malloc, Realloc, New and other functions to allocate memory from the heap. After use, the program must be responsible for the corresponding call free or delete release the memory block. Otherwise, this memory cannot be used again, and we say this memory leak. 2. The hazard of memory leaks From the perspective of user use, memory leaks itself will not happen. As a general user, it does not feel the existence of memory leaks. Really harmful is the accumulation of memory leaks, which will eventually exhaust all the memory of the system. From this perspective, there is no harm of disposable memory leaks because it will not accumulate. The harmful memory leakage is very large, as it is more difficult to detect more than a haired and intervals of memory. Programs in memory leaks have reduced the performance of the program in addition to more memory, except for more memory. For the server, if this happens, even if the system does not collapse, it will also seriously affect the use. 3. Memory leak detection and recycling for troubles such as memory overflow, you may encounter when writing complex programs with pointers. Under Linux or UNIX, C and C languages ​​are the most commonly used tools. However, the C program lacks corresponding means to detect memory information, and can only use TOP instructions to observe the total dynamic memory total memory. And when the program exits, we cannot learn any memory leak information. (1) Use the Linux command to recycle memory, you can use the PS, KILL two commands to detect memory usage and recycle. Use the command "PS" when using superuser privileges, which lists all the running program names and corresponding process numbers (PID). The working principle of the kill command is to send a system operation signal and program process number (PID) to the kernel of the Linux operating system. The following is an example in which the command PS parameter V: #ps V is used for high efficiency recovery memory.

PID TTY Stat Time Majfl TRS DRS RSS% MEM COMMAND

2530 VC / 1 S 0:00 104 6 1325 408 0.1 / sbin / mingetty TTY1

2531 VC / 2 S 0:00 104 6 1325 408 0.1 / sbin / mingetty TTY2

2532 VC / 3 S 0:00 104 6 1325 408 0.1 / sbin / mingetty TTY3

2533 VC / 4 S 0:00 104 6 1325 408 0.1 / sbin / mingetty TTY4

2534 VC / 5 S 0:00 104 6 1325 408 0.1 / sbin / mingetty TTY52535 VC / 6 S 0:00 104 6 1325 408 0.1 / sbin / mingetty TTY6

2639 PTS / 1 S 0:00 545 16 2643 968 0.3 [SU]

2684 PTS / 1 S 0:00 361 586 2501 1592 0.6 Bash

2711 PTS / 0 S 0:00 545 16 2643 968 0.3 [SU]

2714 PTS / 0 S 0:00 361 586 2501 1592 0.6 Bash

2754 PTS / 2 S 0:00 545 16 2643 968 0.3 [SU]

2757 PTS / 2 S 0:00 361 586 2501 1592 0.6 Bash

2818 PTS / 1 S 0:00 120 29 1478 480 0.1 ping 192.168.1.7

2939 PTS / 2 R 0:00 156 58 2469 644 0.2 PS -V If you want to recycle the memory of the ping command, you can use the following command: # Kill -9 2818 (2) Using tool software Memprof is a very attractive and easy Used software, it is created by Red Hat Owen Talyor. This tool is used for Boehm-Demers-WEISER garbage collectors at GNOME front end. This tool can be executed directly, and it works without any modification of the source code. When execution is executed, this tool will display memory usage in a graphical way, and the work interface is shown in Figure 2. Figure 2 Memory Recycling Tool Memprof However, the tool is currently only running in the Linux system of the X86 and PPC architecture, requiring a complete GNOME environment. This makes it unique to all places. In addition, the development of the tool is also slow, and now is a version 0.5.1. The concept of Linux memory is described above, how much memory, real-time monitoring memory usage, virtual memory implementation mechanism, and memory leaks and recycling methods, I hope to help Linux beginners high efficiency use memory system help .

转载请注明原文地址:https://www.9cbs.com/read-93767.html

New Post(0)