One article explains Linux memory management

1. Overview of Linux memory management

Linux memory management refers to the management of a series of operations such as allocation, release, mapping, management, exchange, and compression of system memory. In Linux, memory is divided into multiple areas, each area has a different role, including kernel space, user space, cache, swap partition, etc. The goal of Linux memory management is to maximize the use of available memory while ensuring system stability and reliability.

1.1 What is memory management

Memory management is a mechanism responsible for managing system memory resources in a computer system, mainly including memory allocation, memory release, memory mapping, and virtual memory management. It is a very important part of the computer system, which can effectively improve the resource utilization of the system and the performance of the application program.

As an open source operating system, Linux's memory management mechanism is highly flexible and customizable, and can meet the needs of different application scenarios. Therefore, understanding the Linux memory management mechanism is very important for system administrators and developers.

The operating system completes the allocation and management of memory through the memory management mechanism, including virtual memory address mapping, memory allocation and recovery, and process memory management. Among them, the virtual memory refers to the virtual address space allocated by the operating system to the process, so that each process can independently occupy a virtual address space of a certain size without worrying about the limitation of physical memory. Memory allocation and recovery means that the operating system allocates and releases the memory required by the process when it is running, so as to ensure the resource utilization and operating efficiency of the system.

For example, when a process needs to allocate memory, it will apply to the operating system for a certain amount of memory space. If there is enough free memory in the system, the operating system will allocate corresponding memory space for the process, and map the memory space into the virtual address space of the process.

And if there is not enough free memory in the system, the operating system will perform memory compression or store part of the data of the process on the hard disk to free up enough memory space for other processes to use. In this way, the memory management mechanism can ensure the running needs of the process and maximize the use of system resources.

1.2 The importance of memory management

Memory management plays a very important role in computer systems. First, memory management determines how much memory the operating system and applications can use. If there is not enough memory, the system and applications can become very slow or crash. Second, memory management ensures that the operating system and applications do not interfere with each other. Without memory management, different applications may use the same memory area, resulting in data confusion and errors. In addition, memory management can also optimize system performance. By allocating and releasing memory reasonably, memory fragmentation can be reduced, memory usage efficiency can be improved, and overall system performance can be improved.

For example, if an operating system does not have memory management, when an application needs memory, it may directly use a certain area of ​​physical memory, which may cause other applications to be unable to use this memory area, resulting in a system crash. In addition, if multiple applications require a large amount of memory, but the memory is not properly allocated and released, it may cause memory fragmentation, thereby reducing system performance. Therefore, memory management is a very important part of the operating system.

The importance of Linux memory management is to ensure the normal operation of the system and the efficient use of system resources. Without memory management, the following problems can occur:

  • System crashes or freezes: Memory management can help the system avoid system crashes or freezes due to problems such as insufficient memory or memory leaks.

  • System performance degradation: Memory management can optimize the use of memory and improve system performance. If there is no memory management, the problem of memory fragmentation may occur, causing the system to be unable to use continuous memory space, thereby reducing system performance.

  • Security issues: memory management can improve the security of the system and prevent some malicious programs from destroying the security of the system by modifying the memory.

  • Waste of resources: If there is no memory management, there may be a waste of memory resources. For example, some programs allocate a large amount of memory but fail to release it in time, resulting in a waste of memory resources.

Therefore, memory management is one of the core functions of the operating system, which plays an important role in ensuring the normal operation of the system and efficient use of system resources.

1.3 Components of memory management

The components of memory management include the following aspects:

  1. Virtual memory management: The physical memory and the address space of the process are mapped and managed, so that each process can have an independent address space, thereby achieving isolation and protection between processes.

  2. Physical memory management: manage physical memory, including memory allocation, recycling, and mapping.

  3. Page replacement algorithm: When the physical memory is insufficient, some pages need to be replaced to release the physical memory. The page replacement algorithm is an algorithm that selects which pages to replace.

  4. Process address space management: manage the address space of the process, including code segment, data segment, stack, etc.

  5. Memory protection and access control: By setting page attributes and access rights, etc., the protection and access control of the process address space are realized.

  6. Memory statistics and monitoring: monitor the memory usage in the system, and perform memory statistics and analysis for memory performance tuning and troubleshooting.

