A Linux system memory allocation accurate statistics method, device, equipment and storage medium

By classifying the Linux system memory space and using node structures and doubly linked lists to record memory allocation and deallocation information, the problem of inaccurate memory allocation statistics in existing technologies is solved, and accurate statistics and optimization of system memory are achieved.

CN115202862BActive Publication Date: 2026-07-14GUANGZHOU ANYKA MICROELECTRONICS CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
GUANGZHOU ANYKA MICROELECTRONICS CO LTD
Filing Date
2022-05-13
Publication Date
2026-07-14

AI Technical Summary

Technical Problem

Existing memory allocation statistics methods in the Linux kernel cannot accurately measure system memory allocation consumption, leading to longer software development cycles and increased costs.

Method used

By classifying memory space, creating node structures and filling them with memory statistics, and using a doubly linked list to record memory allocation and deallocation information, system memory statistics are performed based on the doubly linked list.

Benefits of technology

It enables precise statistics on Linux system memory allocation, allowing for timely detection of memory consumption issues, optimization of memory usage, and improvement of software development and maintenance efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115202862B_ABST
    Figure CN115202862B_ABST
Patent Text Reader

Abstract

The application belongs to the field of computer systems and storage, and discloses a Linux system memory allocation accurate statistics method, device, equipment and storage medium, the method comprises the following steps: classifying memory space; when system memory application is successful each time, creating a node structure body for memory statistics, and filling the classified memory space information of the successful application into the node structure body; creating a chain type double-linked list for memory statistics; when system memory application is successful each time, adding the node structure body information to the tail of the double-linked list; when the successfully applied memory is released in the subsequent running process, removing the corresponding node structure body information from the double-linked list; and based on the node structure body information in the double-linked list, the system memory is counted. The application can accurately count the number and direction of memory allocation in the system, and can quickly count the leaked memory and locate the memory leakage position when the system has memory leakage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer systems and storage, and in particular to a precise statistical method, apparatus, device, and storage medium for memory allocation in a Linux system. Background Technology

[0002] Currently, most embedded electronic products on the market use operating systems based on the open-source Linux kernel, such as Android. Because the Linux kernel is open-source and free, and is continuously maintained and developed, it offers good performance and is stable and reliable, thus it is widely used in various electronic products. However, Linux kernel memory management is relatively complex, adding slab memory allocation, vmalloc memory allocation, and virtual memory allocation to the traditional buddy system memory allocation.

[0003] The existing memory statistics methods designed in the Linux kernel are based on locality, and the statistics are independent of each other. Therefore, it is impossible to accurately count the amount of system memory consumed or the location of memory allocation. This directly leads to longer software development cycles, increased development costs, and is not conducive to the development, debugging, and maintenance of product software.

[0004] Regarding the aforementioned technologies, the inventors discovered that existing Linux system memory allocation methods have the problem of being unable to accurately count system memory allocations. Summary of the Invention

[0005] To accurately measure system memory allocation, this application provides a method, apparatus, device, and storage medium for accurately measuring Linux system memory allocation.

[0006] Firstly, this application provides a precise statistical method for Linux system memory allocation, which features precise statistical analysis of system memory allocation.

[0007] This application is achieved through the following technical solution:

[0008] A precise statistical method for memory allocation in a Linux system, comprising the following steps:

[0009] Categorize memory space;

[0010] Each time a system memory allocation is successful, a node structure for memory statistics is created, and the information of the successfully allocated and categorized memory space is filled into the node structure.

[0011] Create a doubly linked list for memory statistics; each time system memory is successfully allocated, add the information of the node structure to the tail of the doubly linked list; when the successfully allocated memory is released during subsequent operation, remove the information of the corresponding node structure from the doubly linked list.

[0012] Based on the information of the node structure in the doubly linked list, the system memory is calculated.

[0013] In a preferred embodiment, this application can be further configured such that the node structure includes a page header address field, a page count field, a main calling function name field, a called allocator interface function name field, and a memory space allocation type field.

[0014] In a preferred embodiment, this application can be further configured such that: the doubly linked list includes a list head and a statistical node, the statistical node is used to store the address of the node structure, and the address of the node structure maps to the information of the corresponding node structure;