These components are interrelated to form a complete memory management system. In an actual operating system, memory management is usually one of the most complex and core parts of the operating system.

2. Physical memory management

Physical memory management is an important part of Linux memory management, which is used to track and manage the usage of physical memory in the system, including memory allocation and release. The core task of physical memory management is to divide physical memory into a series of pages so that memory can be managed more efficiently.

2.1 What is physical memory

Physical memory refers to the actual memory chip used to store programs and data in computer hardware, also known as main memory (Main Memory). Physical memory consists of many storage units, each with a unique address, for storing data. The capacity of physical memory is one of the important indicators of computer system hardware, which directly determines the amount of data that the computer can handle and the speed of operation.

In Linux, physical memory is usually managed by the operating system's memory management module. Physical memory is allocated to and used by the kernel when the computer is started. The operating system divides the physical memory into some fixed-size pages (Page), and each page is usually 4KB or 8KB in size. Each page has a unique physical address and can be used to store process or kernel data.

The main task of physical memory management is to allocate physical memory space for each process. When a process needs memory, the operating system allocates one or more pages from the pool of free pages and maps them into the process's virtual address space. Physical memory management also needs to implement page swap (Page Swap) and page reclaim (Page Reclaim) functions, so that when the physical memory is insufficient, some pages are transferred to the disk to release the physical memory space for other processes to use.

2.2 Physical memory management method

Physical memory management is one of the core functions of the operating system, which is mainly responsible for managing physical memory resources in computer hardware. In the Linux system, there are two main ways of physical memory management: continuous memory management and non-contiguous memory management.

2.2.1 Contiguous memory management

Continuous memory management is a relatively simple physical memory management method. In the continuous memory management mode, the operating system regards the physical memory space as a continuous address space, and can directly access any physical memory address through pointers.

In the Linux system, continuous memory management is implemented using the Buddy System algorithm. The buddy system is a physical memory management algorithm that is mainly used to manage the memory allocation and release of the operating system. It blocks the physical memory available in the system by size and combines adjacent blocks into a pair of partners.

When a block of memory needs to be allocated, the buddy system will try to find a memory block of the right size, and if the found block is slightly larger than the required block, it will be split into two equal-sized buddy blocks, and the One block acts as a block of memory allocated to the requester, and the other block continues to be reserved for allocation by the system. When a block of memory is freed, the buddy system tries to merge it with adjacent blocks into a larger block for subsequent memory allocations. This can reduce the problem of memory fragmentation and improve memory utilization.

2.2.2 Non-contiguous memory management

Non-contiguous memory management means that memory space does not have to be allocated in continuous address order in physical memory, which is more flexible than continuous memory management. Common non-contiguous memory management methods include paging and segmentation.

In paging memory management, the physical memory is divided into fixed-size pages, and the virtual address space is also divided into pages of the same size, so that the mapping of virtual addresses to physical addresses can be realized, so that processes do not have to consider physical addresses when accessing memory. The actual address of memory. In this way, memory allocation and release are performed in units of pages.

In segmented memory management, the virtual address space is divided into multiple segments of different sizes, and each segment has a segment base address and a length. Segment sizes can vary dynamically, allowing for more flexible memory management. In this way, the allocation and release of memory is carried out in units of segments.

It should be noted that the implementation of the discontinuous memory management method is relatively complicated, requires more hardware and software support, and will bring certain performance overhead. Therefore, it is necessary to weigh the relationship between its flexibility and performance overhead in practical applications.

2.3 Functions and examples related to physical memory management

The main functions used by physical memory management in Linux are as follows:

  • memblock_init(): This function is used to initialize the physical memory block, that is, divide the physical memory into available memory blocks.

  • memblock_reserve(): This function is used to reserve a block of physical memory so that it cannot be allocated by the memory allocator.

  • memblock_free(): This function is used to release the physical memory block.

  • memblock_alloc(): This function is used to allocate a physical memory block.

  • memblock_find_in_range(): This function is used to find free physical memory blocks within the specified range.

Here is a simple example code that allocates a block of physical memory and prints its address:

#include 
#include 
#include 
#include 
#include 

static int __init test_init(void)
{
    unsigned long size = 4096;
    unsigned long *ptr;

    ptr = memblock_alloc(size, PAGE_SIZE);
    if (!ptr) {
        pr_err("Failed to allocate memory\n");
        return -ENOMEM;
    }

    pr_info("Allocated %ld bytes of physical memory at address %p\n", size, ptr);

    return 0;
}

static void __exit test_exit(void)
{
    pr_info("Exiting test module\n");
}

module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Test module");

In the sample code above, first the calling memblock_alloc()function allocates a block of physical memory and stores its address in a pointer ptr. If the allocation fails, an error message is printed and returned -ENOMEM. If the allocation is successful, print the allocated memory block size and address.

3. Virtual memory management

Virtual memory refers to an abstract memory management method provided by the operating system for the process. It maps the address space required by the process to a certain area in the physical memory, so that the memory space seen by the process is continuous, while the actual The above memory may be scattered in different physical memory pages. Virtual memory provides many benefits to the operating system, including better memory management, higher availability, better security, and better performance, among others.

3.1 What is virtual memory

Virtual memory is a computer memory management technology that combines physical memory with a portion of space on the hard disk, allowing the operating system to manage memory more flexibly. Virtual memory divides the memory space required by a process into several virtual pages. The size of each virtual page is usually . The number of virtual 4KBpages that can be used by a process is huge, far greater than the size of physical memory.

Virtual memory technology allows the operating system to transfer some of the virtual pages needed by the process into physical memory. When the process no longer needs these virtual pages, the operating system can swap them to the disk, thus freeing up the physical memory space for other processes. use.

When using virtual memory, the memory space used by each process is composed of virtual addresses, not physical addresses. The operating system maps the virtual address required by the process to the actual physical address, thereby realizing the translation from the virtual address to the physical address. In this way, each process thinks that it has monopolized the entire physical memory, but in fact multiple processes can share the same physical memory.

The introduction of virtual memory has greatly improved the memory management capabilities of the operating system. Through virtual memory technology, the operating system can better manage memory resources, improving the performance and stability of the system.

3.2 The principle of virtual memory management

In virtual memory management, each process has an independent virtual address space, which is continuous, but not all addresses have been allocated physical memory. When a process needs to access a virtual address that has not allocated physical memory, the operating system will allocate physical memory for the address and map the virtual address to the physical address, so that the process can access the address.

Memory Management UnitThe implementation of virtual memory management relies on the MMU ( ) support on the hardware . The MMU is mainly responsible for the translation of virtual addresses to physical addresses. When a process accesses a virtual address, the MMU translates the address into a corresponding physical address, allowing the process to access physical memory.

Suppose a computer has 4GB of physical memory, and a process needs to use 10GB of memory space. The process would not function properly with traditional physical memory management. Therefore, the operating system uses part of the physical memory space as virtual memory, allowing processes to use virtual memory to expand their available memory space. When a process needs to access a certain page in the virtual memory, the page will be transferred from the disk to the physical memory and mapped to the virtual memory space, and the process can directly access the page. When the page is no longer needed, the operating system frees it from physical memory and saves it back to disk.

In this way, a process can expand its available memory space by using virtual memory even if the physical memory is insufficient to meet the memory requirements of the process. This virtual memory technology enables computer systems to support more processes and larger programs with limited physical memory.

3.3 Functions and examples related to virtual memory management

Virtual memory management involves many functions, among which the more commonly used ones include the following:

1.  mmap: It is used to map files or other objects to the address space of the process, and its function prototype is as follows:

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

Wherein, addrindicates the first address of the mapping area; lengthindicates the length of the mapping area; protindicates the access authority of the mapping area; flagsindicates the mapping option; fdindicates the file descriptor to be mapped; offsetindicates the file offset to be mapped. An example of use is as follows:

#include 
#include 
#include 
#include 

#define FILENAME "file.txt"