[0015] Each time system memory is successfully allocated, the information of the node structure is added to the tail of the doubly linked list through the statistics node; when the successfully allocated memory is released during subsequent operation, the information of the corresponding node structure is removed from the doubly linked list by removing the corresponding statistics node.

[0016] In a preferred embodiment, this application can be further configured such that the step of calculating system memory based on the information of the node structure in the doubly linked list includes:

[0017] The main function names of the node structures in the statistical nodes are counted to obtain the allocated amount of system memory space;

[0018] The system obtains the allocation path of memory space by counting the names of the called allocator interface functions of the node structure in the statistical node.

[0019] In a preferred embodiment, this application can be further configured such that the classification of memory space includes:

[0020] Memory space is classified into memory used by the kernel space, memory allocated by the user space, memory reserved by the kernel, and high memory used by the user space.

[0021] In a preferred embodiment, this application can be further configured such that the memory used by the kernel space includes kernel general memory, kernel stack memory, kernel page table memory, vmalloc memory, and slab memory.

[0022] In a preferred embodiment, this application can be further configured such that the memory requested by the user space includes memory pages associated with files, anonymous memory unrelated to files, and pages that are prohibited from being swapped out.

[0023] In a preferred embodiment, this application may be further configured such that the memory reserved by the kernel includes the memory occupied by the reserved DTB image, the reserved hash table memory, the reserved perCPU memory, the reserved device tree memory, and other reserved miscellaneous memory.

[0024] In a preferred embodiment, this application may be further configured such that the high-end memory used by the user space includes a large capacity of high-end memory.

[0025] Secondly, this application provides a precise statistical device for Linux system memory allocation, which has the feature of accurately statistically analyzing system memory allocation.

[0026] This application is achieved through the following technical solution:

[0027] A precise statistical device for memory allocation in a Linux system, comprising,

[0028] The classification module is used to classify memory space;

[0029] The node structure module is used to create a node structure for memory statistics each time the system memory allocation is successful, and to fill the node structure with information about the successfully allocated and categorized memory space.

[0030] The doubly linked list module is used to create a chain-like doubly linked list for memory statistics. Each time system memory is successfully allocated, the information of the node structure is added to the tail of the doubly linked list. When the successfully allocated memory is released during subsequent operation, the information of the corresponding node structure is removed from the doubly linked list.

[0031] The statistics module is used to calculate system memory usage based on information from the node structures in the doubly linked list.

[0032] Thirdly, this application provides a computer device with the feature of accurately calculating system memory allocation.

[0033] This application is achieved through the following technical solution:

[0034] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of any of the above-described methods for accurate statistical analysis of memory allocation in a Linux system.

[0035] Fourthly, this application provides a computer-readable storage medium with the feature of accurately statistically analyzing system memory allocation.

[0036] This application is achieved through the following technical solution:

[0037] A computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of any of the above-described methods for precise statistical analysis of memory allocation in a Linux system.

[0038] In summary, compared with the prior art, the beneficial effects of the technical solution provided in this application include at least the following:

[0039] 1. Classify the memory space of the Linux system to determine the type of memory allocation and its corresponding attribute information. Each time system memory is successfully allocated, create a node structure for memory statistics and fill it with the information of the successfully allocated memory space to retain this information. Create a doubly linked list for memory statistics. Each time system memory is successfully allocated, add the node structure information to the end of the doubly linked list to record the successfully allocated memory space. When successfully allocated memory is released during subsequent operation, remove the corresponding node structure information from the doubly linked list to ensure that the statistics in the doubly linked list only show the memory currently in use. Based on the information in the node structures of the doubly linked list, system memory is statistically analyzed to achieve comprehensive and accurate memory allocation statistics. This also allows for the timely detection of memory consumption problems in the Linux system, especially in large-scale embedded system software development. Timely and dynamic statistics of memory consumption and memory optimization are beneficial for product software development, debugging, and maintenance.

[0040] 2. Design a node structure based on the type of memory space requested. Use the page header address to indicate the location of memory allocation, the main calling function name and page count to indicate the amount of memory allocated, and the allocator interface function name to indicate the allocation path. This facilitates tracking the memory allocation path, allowing for precise statistics on the amount and destination of memory allocations in the system. Furthermore, when a memory leak occurs, the page header address and recorded main calling function name can be used to identify which address spaces are experiencing continuous memory growth without being released, thus pinpointing the location of the memory leak. This achieves the goal of quickly calculating and locating leaked memory.

[0041] 3. Using a doubly linked list to update the memory information currently in use by the system in real time facilitates accurate statistics of system memory allocation. At the same time, the information of the node structure is added to the doubly linked list in the form of statistical nodes, or the corresponding statistical node is removed when the node structure is released. The address of the node structure is stored in the statistical node, and the information of the corresponding node structure is mapped through the address of the node structure. This helps to reduce the space occupied by the doubly linked list in the system and reduce the system load. Attached Figure Description

[0042] Figure 1 A flowchart illustrating a precise statistical method for Linux system memory allocation, provided as an exemplary embodiment of this application.

[0043] Figure 2 This is a schematic diagram of the structure of a doubly linked list in a precise statistical method for Linux system memory allocation, provided as another exemplary embodiment of this application.

[0044] Figure 3 A structural block diagram of a precise statistical device for Linux system memory allocation provided as another exemplary embodiment of this application. Detailed Implementation

[0045] This specific embodiment is merely an explanation of this application and is not intended to limit it. After reading this specification, those skilled in the art can make modifications to this embodiment without contributing any inventive step, but such modifications are protected by patent law as long as they fall within the scope of the claims of this application.

[0046] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0047] Furthermore, the term "and / or" in this article is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. Additionally, the character " / " in this article, unless otherwise specified, generally indicates that the preceding and following related objects have an "or" relationship.

[0048] The embodiments of this application will now be described in further detail with reference to the accompanying drawings.

[0049] Reference Figure 1This application provides a method for accurate statistical analysis of memory allocation in a Linux system. The main steps of the method are described below.

[0050] S1: Classify memory space;

[0051] S2: Each time a system memory allocation is successful, a node structure for memory statistics is created, and the information of the successfully allocated and categorized memory space is filled into the node structure;

[0052] S3: Create a doubly linked list for memory statistics; each time the system successfully allocates memory, add the node structure information to the tail of the doubly linked list; when the successfully allocated memory is released during subsequent operation, remove the corresponding node structure information from the doubly linked list.

[0053] S4: Calculate system memory usage based on information from the node structure in the doubly linked list.

[0054] Specifically, based on the Linux system, memory space is divided into memory used by the kernel space and memory used by the user space. Different applications run in their own independent user spaces, but there is only one kernel space for each application. The physical memory requested by the user space and kernel space is allocated from the kernel's buddy system and then mapped to different virtual spaces. Since the memory allocated by the buddy system is based on page size, with each page typically 4KB, it facilitates system memory maintenance and management and improves memory allocation and reclamation efficiency. To address the issue of small memory fragmentation during normal Linux system operation, this application adds a slab allocator to the Linux kernel to solve the problem of small memory usage during Linux system operation, while improving system memory allocation efficiency and system performance. Furthermore, based on the physical address space of allocated memory, physical addresses can be divided into contiguous and non-contiguous memory. To address the problem of non-contiguous physical memory allocation, this application adds a vmalloc allocator to the Linux kernel to solve the problem of non-contiguous physical memory allocation during Linux system operation. In this application, the slab allocator and vmalloc allocator are allocator interfaces for the non-buddy system.

[0055] This application classifies memory space according to the memory K used in kernel space, the memory U allocated in user space, the memory R reserved by the kernel, and the high memory H used in user space.

[0056] The memory K used in kernel space primarily includes memory allocated from the source code of the kernel architecture, kernel drivers, and kernel protocol stack. This includes kernel stack memory, page table memory, virtual contiguous memory allocated by vmalloc, and block memory allocated by slab. Therefore, the memory used in kernel space can be further subdivided into the following memory types, where uppercase letters represent abbreviations of memory space names:

[0057] (1) Memory statistics S (short for slab) allocated using the slab allocator in the kernel;

[0058] (2) Memory statistics V (short for vmalloc) allocated using the vmalloc allocator in the kernel;

[0059] (3) Memory statistics P (short for Pagetable) of page table allocation in the kernel;

[0060] (4) Memory statistics T (short for Stack) of stack allocation in the kernel;

[0061] (5) Memory statistics K (short for Kernel) allocated in other parts of the kernel.