int main() {
    int fd = open(FILENAME, O_RDONLY);
    if (fd < 0) {
        perror("open file failed");
        exit(1);
    }

    char *map = mmap(NULL, 1024, PROT_READ, MAP_PRIVATE, fd, 0);
    if (map == MAP_FAILED) {
        perror("mmap failed");
        exit(1);
    }

    printf("content of the file:\n%s\n", map);

    munmap(map, 1024);
    close(fd);

    return 0;
}

This sample program opens a file, maps the file into memory, outputs the contents of the file, frees the memory, and closes the file.

2. munmap: Used to release the mapping relationship. Its function prototype is as follows:

int munmap(void *addr, size_t length);

Wherein, addrrepresents the first address of the mapping area; lengthrepresents the length of the mapping area. An example of usage is shown in the code above.

3. mlock: Used to lock a virtual memory area so that the area will not be replaced. Its function prototype is as follows:

int mlock(const void *addr, size_t len);

Wherein, addrindicates the first address of the virtual memory area to be locked; lenindicates the length of the virtual memory area to be locked.

An example of use is as follows:

#include 
#include 
#include 

#define LEN (1 << 20)

int main() {
    void *p = malloc(LEN);
    if (p == NULL) {
        perror("malloc failed");
        exit(1);
    }

    int ret = mlock(p, LEN);
    if (ret != 0) {
        perror("mlock failed");
        exit(1);
    }

    printf("locked %d MB memory\n", LEN / (1 << 20));

    free(p);

    return 0;
}

The sample program uses mallocthe function to allocate 1MB of memory, then uses mlockthe function to lock the memory area, and finally releases the memory. After running the program, you can see  locked 1 MB memory that the memory that has been successfully locked is output 1MB.

4. mprotect: Used to change the access rights of the virtual memory area. A file or other object can be mapped into the address space of the process, so as to realize the access to these objects. Its function prototype is as follows:

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

The meaning of each parameter is as follows, addrspecify the starting address of the mapping area, usually set to NULL, automatically allocated by the kernel; lengththe length of the mapping area, in bytes; protthe protection method of the mapping area, that is, the area can be read and written , execution or sharing, etc.; flagsthe type and attributes of the mapping area, such as private mapping or shared mapping, whether the mapping area can be updated, etc.; fdspecify the object to be mapped to the process address space, usually a file descriptor; offsetspecify from the object which Offset to start mapping, usually set to 0.

An example of use is as follows:

#include 
#include 
#include 
#include 

#define FILE_PATH "test.txt"
#define MAP_SIZE 1024

int main() {
    int fd = open(FILE_PATH, O_RDWR);
    if (fd < 0) {
        perror("open");
        return -1;
    }
    char *addr = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED) {
        perror("mmap");
        close(fd);
        return -1;
    }
    printf("%s", addr);
    if (munmap(addr, MAP_SIZE) < 0) {
        perror("munmap");
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}

4. Memory allocation and release

Memory allocation and release is a very important function in the operating system and a core part of memory management. In Linux, memory allocation and release are usually implemented using C library functions or kernel functions. The following will introduce the relevant knowledge of memory allocation and release in detail.

4.1 Overview of memory allocation and deallocation

Memory allocation and deallocation is an important part of operating system memory management. Memory allocation is when the operating system allocates memory space for applications so that the applications can perform their functions. Memory deallocation refers to applications releasing allocated memory space so that it can be used by other applications or by the operating system itself.

Inside the operating system, memory allocation and deallocation are the responsibility of the memory management subsystem. The memory management subsystem tracks the state of available and allocated memory and determines the best memory allocation and deallocation strategy. For most modern operating systems, the memory allocation and deallocation strategy is usually based on the concept of virtual memory.

Memory allocation and deallocation is one of the key factors of application performance. On the one hand, overallocating memory can lead to degraded application performance, as insufficient memory can lead to frequent swapping or garbage collection. On the other hand, unreleased memory can lead to memory leaks, which can lead to application crashes or other unexpected behavior.

When writing an application, it is very important to allocate and free memory correctly. Memory leaks are a common problem that can be avoided by writing code carefully and using memory analysis tools. Also, over-allocating memory can cause performance issues, so care needs to be taken in how you use memory.