[0062] User-space memory U primarily includes memory needed to load the user program image, stack memory required for program execution, cache memory needed for file system operations, and memory allocated through system calls such as malloc and mmap. Therefore, user-space memory can be further subdivided into the following categories, where uppercase letters represent abbreviations of memory space names:

[0063] (1) Statistics F (short for file-backed pages) of memory pages allocated in user space and associated with files;

[0064] (2) User space requests for anonymous memory pages (A, short for anonymous pages) that are unrelated to files;

[0065] (3) User space requests memory pages that cannot be swapped out (U, short for Unevictable pages), which are memory pages that cannot be paged out or swapped out.

[0066] The kernel-reserved memory R primarily includes memory used by the kernel's DTB image, hash table memory, perCPU cache memory, device tree memory, and other miscellaneous memory. Kernel-reserved memory can be further subdivided into the following memory types, where uppercase letters indicate abbreviations of memory space names:

[0067] (1) Statistical data on memory pages occupied by the DTB image retained during the Linux system startup phase (D is an abbreviation for device treeblob);

[0068] (2) Statistics on memory pages occupied by the hash table (abbreviation for hash table) during the Linux system startup phase;

[0069] (3) The memory page statistics per CPU (P, short for page table) retained during the Linux system startup phase;

[0070] (4) Statistical data on memory pages occupied by the device tree (abbreviation for device tree) during the Linux system startup phase;

[0071] (5) Statistics on memory pages occupied by other miscellaneous items reserved during the Linux system startup phase (O is an abbreviation for others).

[0072] The high-end memory H (short for hashtable) used in user space is a large-capacity high-end memory. If memory allocation using HugePages is used, the statistics of large-page memory in the high-end address space need to be calculated.

[0073] The memory K used in the kernel space includes kernel general memory KK, kernel stack memory KT, kernel page table memory KP, vmalloc memory KV, and slab memory KS.

[0074] The memory U requested by the user space includes file-associated memory pages UF, file-independent anonymous memory UA, and pages UU that are prohibited from being swapped out.

[0075] The kernel-reserved memory R includes the reserved memory RD occupied by the DTB image, the reserved hash table memory RH, the reserved perCPU memory RP, the reserved device tree memory RT, and the reserved other miscellaneous memory RO.

[0076] The high-end memory H used in user space includes large-capacity high-end memory HH.

[0077] That is, the memory allocation space type is divided into main memory type and secondary memory type. The classification results of the memory allocation space in Linux system are shown in Table 1.

[0078] Table 1

[0079] main memory type Secondary memory type Combinatorial space type Function Description K K KK Kernel General Memory T KT Kernel stack memory P KP Kernel page table memory V KV vmalloc memory S KS slab memory U F UF Memory pages associated with files A UA Anonymous memory unrelated to files U UU Pages that are not allowed to be swapped out R D RD Memory usage of the retained DTB image H RH Reserved hash table memory P RP Reserved PerCPU memory T RT Reserved device tree memory O RO Other miscellaneous memory reserved H H HH High-capacity memory, high-end memory

[0080] Therefore, this application classifies memory space according to the memory K (kernel) used by the kernel space, the memory U (user) requested by the user space, the memory R (reserved) reserved by the kernel, and the high memory H (huge) used by the user space.

[0081] Secondly, each time a system memory allocation is successful, a node structure for memory statistics is created, and the information of the successfully allocated memory space is filled into the node structure. During the Linux system's operation, each time memory is successfully allocated from the buddy system, a node structure is created, and the information of the currently allocated memory is filled into that node structure.

[0082] The node structure includes the page header address, page count, main calling function name, called allocator interface function name, and memory space type. The called allocator interface function name includes the called buddy system interface function name and other allocator interface function name entries. The node structure is shown in Table 2.

[0083] Table 2

[0084]

[0085] Based on the node structure, the information of successfully allocated memory space in the node structure includes the following elements: the address of the first page after successful memory allocation, the number of pages allocated, the function name of the buddy system allocator interface called during the memory allocation, the function name of other allocator interfaces (slab / vmalloc) called during the memory allocation, and the space type of the memory allocation.

[0086] The main calling function name is the name of the function that first calls the memory allocation request in the kernel. The called buddy system interface function name is the name of the memory allocation function provided by the buddy system. The other allocator interface function names refer to the function names provided by the slab and vmalloc memory allocators.

[0087] When analyzing memory allocation, given the total allocatable memory in the system, the amount of allocated memory can be obtained by analyzing the names of the main calling functions, thus identifying where in the Linux kernel how much memory was requested. By analyzing the names of called buddy system interface functions and other allocator interface functions, the allocation path of the system memory can be determined, indicating which allocator interface allocated the memory, facilitating memory allocation tracking. This allows for precise statistics on used memory, the destination of allocated memory, and the remaining available memory. Furthermore, when a memory leak occurs, the page header address and recorded main calling function names can be used to identify which address spaces are experiencing continuous memory growth without being released, allowing for precise location of the memory leak. This enables rapid statistics on leaked memory and its location.

[0088] This application obtains the memory allocation statistics from the doubly linked list, which shows the total amount of memory already allocated in the doubly linked list under the current Linux system operating environment. By comparing this total amount of memory that can be allocated in the system with the total amount of memory that can be allocated in the system, if the remaining free memory decreases after subtracting the total amount of memory already allocated from the total amount of memory that can be allocated in the linked list, it indicates that there is a memory leak. This allows for more precise, faster, and more convenient location of memory leaks, making the situation clear at a glance.

[0089] Create a doubly linked list for memory statistics; each time a system memory allocation is successful, add the created node structure to the end of the doubly linked list until the last memory allocation is successful.

[0090] When the successfully allocated memory is released during subsequent operation, the corresponding node structure is released from the doubly linked list, ensuring that the statistics in the doubly linked list only include memory that is currently in use, thus achieving accurate statistics on memory allocation.

[0091] Reference Figure 2 A doubly linked list includes a head and a statistics node. The statistics node stores the address of the node structure, and the address of the node structure maps to the information of the corresponding node structure.

[0092] Finally, based on the information in the node structure of the doubly linked list, the system memory usage is calculated.

[0093] The above embodiments will be described in detail below.

[0094] For user space memory allocation, Linux system user programs call memory allocation functions such as malloc, mmap, fork, and execute in the Linux system API. Through the libc library, they use system call interfaces to enter the Linux kernel and request virtual address memory allocation from the buddy system. The buddy system then allocates the physical space of the requested memory size, which is then mapped to the corresponding virtual address space through page tables. Alternatively, a page fault interrupt can trigger the buddy system's memory allocation function interface to request physical space of the relevant size, which is then mapped to the corresponding virtual address space through page tables. Or, file system function interfaces can be used to perform read and write operations, allocating physical space of the requested memory size from the buddy system, which is then mapped to the corresponding virtual address space through page tables.

[0095] After a memory allocation is successful, the allocated memory is included in the active, inactive, and unevictable linked lists of the Linux kernel's LRU algorithm, based on the statistical range in which the allocated memory falls. At the same time, a doubly linked list is added to include the memory allocation information in different statistical ranges into the statistical nodes of the doubly linked list for statistical management, so that the memory in user space can be accurately counted without omission.

[0096] HugePages allocated by user space are also counted in high memory using the same method as user space memory statistics.

[0097] There are three ways to request memory allocation in kernel space: the first is to request large blocks of memory directly through the buddy system; the second is to request memory allocation through the slab memory allocator; and the third is to request virtual memory through vmalloc.

[0098] In particular, requests for large blocks of memory through the buddy system are also counted using the same method as user space memory statistics.

[0099] Memory allocated through the slab allocator ultimately requests physical memory from the buddy system via the buddy system interface. Memory allocated by the slab allocator is subject to precise allocation statistics. After each successful slab memory allocation, the Linux kernel records the memory allocation attributes in the kernel's SReclaimable and SUnreclaim memory variables. Simultaneously, a doubly linked list is added to include memory allocation information within different attribute statistical ranges into the statistical nodes of the doubly linked list for statistical management, achieving precise statistics.

[0100] In this process, virtual memory is requested through vmalloc, which ultimately requests physical memory from the buddy system through the buddy system interface. This requested memory is recorded in the vmallocinfo file in the proc file system of the Linux system. After the vmalloc memory request is successful, a doubly linked list is added to include the memory information of the virtual memory request in the memory node struct structure of the doubly linked list for statistical management. Therefore, this part of the memory allocation will be accurately counted.