4.2 Methods of memory allocation and release

In the Linux system, there are several methods of memory allocation and release:

  1. Static memory allocation: The memory space is allocated when the program is compiled, and it is generally used to allocate small blocks of memory.

  2. Stack memory allocation: When a function is called, the system will automatically allocate a piece of memory for the function to store information such as local variables and return addresses of the function. When the function is executed, this memory will be automatically released by the system.

  3. Heap memory allocation: The program can apply for a memory space of a specified size by calling a memory allocation function such as malloc(). When the program no longer needs this memory, it needs to release it back to the system by calling a memory release function such as free().

  4. Memory-mapped files: Programs can allocate memory by mapping files into memory. The memory-mapped file approach is usually used to handle large files.

  5. Shared memory: Multiple processes can share the same memory space, thereby realizing communication and data sharing between processes. Shared memory needs to be managed by calling the API provided by the system.

These methods are widely used in actual development, and developers need to choose appropriate memory allocation and release methods according to different scenarios and requirements.

4.3 Functions and examples related to memory allocation and release

In Linux, memory allocation and release are mainly implemented through the following functions:

  1. malloc() and  free()functions

  2. calloc() and  realloc()functions

  3. mmap() and  munmap()functions

The following are examples of the use of the above functions and, you can check the examples for learning.

4.3.1 malloc() and free() functions

malloc() and  free()are the most commonly used memory allocation and deallocation functions, stdlib.hprovided by the library. Among them, malloc()the function is used to allocate a memory space of a specified size and return the first address of the space; and free()the function is used to release the previously allocated memory space.

Sample code:

#include 
#include 

int main() {
    int* p = (int*)malloc(sizeof(int));  // 分配一个int类型的内存空间
    if (p == NULL) {  // 判断是否分配成功
        printf("Failed to allocate memory!\n");
        return -1;
    }
    *p = 123;  // 对分配的内存空间进行赋值
    printf("%d\n", *p);
    free(p);  // 释放之前分配的内存空间
    return 0;
}

4.3.2 calloc() and realloc() functions

calloc() And  realloc()these two functions can also be used to allocate memory space. Among them, calloc()the function is used to allocate a specified size of memory space, and will initialize the space 0; and realloc()the function is used to reallocate the previously allocated memory space.

Sample code:

#include 
#include 

int main() {
    int* p1 = (int*)calloc(5, sizeof(int));  // 分配5个int类型的内存空间,并将它们初始化为0
    if (p1 == NULL) {  // 判断是否分配成功
        printf("Failed to allocate memory!\n");
        return -1;
    }
    for (int i = 0; i < 5; i++) {
        printf("%d ", p1[i]);  // 输出分配的内存空间中的值
    }
    printf("\n");
    int* p2 = (int*)realloc(p1, 10 * sizeof(int));  // 重新分配10个int类型的内存空间
    if (p2 == NULL) {  // 判断是否分配成功
        printf("Failed to allocate memory!\n");
        return -1;
    }
    for (int i = 5; i < 10; i++) {
        p2[i] = i * 2;  // 对分配的新的内存空间进行赋值
    }
    for (int i = 0; i < 10; i++) {
        printf("%d ", p2[i]);  // 输出分配的内存空间中的值
    }
    printf("\n");
    free(p2);  // 释放之前分配的内存空间
    return 0;
}

4.2.3 mmap() and munmap() functions

mmap() And  munmap()these two functions are used to map a section of file or anonymous memory space in the address space of the process. Among them, mmap()the function is used to create a new mapping and return the starting address of the mapping area; and munmap()the function is used to cancel the previously created mapping.

Sample code:

#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[]) {
    int fd;
    char *file_memory;
    struct stat statbuf;

    /* 打开文件 */
    fd = open(argv[1], O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    /* 获取文件信息 */
    if (fstat(fd, &statbuf) == -1) {
        perror("fstat");
        exit(EXIT_FAILURE);
    }

    /* 将文件映射到内存中 */
    file_memory = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (file_memory == MAP_FAILED) {
        perror("mmap");
        exit(EXIT_FAILURE);
    }

    /* 打印文件内容 */
    printf("%s", file_memory);

    /* 撤销映射 */
    if (munmap(file_memory, statbuf.st_size) == -1) {
        perror("munmap");
        exit(EXIT_FAILURE);
    }

    /* 关闭文件 */
    if (close(fd) == -1) {
        perror("close");
        exit(EXIT_FAILURE);
    }

    return 0;
}