[0101] In the Linux kernel, there are still some kernel and driver code instances where large blocks of memory are requested directly through the buddy system function interface. After the buddy system successfully allocates the memory, the memory information that was not previously recorded in the memory allocation statistics is included in the statistics nodes of the doubly linked list for statistical management, thereby achieving accurate statistics.

[0102] For kernel reserved space memory allocation, the Linux kernel constructs a global structure for physical memory allocation using memory information in the device tree during startup. Then, the Linux system uses the memblock allocator in the early stages to request memory that needs to be reserved in the Linux system, such as memory occupied by the DTB image, hash table, per-CPU, device tree, and other miscellaneous memory reserved during system startup. After this reserved memory allocation is completed, the remaining memory is handed over to the Linux system's buddy system for allocation. Since reserved memory is requested in the memblock allocator during Linux system startup and is allocated independently, Linux currently does not manage it. Therefore, after the reserved memory request is successfully allocated by the buddy system, the memory information that was not previously included in memory allocation statistics is added to the statistics nodes of a doubly linked list for accurate statistics management.

[0103] In summary, while retaining the basic memory statistics built into the Linux system, a doubly linked list is used to perform accurate statistics on memory allocation for the entire memory space of the Linux system, including the memory reserved by memblock at system startup, memory requested by directly calling the buddy system interface, memory allocated by slab and vmalloc allocators, and high memory.

[0104] During runtime, a Linux system requests memory allocation from its buddy system, and can use different allocation interfaces, for example,

[0105] The `alloc_pages` function requests the allocation of a page block of order `order` and returns a `page` instance.

[0106] The alloc_page function is a simplified form of the alloc_pages function when the order is 0, allocating only one page;

[0107] The __get_free_pages function is a wrapper around the alloc_pages function. It can only allocate pages from the low memory region and returns the virtual address.

[0108] The __get_free_page function is a simplified form of the __get_free_pages function when the order is 0, allocating only one page.

[0109] `get_zeroed_page` is a flag set by the function `__get_free_pages` for the parameter `gfp_mask`.

[0110] The simplified form of __GFP_ZERO with an order of 0, which allocates only one page and initializes it with zero.

[0111] The following interface can be used to release memory from the buddy system:

[0112] The __free_pages function: releases the memory at the address of a page instance of order order;

[0113] The `free_pages` function: releases the starting kernel virtual address of a physical page of order `order`.

[0114] When using the slab allocator, the general memory cache allocation and deallocation interfaces are as follows:

[0115] kmalloc: The block allocator allocates a suitable general-purpose memory cache;

[0116] krealloc: reallocates memory for an object based on its length;

[0117] kfree: Release memory.

[0118] When using the slab allocator, the dedicated memory cache allocation interface functions are as follows:

[0119] Create a memory cache: kmem_cache_create;

[0120] Allocate an object from the specified memory cache: kmem_cache_alloc;

[0121] Release object: kmem_cache_free;

[0122] Destroy memory: kmem_cache_destroy.

[0123] When using the vmalloc allocator, the allocation and deallocation interfaces are as follows:

[0124] The function interface for allocating memory: vmalloc;

[0125] The function interface for releasing memory: vfree.

[0126] Therefore, when performing memory statistics, the memory allocation path can be easily tracked by counting the names of the allocation interface functions.

[0127] During the Linux system startup process, memory management uses the memblock allocator by default. After initializing the memory of the memblock allocator, the memory information reserved during the Linux kernel startup phase is requested and stored through the memblock_reserve interface.

[0128] After the Linux memory buddy system allocator, slab allocator, and vmalloc allocator are initialized, a doubly linked list is created and initialized. The statistical nodes of the doubly linked list are created by the kmem_cache allocator of slab in the kernel_init thread.

[0129] During system operation, after each successful memory allocation by the buddy system, a node structure is created to store memory information. The page header address, the number of pages allocated, the function name of the buddy system allocator interface called during the memory allocation, the function name of other allocator interfaces called during the memory allocation, and the memory type information of the allocated memory are added to a doubly linked list as statistical nodes. This ensures that all retained memory information is managed by adding statistical nodes to the doubly linked list. When successfully allocated memory is subsequently released, the corresponding statistical node is found in the doubly linked list based on the page address of the released memory, and the corresponding statistical node is removed from the doubly linked list. This achieves the management of dynamically allocated memory during system operation.

[0130] The specific method for parsing memory allocation statistics includes creating a smart_meminfo file through the proc file system, providing the seq_read interface of the struct file_operations operation of the proc file system, and dynamically reading the current memory allocation information and statistics through the cat command in the Linux file system. After the application layer opens the smart_meminfo file through the proc file system, it obtains the information of the statistical nodes one by one through the doubly linked list of Linux memory, and then classifies and counts them. The obtained statistical information is shown in Table 3.

[0131] Table 3

[0132]

[0133]

[0134] This application classifies the memory space of the Linux system to determine the type of memory allocation and its corresponding attribute information. Each time system memory is successfully allocated, a node structure for memory statistics is created, and the information of the successfully allocated memory space is filled into the node structure to retain this information. A doubly linked list is created for memory statistics. Each time system memory is successfully allocated, the information of the node structure is added to the end of the doubly linked list to collect statistics on the successfully allocated memory space. When the successfully allocated memory is released during subsequent operation, the information of the corresponding node structure is removed from the doubly linked list to ensure that the statistics in the doubly linked list only represent memory currently in use. System memory is then statistically analyzed based on the information in the node structures of the doubly linked list.

[0135] In summary, based on the space partitioning of Linux system memory allocation, when statistically analyzing memory allocation data, the path and amount of memory consumption can be accurately determined by classifying memory space types. This allows for rapid assessment of the memory consumption of applications and the kernel during system operation, as well as the presence of memory leaks, achieving the goal of accurate statistical analysis of Linux system memory allocation. It also enables timely detection of memory consumption issues in Linux systems, particularly in large-scale embedded system software development. Timely and dynamic statistics of memory consumption and subsequent memory optimization are beneficial for product software development, debugging, and maintenance. Furthermore, it significantly improves the efficiency of software memory management and analysis in Linux systems, contributing to improved system stability and ease of maintenance. This enhances software development efficiency, reduces development costs, and demonstrates practical value and innovation.

[0136] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.

[0137] Reference Figure 3 This application also provides a precise statistical device for Linux system memory allocation, which corresponds one-to-one with the precise statistical method for Linux system memory allocation described in the above embodiments. This precise statistical device for Linux system memory allocation includes...

[0138] The classification module is used to classify memory space;

[0139] The node structure module is used to create a node structure for memory statistics each time the system memory allocation is successful, and to fill the node structure with information about the successfully allocated and categorized memory space.

[0140] The doubly linked list module is used to create a linked doubly linked list for memory statistics. Each time the system successfully allocates memory, the information of the node structure is added to the end of the doubly linked list. When the successfully allocated memory is released during subsequent operation, the information of the corresponding node structure is removed from the doubly linked list.

[0141] The statistics module is used to calculate system memory usage based on information from the node structures in a doubly linked list.

[0142] The classification module includes memory units used in kernel space, memory units allocated in user space, memory units reserved by the kernel, and high memory units used in user space.

[0143] The memory units used in kernel space are used to classify memory space according to the memory used in kernel space K. The memory used in kernel space K includes kernel general memory KK, kernel stack memory KT, kernel page table memory KP, vmalloc memory KV, and slab memory KS.

[0144] The memory units allocated by the user space are used to classify the memory space according to the memory U allocated by the user space. The memory U allocated by the user space includes memory pages UF associated with files, anonymous memory UA unrelated to files, and pages UU that are prohibited from being swapped out.

[0145] Kernel-reserved memory units are used to classify memory space according to kernel-reserved memory R. Kernel-reserved memory R includes reserved memory RD occupied by DTB images, reserved hash table memory RH, reserved perCPU memory RP, reserved device tree memory RT, and reserved other miscellaneous memory RO.

[0146] High-end memory units used in user space are used to classify memory space according to the high-end memory used in user space H, which includes large-capacity high-end memory HH.

[0147] For specific limitations regarding a precise statistical device for Linux system memory allocation, please refer to the limitations of a precise statistical method for Linux system memory allocation described above, which will not be repeated here. Each module in the aforementioned precise statistical device for Linux system memory allocation can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware or independently of the processor in the computer device, or stored in software in the memory of the computer device, so that the processor can call and execute the operations corresponding to each module.