5. Process switching and memory management

Process switching and memory management are closely related. When a process is scheduled for execution, its corresponding virtual memory needs to be mapped to physical memory, that is, page table switching is performed. During process switching, the page table of the current process will be saved, and the page table of the next process will be loaded, which requires the page table mechanism in memory management. Therefore, process switching and memory management are closely related to ensure the normal operation of the process.

5.1 Overview of process switching

Process switching is one of the important concepts in the operating system, which refers to switching from one executing process to another and starting execution. When multiple processes are running at the same time, the operating system needs to switch between these processes so that each process has a chance to run and get the resources it needs.

Process switching usually involves saving the state of the current process so that execution of the process can be restarted at a later time. The OS then chooses the next process to execute and loads its state into the CPU. Process switching is a resource-intensive process because large amounts of data must be saved and loaded during the switch. However, it is also a necessary process to ensure the proper functioning of a multitasking operating system.

Process switching involves many aspects, including context switching, scheduling algorithm, process state, etc. For memory management, process switching also involves the management of memory mapping and virtual memory to ensure that each process can access the required memory space.

5.2 Relationship between process switching and memory management

Process switching and memory management are two important concepts in the operating system, and there is a close relationship between them. Process switching refers to the process in which the operating system transfers the control of the CPU from one process to another process according to a certain strategy in a multi-programming environment. In the process of process switching, the operating system needs to save the context information of the current process, including the program counter, registers, stack pointer, etc., so that the execution state of the process can be restored when switching back to the process.

Memory management is the allocation, recovery and management of memory by the operating system to provide memory resources for processes. The operating system corresponds the physical memory with the virtual address space through the virtual memory mechanism, and provides the virtual address space for the process, thereby realizing the memory isolation and protection between the processes. During process switching, the operating system needs to save the memory mapping information of the current process, so that the memory mapping state of the process can be restored correctly when switching back to the process.

Therefore, process switching and memory management are interdependent. When the operating system performs process switching, it needs to consider the memory state of the current process, including the mapping of the virtual address space and the usage of physical memory, so as to correctly restore the memory state of the process when switching back to the process. At the same time, memory management also needs to consider the impact of process switching. For example, it is necessary to avoid affecting the memory of other processes during memory allocation and recycling, so as to ensure the stability and security of the operating system.

5.3 Functions and examples related to process switching and memory management

There are many functions involved in process switching and memory management. Here are some commonly used functions and examples:

5.3.1 fork() function

fork()Function: used to create a new process, the new process has the same memory image as the parent process, but the memory between the parent and child processes is independent. Example:

#include 
#include 
#include 

int main() {
    int pid = fork();
    if (pid < 0) {
        perror("fork error");
        exit(1);
    } else if (pid == 0) {
        // Child process
        printf("Child process\n");
        exit(0);
    } else {
        // Parent process
        printf("Parent process\n");
    }
    return 0;
}

5.3.2 exec() function

exec()The function is used to load and execute a new program, which will overwrite the memory image of the original process. Example:

#include 
#include 
#include 

int main() {
    char* argv[] = {"ls", "-l", NULL};
    execvp("ls", argv);
    perror("exec error");
    exit(1);
}

5.3.3 mmap() function

mmap()The function is used to map a file or device into the address space of the process, so as to realize the access of the file or device. Example:

#include 
#include 
#include 
#include 

int main() {
    int fd = open("file.txt", O_RDONLY);
    if (fd < 0) {
        perror("open error");
        exit(1);
    }

    char* ptr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
    if (ptr == MAP_FAILED) {
        perror("mmap error");
        exit(1);
    }

    printf("%s", ptr);

    if (munmap(ptr, 4096) < 0) {
        perror("munmap error");
        exit(1);
    }

    close(fd);
    return 0;
}

5.3.4 malloc() function

malloc()The function is used to dynamically allocate memory and returns a pointer to the allocated memory. Example:

#include 
#include 

int main() {
    int* ptr = (int*) malloc(sizeof(int));
    if (ptr == NULL) {
        perror("malloc error");
        exit(1);
    }

    *ptr = 123;
    printf("%d\n", *ptr);

    free(ptr);
    return 0;
}

5.3.5 sbrk() function

sbrk()The function is used to expand or shrink the heap space of the process, and returns a pointer to the new top of the heap. Example:

#include 
#include 
#include 

int main() {
    int* ptr1 = (int*) sbrk(sizeof(int));
    if (ptr1 == (void*) -1) {
        perror("sbrk error");
        exit(1);
    }

    *ptr1 = 123;
    printf("%d\n", *ptr1);

    int* ptr2 = (int*) sbrk(sizeof(int));
    if (ptr2 == (void*) -1) {
        perror("sbrk error");
        exit(1

6. Tuning of Linux memory management

The tuning of Linux memory management refers to optimizing the efficiency of system memory usage by adjusting system parameters and configurations to achieve better performance and reliability.

6.1 Performance tuning of Linux memory management

In the Linux system, the performance tuning of memory management mainly involves the following aspects:

  • Memory usage tuning: optimize memory usage and improve memory utilization efficiency. Through the analysis of the memory usage, the kernel parameters can be adjusted in a targeted manner, and the system operating parameters can be adjusted to avoid memory waste and improve memory utilization.

  • Memory swap tuning: Reasonably configure memory swap space and memory swap strategies to ensure system stability and performance. It can be adjusted according to the actual situation of the system to avoid problems such as system crash caused by insufficient memory.

  • Memory mapping optimization: optimize the performance bottleneck of memory mapping operations to improve the efficiency of memory mapping operations. It can be optimized by increasing the size of the memory-mapped cache, optimizing the access method of the memory map, and so on.

  • Memory allocation tuning: optimize memory allocation operations to improve the efficiency and performance of memory allocation. It can be optimized by increasing the size of the memory allocation cache and optimizing the memory allocation algorithm.

When tuning the performance of memory management, it is necessary to fully consider the actual situation of the system and use various tuning methods comprehensively to achieve optimal performance and stability.

6.2 Detection and debugging of memory leaks

A memory leak means that the program does not release the memory in time after dynamically allocating it, resulting in the memory being unable to be used again, which eventually leads to problems such as insufficient memory in the system. In order to avoid the impact of memory leaks on the system, detection and debugging are required.

Linux provides some tools to detect and debug memory leak issues, such as:

  • Valgrind: A tool for memory debugging, memory leak detection, etc., which can detect problems such as unreleased memory and repeated release of memory.

  • AddressSanitizer: A tool for detecting memory errors, including memory leaks, memory access out of bounds, use of freed memory, and more.

  • GDB: A powerful debugger that can be used to debug memory leaks.

  • LeakTracer: A lightweight memory leak detection tool that can monitor whether dynamically allocated memory is released.

Using these tools can help developers discover memory leaks in time, and make timely adjustments and repairs.

6.3 Defragmentation and optimization of memory

Memory fragmentation refers to scattered small pieces of unused memory in memory, the sum of which may be sufficient to meet memory needs, but due to its scattered nature, cannot be effectively utilized. Memory fragmentation can cause memory allocation failures or performance degradation. Therefore, in order to improve memory utilization efficiency and performance, it is necessary to defragment and optimize memory.

Common memory defragmentation and optimization methods include:

  • Buddy System: Reduces fragmentation by merging small chunks of unused memory into larger chunks.

  • Memory pool: A certain amount of memory is pre-allocated when the program is initialized, and memory fragmentation is avoided through the cache mechanism.

  • Memory compression: Compress data in memory to reduce the size of unused memory blocks, thereby reducing memory fragmentation.

  • Memory manager: Using the memory manager, you can dynamically allocate and release memory blocks while avoiding memory fragmentation.

  • Memory alignment: By aligning memory allocation and release according to certain rules, memory fragmentation can be reduced.

To sum up, according to different application scenarios and memory usage, choosing an appropriate memory management method and optimization method can improve memory utilization efficiency and performance.

7. Application examples of Linux memory management

The following are some examples of common Linux memory management in different parts of the system. I will give examples from the three levels of application, driver and system from the shallower to the deeper.

7.1 Application scenarios of Linux memory management

Linux memory management is widely used, the following are some major application areas:

  1. Server application: As a popular server operating system, Linux's memory management solution can guarantee stability and reliability under high load, and provide excellent performance and scalability.

  2. Embedded systems: Linux is widely used in the embedded field, and its memory management mechanism can help developers run applications efficiently in limited memory space.

  3. Scientific computing and data processing: Linux provides support for high-performance computing and data processing. Memory management solutions are very important in these applications to ensure good performance and accuracy of these calculations.

  4. Operating system development and kernel programming: Linux kernel development requires an in-depth understanding of memory management mechanisms to ensure system stability and reliability, and improve performance and scalability.

  5. Virtualization: Linux memory management mechanism also plays a vital role in virtualization technology. Virtualization technology can divide physical memory into multiple virtual memory spaces, and the Linux memory management mechanism can effectively manage these virtual memory spaces.

In summary, Linux memory management plays an important role in various application fields, and its performance and stability are crucial to the successful operation of applications.

7.2 Application of Linux memory management in driver development

Linux memory management has an important application in driver development. Drivers need to allocate and manage memory in the kernel to perform various operations. The following are some application examples of memory management in driver development:

  1. Character device drivers: Many character device drivers need to allocate memory buffers to store data read from or written to the device. In this case, drivers typically allocate memory using the kmalloc() or vmalloc() functions.

  2. Network device drivers: Network device drivers often need to allocate memory buffers to store packets. In this case, drivers typically allocate pages using the alloc_pages() function.

  3. Block Device Drivers: Block device drivers are required to manage data blocks on disk. In this case, drivers usually use memory-mapping technology to manage memory, so that kernel buffers and disk data blocks can correspond in memory.

  4. Video Device Drivers: Video device drivers often need to allocate memory to store image data. In this case, drivers typically allocate memory using the vmalloc() function.

In short, in Linux driver development, memory management is an important task, and the driver must be able to allocate, release and manage memory. Therefore, the Linux kernel provides various memory management tools and functions so that drivers can manage memory efficiently.

7.3 Application of Linux memory management in system optimization

Linux memory management plays a vital role in system optimization. Optimizing memory management improves system performance and stability, and makes the system more reliable.

Some common memory management optimization techniques include:

  • Memory Compression: Reduce memory usage by compressing infrequently used memory pages. For example, using zswap can compress the memory page to the hard disk, thereby improving the response speed of the system.

  • Memory reclamation: Release memory pages that are no longer in use for reuse. For example, the Linux kernel has a memory reclamation mechanism that maximizes the use of available memory by reclaiming unused pages and reallocating memory.

  • Transparent Huge Pages: THP is a technique for merging memory pages into larger pages. It reduces the number of page table entries and can reduce memory fragmentation, thereby improving system performance.

  • Swap partition setting: You can set the swap partition to move unused memory pages to the hard disk, so as to reallocate memory when needed. However, swap partitions should be used with caution, as using too many swap partitions may result in slower system response.

In short, the application of Linux memory management can improve system performance and stability, making the system more reliable.

8. Summary

This article first introduces the concept and function of memory management, as well as the importance and basic structure of Linux memory management. Then, the physical memory management and virtual memory management are explained in detail, including their principles, methods, related functions and examples. Then it introduces the concept, method, related functions and examples of memory allocation and release, as well as the relationship between process switching and memory management.

Finally, it discusses performance tuning of memory management, detection and debugging of memory leaks, defragmentation and optimization of memory, and the application of Linux memory management in driver development and system optimization. In short, this article elaborates on all aspects of Linux memory management and is a comprehensive and detailed reference.

Reposted from: https://www.eet-china.com/mp/a213284.html

 

Guess you like

Origin blog.csdn.net/LinkSLA/article/details/131060731