[0148] In one embodiment, a computer device is provided, which may be a server. The computer device includes a processor, memory, a network interface, and a database connected via a system bus. The processor provides computing and control capabilities. The memory includes a non-volatile storage medium and internal memory. The non-volatile storage medium stores an operating system, computer programs, and a database. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage medium. The network interface is used to communicate with external terminals via a network connection. When the computer program is executed by the processor, it implements any of the aforementioned methods for accurate statistical analysis of Linux system memory allocation.

[0149] In one embodiment, a computer-readable storage medium is provided, including a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein the processor executes the computer program to perform the following steps:

[0150] S1: Classify memory space;

[0151] S2: Each time a system memory allocation is successful, a node structure for memory statistics is created, and the information of the successfully allocated and categorized memory space is filled into the node structure;

[0152] S3: Create a doubly linked list for memory statistics; each time the system successfully allocates memory, add the node structure information to the tail of the doubly linked list; when the successfully allocated memory is released during subsequent operation, remove the corresponding node structure information from the doubly linked list.

[0153] S4: Calculate system memory usage based on information from the node structure in the doubly linked list.

[0154] Furthermore, a computer-readable storage medium can be a computer-readable and writable storage medium.

[0155] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include volatile memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0156] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is used as an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the system can be divided into different functional units or modules to complete all or part of the functions described above.

Claims

1. A precise statistical method for memory allocation in a Linux system, characterized in that, Includes the following steps, The memory space is classified, which includes classifying the memory space according to the memory used by the kernel space, the memory requested by the user space, the memory reserved by the kernel, and the high memory used by the user space. Each time a system memory allocation is successful, a node structure for memory statistics is created, and the information of the successfully allocated and categorized memory space is filled into the node structure. The node structure includes a page header address, a page count, a main calling function name, a called allocator interface function name, and a memory space type. Create a doubly linked list for memory statistics. The doubly linked list includes a list head and a statistics node. The statistics node is used to store the address of the node structure, and the address of the node structure maps to the information of the corresponding node structure. Each time system memory is successfully allocated, the information of the node structure is added to the tail of the doubly linked list through the statistics node; when the successfully allocated memory is released during subsequent operation, the information of the corresponding node structure is removed from the doubly linked list by removing the corresponding statistics node. Based on the information of the node structures in the doubly linked list, the system memory is statistically analyzed, including: analyzing the main calling function names of the node structures in the statistical nodes to obtain the allocated amount of system memory space; and analyzing the called allocator interface function names of the node structures in the statistical nodes to obtain the allocation path of system memory space.

2. The precise statistical method for Linux system memory allocation according to claim 1, characterized in that, The node structure includes a page header address field, a page count field, a main calling function name field, a called allocator interface function name field, and a memory space allocation type field.

3. The precise statistical method for Linux system memory allocation according to claim 1, characterized in that, The memory used in the kernel space includes general kernel memory, kernel stack memory, kernel page table memory, vmalloc memory, and slab memory.

4. The precise statistical method for Linux system memory allocation according to claim 1, characterized in that, The memory requested by the user space includes memory pages associated with files, anonymous memory unrelated to files, and pages that are prohibited from being swapped out.

5. The precise statistical method for Linux system memory allocation according to claim 1, characterized in that, The memory reserved by the kernel includes the memory occupied by the reserved DTB image, the reserved hash table memory, the reserved perCPU memory, the reserved device tree memory, and other reserved miscellaneous memory.

6. The precise statistical method for Linux system memory allocation according to claim 1, characterized in that, The high-end memory used in the user space includes large-capacity high-end memory.

7. A precise statistical device for memory allocation in a Linux system, characterized in that, include, The classification module is used to classify memory space; The node structure module is used to create a node structure for memory statistics each time the system memory allocation is successful, and to fill the node structure with information about the successfully allocated and categorized memory space. The doubly linked list module is used to create doubly linked lists in a linked structure for memory statistics. Each time system memory is successfully allocated, the information of the node structure is added to the tail of the doubly linked list; when the successfully allocated memory is released during subsequent operation, the information of the corresponding node structure is removed from the doubly linked list. The statistics module is used to calculate system memory usage based on information from the node structures in the doubly linked list.

8. A computer device, characterized in that, The method includes a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the steps of the method according to any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Method for detecting internal memory leakage of Linux kernel

    CN102866947A