Memory management method, storage medium, program product, controller and vehicle
By configuring an independent cache space for each process and optimizing the memory allocation and release process, the problem of low efficiency in inter-process shared memory is solved, system performance and communication efficiency are improved, and lock conflicts and memory leaks are avoided.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-04
- Publication Date
- 2026-03-31
AI Technical Summary
In existing technologies, the read and write efficiency of shared memory between processes is low, which leads to low communication efficiency. Furthermore, frequent memory allocation and release can cause lock collisions and performance degradation, potentially resulting in memory leaks.
Each process is configured with an independent cache space, and the process's memory operations are performed directly in this cache space, reducing interaction with the shared memory pool. The memory allocation and release process is optimized through the identification of memory units and linked list management.
It improves system performance and communication throughput, avoids frequent lock conflicts and memory leaks, and enhances the efficiency of memory operations.
Smart Images

Figure CN121764701A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of memory management technology, and more particularly to a memory management method, storage medium, program product, controller, and vehicle. Background Technology
[0002] Currently, multiple processes can communicate through shared memory. For example, they can obtain memory allocation requests triggered by each process and allocate shared memory to that process based on the request, allowing the process to perform read and write operations on the shared memory. However, since each process needs to allocate memory from the shared memory before performing read and write operations, which takes a considerable amount of time, the read and write efficiency of each process on shared memory is low, resulting in low efficiency in inter-process communication. Summary of the Invention
[0003] This application provides a memory management method that improves system performance and at least partially solves the above-mentioned technical problems.
[0004] To achieve the above objectives, according to a first aspect of this application, a memory management method is provided, the method comprising:
[0005] In response to an operation request from the first process or a second process communicating with the first process, the system operates on the independent cache space requested by the first process from the shared memory pool.
[0006] Optionally, the operation request includes a memory release request from the second process, and the operation request in response to the first process or the second process communicating with the first process, involving the operation on the independent cache space requested by the first process from the shared memory pool, includes:
[0007] In response to the memory release request of the second process, the memory unit corresponding to the memory release request is released to the cache space of the first process that requested the memory unit.
[0008] Optionally, responding to the memory release request of the second process and releasing the memory unit corresponding to the memory release request to the cache space of the first process that requested the memory unit includes:
[0009] Based on the identifier of the memory unit to be released carried in the memory release request, the memory unit to be released is released, and the linked list corresponding to the memory unit to be released is updated.
[0010] Optionally, updating the linked list corresponding to the memory unit to be released includes:
[0011] When the memory unit to be released is a memory block, the memory page to which the memory block to be released belongs is determined according to the first memory capacity parameter of the memory block to be released;
[0012] When all memory blocks of the memory page to which the page belongs are in a free state, the memory page to which the page belongs is removed from the first linked list corresponding to the first memory capacity parameter, and the memory page to which the page belongs is added to the second linked list corresponding to the first memory capacity parameter.
[0013] Optionally, updating the linked list corresponding to the memory unit to be released includes:
[0014] When the memory unit to be released is a memory page, the fourth linked list corresponding to the second memory capacity parameter is determined according to the second memory capacity parameter of the memory page to be released;
[0015] Add the freed memory pages to the fourth linked list corresponding to the second memory capacity parameter.
[0016] Optionally, the operation request includes a memory allocation request from the first process, and the operation request in response to the first process or a second process communicating with the first process, performing operations on the independent cache space requested by the first process from the shared memory pool, includes:
[0017] In response to the memory allocation request of the first process, based on the memory capacity of the target message carried in the memory allocation request, a target memory unit corresponding to the memory capacity is allocated from the cache space.
[0018] Optionally, the cache space includes multiple types of memory units, and the allocation of a target memory unit corresponding to the memory capacity from the cache space based on the memory capacity of the target message carried in the memory allocation request includes:
[0019] Based on the memory capacity of the target message and the memory units of each type, memory units of the corresponding type are allocated from the cache space for the target message as the target memory units.
[0020] Optionally, the plurality of memory units include a first type of memory page for storing transmission messages smaller than a preset capacity threshold, and the plurality of first type memory pages are used to be divided into a plurality of memory blocks with different first preset capacity parameters.
[0021] Optionally, the step of allocating memory units of the corresponding type from the cache space for the target message based on the memory capacity of the target message and the memory units of each type, as the target memory unit, includes:
[0022] When the memory capacity is less than a preset capacity threshold, based on the memory capacity, a first target capacity parameter that matches the memory capacity is determined from multiple different first preset capacity parameters;
[0023] From the first type of memory pages corresponding to the first target capacity parameter, a target memory block is determined as the target memory unit.
[0024] Optionally, determining the target memory block from the first type of memory pages corresponding to the first target capacity parameter includes:
[0025] A first free page is determined from the first linked list corresponding to the first target capacity parameter; wherein, the first linked list is used to manage a portion of the free first type of memory pages corresponding to the first target capacity parameter;
[0026] From the plurality of memory blocks contained in the first free page, allocate a first target memory block in a free state as the target memory block.
[0027] Optionally, determining the first free page from the first linked list corresponding to the first target capacity parameter includes:
[0028] When the first linked list corresponding to the first target capacity parameter is a non-empty linked list, the step of determining the first free page from the first linked list corresponding to the first target capacity parameter is executed.
[0029] Optionally, after allocating the first target memory block in a free state, the method further includes:
[0030] Determine whether there are any free memory blocks in the first free page.
[0031] If there is no free memory block in the first free page, the first free page is removed from the first linked list.
[0032] Optionally, the method further includes:
[0033] When the first linked list corresponding to the first target capacity parameter is an empty linked list, the second free page is determined from the second linked list corresponding to the first target capacity parameter; wherein, the second linked list is used to manage all free first type memory pages corresponding to the first target capacity parameter;
[0034] From the multiple memory blocks contained in the second free page, allocate a second target memory block that is in a free state, as the target memory block.
[0035] Optionally, after allocating a second target memory block in a free state, the method further includes:
[0036] The second free page is marked as a partially free first type of memory page and added to the first linked list.
[0037] Optionally, the target message is the header of a transmission message or the body of the transmission message.
[0038] Optionally, the plurality of memory units include a second type of memory page for storing transmission messages that are greater than or equal to a preset capacity threshold, and the plurality of second type memory pages are used to store transmission messages corresponding to different second preset capacity parameters respectively.
[0039] Optionally, the step of allocating memory units of the corresponding type from the cache space for the target message based on the memory capacity of the target message and the memory units of each type, as the target memory unit, includes:
[0040] When the memory capacity is greater than or equal to the preset capacity threshold, based on the memory capacity, a second target capacity parameter matching the memory capacity is determined from multiple different second preset capacity parameters;
[0041] From the second type of memory pages corresponding to the second target capacity parameter, a target memory page is determined as the target memory unit.
[0042] Optionally, determining the target memory page from the second type of memory pages corresponding to the second target capacity parameter includes:
[0043] The third free page is determined from the third linked list corresponding to the second target capacity parameter; wherein, the third linked list is used to manage all free second type memory pages corresponding to the second target capacity parameter;
[0044] The target memory page is obtained based on the third free page.
[0045] Optionally, the third free page is determined from the third linked list corresponding to the second target capacity parameter, including:
[0046] When the third linked list corresponding to the second target capacity parameter is a non-empty linked list, the step of determining the third free page from the third linked list corresponding to the second target capacity parameter is executed.
[0047] Optionally, the method further includes:
[0048] When the third linked list corresponding to the second target capacity parameter is an empty linked list, the second type of memory pages contained in the fourth linked list corresponding to the second target capacity parameter are migrated to the third linked list; wherein, the fourth linked list is used to manage the second type of memory pages to be released corresponding to the second target capacity parameter.
[0049] Optionally, the target message includes the message body of the transmitted message.
[0050] Optionally, the method further includes:
[0051] The message list of all allocated but not released memory units of the first process is traversed. When an abnormal event is detected in the message list corresponding to an abnormal transmission message, the memory unit corresponding to the abnormal transmission message is released.
[0052] Optionally, releasing the memory unit corresponding to the abnormal transmission message includes:
[0053] Determine whether the identifier parameter in the message header of the transmitted message satisfies a first preset condition with respect to the preset identifier parameter;
[0054] When the first preset condition is met, the transmission message is determined to be an abnormal transmission message and the memory unit corresponding to the abnormal transmission message is released.
[0055] Optionally, the first preset condition includes:
[0056] When the first identifier parameter in the message header is inconsistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is a first preset value, it is determined that an abnormal event has occurred in the first process; or
[0057] If the first identifier parameter in the message header is consistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is inconsistent with the current identifier parameter of the process communicating with the first process, it is determined that an abnormal event has occurred in the process communicating with the first process.
[0058] Optionally, detecting an abnormal event in the process corresponding to an abnormally transmitted message in the message chain includes:
[0059] Determine the specified file created by the process corresponding to the abnormal transmission message, wherein the process is a first process or a process communicating with the first process;
[0060] The system detects a closing operation on the specified file and determines that an abnormal event has occurred.
[0061] Optionally, the method further includes:
[0062] Adjust the independent cache space requested by the first process from the shared memory pool.
[0063] Optionally, adjusting the independent cache space requested by the first process from the shared memory pool includes:
[0064] If the communication frequency of the first process is less than or equal to a preset frequency threshold; or if no operation of the first process continuing to request memory from the shared memory pool is detected within a preset time period, the cache space is reduced.
[0065] Optionally, adjusting the independent cache space requested by the first process from the shared memory pool includes:
[0066] If the communication frequency of the first process is greater than a preset frequency threshold, or if the operation of the first process continuing to request memory from the shared memory pool is detected within a preset time period, the cache space is increased.
[0067] According to a second aspect of this application, a computer-readable storage medium is provided that stores a computer program or instructions thereon, which, when executed by a processor, implement the steps of any of the methods described in the embodiments of this application.
[0068] According to a third aspect of this application, a computer program product is provided, including a computer program or instructions that, when executed by a processor, implement the steps of any of the methods described in the embodiments of this application.
[0069] According to a fourth aspect of this application, a controller is provided that stores a computer program or instructions thereon, which, when executed by a processor, implement the steps of any of the methods described in the embodiments of this application.
[0070] According to a fifth aspect of this application, a vehicle is provided, including a controller as described above, or performing the steps of a method as described in any of the embodiments of this application.
[0071] In summary, the embodiments of this application, through the above technical solution, by configuring a cache space for each process, can avoid frequent interactions between all processes and the shared memory pool, thus avoiding conflicts in accessing shared resources. This can improve system performance and communication throughput.
[0072] Other features and advantages of this application will be described in detail in the following detailed description section. Attached Figure Description
[0073] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0074] To gain a more complete understanding of this application and its beneficial effects, the following description will be provided in conjunction with the accompanying drawings, wherein the same reference numerals in the following description denote the same parts.
[0075] Figure 1 This is an application scenario diagram of a memory management method according to some embodiments of this application;
[0076] Figure 2 This is an exemplary flowchart of a memory management method according to some embodiments of this application;
[0077] Figure 3 This is an exemplary schematic diagram illustrating the release of memory blocks according to some embodiments of this application;
[0078] Figure 4 These are exemplary schematic diagrams illustrating the release of memory pages according to some embodiments of this application;
[0079] Figure 5 This is an exemplary schematic diagram of a cache space according to some embodiments of this application;
[0080] Figure 6 These are exemplary schematic diagrams of various types of memory cells according to some embodiments of this application;
[0081] Figure 7 This is an exemplary schematic diagram illustrating the allocation of memory blocks according to some embodiments of this application;
[0082] Figure 8 These are exemplary schematic diagrams illustrating the allocation of memory pages according to some embodiments of this application;
[0083] Figure 9 This is an exemplary schematic diagram illustrating the allocation and transmission of messages according to some embodiments of this application;
[0084] Figure 10 This is an exemplary schematic diagram illustrating abnormal monitoring events according to some embodiments of this application;
[0085] Figure 11 These are exemplary schematic diagrams illustrating the transmission of information according to some embodiments of this application;
[0086] Figure 12 This is an exemplary schematic diagram of the cache space of the recycling process according to some embodiments of this application;
[0087] Figure 13 This is an exemplary schematic diagram illustrating the adjustment of the cache space of the process according to some embodiments of this application;
[0088] Figure 14 These are exemplary schematic diagrams showing different second preset capacity parameters according to some embodiments of this application;
[0089] Figure 15This is a schematic diagram of the structure of an electronic device according to some embodiments of this application. Detailed Implementation
[0090] 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 a part of the embodiments of this application, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of this application without creative effort are within the protection scope of this application.
[0091] Currently, inter-process data communication often uses shared memory. A relatively large shared memory pool is allocated to all participating processes. When process A needs to send data to process B, process A allocates a small block of memory from the shared memory pool, writes the communication data into the newly allocated memory, and sends the memory address to process B. After process B finishes using the communication data, it releases the memory back to the shared memory pool. However, when a large number of processes frequently need to request and release memory from the shared memory pool, it can cause severe lock collisions and performance degradation. Furthermore, if a process crashes, the requested memory may not be released back to the shared memory pool, leading to memory leaks.
[0092] In view of this, this application provides a memory management method in which, when a process needs to release memory, it does not release it directly to the shared memory pool, but releases it to the cache space of the process that requested the memory. This allows the process to request memory in the corresponding cache space during the next communication, and release the memory in the cache space back to the shared memory pool when appropriate. Each process's cache space is independent, so processes do not need to compete for locks when accessing their respective cache spaces (such as allocating or releasing small objects), which helps to improve performance.
[0093] Figure 1 This is an application scenario diagram of a memory management method according to some embodiments of this application.
[0094] like Figure 1 As shown, the memory management method provided in this application embodiment can be applied to, for example, Figure 1 The application environment shown depicts a terminal communicating with a server via a network. This terminal can be, but is not limited to, various personal computers, laptops, smartphones, tablets, portable wearable devices, smart voice interaction devices, smart home appliances, in-vehicle terminals, and other vehicle-to-everything (V2X) devices.
[0095] The server can be implemented using a standalone server or a server cluster composed of multiple servers. It is understood that the server provided in this application embodiment can be a standalone physical server, a server cluster composed of multiple physical servers, or a distributed system. It can also be a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. The terminal and server can be connected directly or indirectly through wired or wireless communication methods; this application embodiment does not impose any limitations on this.
[0096] It is important to note that the application scenario diagrams of the memory management methods are provided for illustrative purposes only and are not intended to limit the scope of this specification. Those skilled in the art can make various changes and modifications based on the descriptions in this specification. For example, application scenarios may also include message libraries, information sources, etc. Furthermore, application scenarios may be implemented on other devices to achieve similar or different functions. However, these changes and modifications will not depart from the scope of this specification.
[0097] Figure 2 This is an exemplary flowchart illustrating a memory management method according to some embodiments of this application. In some embodiments, process 200 may be executed based on an electronic device. Figure 2 As shown, process 200 includes the following steps.
[0098] Step 210: Respond to the operation request of the first process or the second process communicating with the first process, and operate on the independent cache space requested by the first process from the shared memory pool.
[0099] A shared memory pool is a pre-allocated and managed area of physical memory by the operating system or a dedicated memory management module, accessible to multiple processes. The size of the shared memory pool can be dynamically adjusted according to system configuration and application requirements.
[0100] The first process refers to the process that actively initiates requests for data communication, memory allocation, and other operations during communication activities. The first process can be any application that needs to communicate with other processes.
[0101] A second process refers to another process that communicates with the first process. For example, a second process could be a process that receives, processes, or responds to transmitted messages.
[0102] An independent cache space refers to a memory management area allocated separately for each process. For example, when a first process needs to send data to a second process, it first requests a block of memory from its own independent cache space. This memory allocation operation can be performed based on the memory management strategy of the first process.
[0103] An operation request refers to a request to read, write, or perform other operations on the independent cache space of the first process. For example, an operation request includes a write operation request initiated by the first process, or a memory release request initiated by the second process after it has finished processing the data.
[0104] In some embodiments of this application, the use of an independent cache space can reduce contention for the global shared memory pool and improve the efficiency of memory operations. Based on the response to the operation request, whether it comes from the first process itself or the second process, the corresponding operation on the independent cache space is triggered, ensuring stability and resource utilization in complex communication scenarios, while avoiding memory leaks caused by unexpected process exits.
[0105] In some embodiments, the operation request includes a memory release request from the second process, and the operation request in response to the first process or the second process communicating with the first process, involving operations on the independent cache space requested by the first process from the shared memory pool, includes:
[0106] In response to the memory release request of the second process, the memory unit corresponding to the memory release request is released to the cache space of the first process that requested the memory unit.
[0107] In some embodiments, the first process can write the transmission message into the corresponding memory region in the cache space, and send the virtual address or shared memory offset of the memory region to the second process through inter-process communication, so that the second process can access the memory region allocated by the first process.
[0108] A memory release request from a second process refers to an instruction initiated by the second process to release a certain memory region.
[0109] In some embodiments, after receiving a memory release request from the second process, the first process or the memory management module parses the memory address in the memory release request and releases the memory region corresponding to that memory address.
[0110] In some embodiments of this application, by releasing the memory region back into the cache space of the first process, frequent cross-process memory release and reallocation can be avoided, significantly improving the efficiency of memory operations.
[0111] In some embodiments, responding to the memory release request of the second process and releasing the memory unit corresponding to the memory release request to the cache space of the first process that requested the memory unit includes:
[0112] Based on the identifier of the memory unit to be released carried in the memory release request, the memory unit to be released is released, and the linked list corresponding to the memory unit to be released is updated.
[0113] An identifier is information carried by a second process when initiating a release operation request, used to uniquely identify the memory unit to be released.
[0114] The identifier can take various forms. For example, it can be the virtual address, index number, or offset of the memory unit within the shared memory pool (i.e., a byte offset relative to the shared memory base address). The identifier of the memory unit to be freed can be sent to the second process along with the transfer message.
[0115] Releasing the memory unit corresponding to the identifier means performing the actual memory state change operation based on the parsed identifier. For example, changing the memory unit corresponding to the identifier from an allocated state to a free state, making it available for subsequent reuse.
[0116] It should be noted that the release operation does not immediately return the physical memory. Instead, it marks the memory unit as free and adds it to the free list of the process to which it belongs, so that it can be quickly acquired during the next allocation, thereby avoiding the expensive physical page allocation overhead.
[0117] In some embodiments of this application, the accuracy of the release operation can be ensured by using the identifier of the memory unit as the addressing basis; the release is used to change the memory state to an idle state, thereby realizing memory reuse.
[0118] In some embodiments, updating the linked list corresponding to the memory unit to be released includes:
[0119] When the memory unit to be released is a memory block, the memory page to which the memory block to be released belongs is determined according to the first memory capacity parameter of the memory block to be released;
[0120] When all memory blocks of the memory page to which the page belongs are in a free state, the memory page to which the page belongs is removed from the first linked list corresponding to the first memory capacity parameter, and the memory page to which the page belongs is added to the second linked list corresponding to the first memory capacity parameter.
[0121] A memory block to be freed refers to a memory unit allocated with a small granularity. For example, a memory block to be freed can be a memory region of no more than 2KB. For example, a memory block may be obtained by the slab allocator from larger memory pages to meet the allocation needs of a process for small objects.
[0122] The first memory capacity parameter refers to the size of the memory block to be released. The first memory capacity parameter can be a value less than 4096 bytes. For example, memory blocks can be assigned to predefined capacity slots based on the first memory capacity parameter. Each capacity slot corresponds to an independent linked list used to manage the allocation and release of memory blocks of the same size.
[0123] A memory page refers to a memory unit that can be divided into multiple memory blocks. A memory page can be divided into multiple memory blocks of the same size, the specific number of which is determined by the size of the memory blocks. For example, a memory page of 4096 bytes (4KB) can be divided into 16 memory blocks of 64 bytes each.
[0124] The first linked list refers to a linked list of partially free memory pages managed for a specific capacity slot. For example, the first linked list could be called the hdr_partial_inuse linked list. Each node in this first linked list represents a memory page of a predefined size (e.g., 4KB), in which at least one memory block is allocated and at least one memory block is free.
[0125] A second linked list is a linked list of completely free memory pages managed for a specific capacity slot. For example, a second linked list could be called a free_pages linked list. Each node in this second linked list represents a memory page of a predefined size (e.g., 4KB), where all memory blocks are in a free state.
[0126] In some embodiments, such as Figure 3 The process involves finding the corresponding slot and memory page, along with the corresponding memory block number within the memory page, based on the memory address and other information carried in the memory release request, and marking the memory block as free. Then, by traversing the metadata of all memory blocks within the memory page to which the memory block belongs, the corresponding status flags are checked. If all memory blocks are free, the memory page to which the memory block to be released belongs is removed from the first linked list (e.g., hdr_partial_inuse) corresponding to the first memory capacity parameter, and added to the second linked list (e.g., free_pages) corresponding to the first memory capacity parameter, making it a completely free page.
[0127] It should be noted that after all memory cards in a memory page managed by a certain capacity slot are released, they are first released to the second linked list. The GC module will release all memory pages in the second linked list to the shared memory pool managed by the buddy allocator at an appropriate time.
[0128] In some embodiments of this application, by updating the linked list corresponding to the memory unit to be released, completely free pages can be directly obtained from the second linked list for allocation, or the second linked list can be returned to the buddy allocator when memory is tight, thereby improving memory utilization efficiency and overall performance.
[0129] In some embodiments, updating the linked list corresponding to the memory unit to be released includes:
[0130] When the memory unit to be released is a memory page, the fourth linked list corresponding to the second memory capacity parameter is determined according to the second memory capacity parameter of the memory page to be released;
[0131] Add the freed memory pages to the fourth linked list corresponding to the second memory capacity parameter.
[0132] A memory page to be freed refers to a memory unit that is allocated and freed on a page-by-page basis. A freed memory page is the memory page obtained after updating the state of the memory page to be freed.
[0133] The second memory capacity parameter refers to the size of the memory pages to be freed.
[0134] In some embodiments, the size of a memory page is 4KB, and the second memory capacity parameter can be a value of N*4KB, such as 2MB or 1GB.
[0135] The fourth linked list refers to a linked list that manages memory pages with a specific second memory capacity parameter after they have been freed. For example, a free_pages
[4096] linked list can be set up for 4KB pages, and a free_pages_large[2097152] linked list can be set up for 2MB pages. Each node in the fourth linked list represents a complete, freed memory page.
[0136] In some embodiments, such as Figure 4 As shown, based on the size of the freed memory page, the fourth linked list matching its second memory capacity parameter can be located, and the freed memory page can be added to the corresponding fourth linked list. For example, if the size of the memory page to be freed is 4KB, the free_pages_large
[4096] linked list is searched; if it is 2MB, the free_pages_large[2097152] linked list is searched. The search can be completed directly using array indexing, or it can be implemented using data structures such as hash tables or red-black trees. The fourth linked lists corresponding to different second memory capacity parameters can be pre-configured according to the size of the memory page.
[0137] In some embodiments, the node of the freed memory page can be inserted into the head or tail of the fourth linked list, the pointers before and after can be updated to make it a valid node in the fourth linked list, and the memory page can be marked as freed for use by subsequent memory allocation requests.
[0138] In some embodiments of this application, by performing a release operation on the memory page to be released, obtaining the released memory page, and adding it to the corresponding fourth linked list, the overhead of requesting a new page can be avoided every time a large amount of memory is allocated, thereby further improving allocation efficiency.
[0139] In some embodiments, the operation request includes a memory allocation request from the first process, and the operation in response to the operation request from the first process or a second process communicating with the first process, performing operations on the independent cache space requested by the first process from the shared memory pool, includes:
[0140] In response to the memory allocation request of the first process, based on the memory capacity of the target message carried in the memory allocation request, a target memory unit corresponding to the memory capacity is allocated from the cache space.
[0141] It is understood that the first process can be any of the multiple processes, and this application embodiment does not limit this.
[0142] For example, when a first process needs to communicate with other processes (such as a second process), the first process can trigger a memory allocation request to allocate a target memory unit for itself. Then, the first process can communicate with the second process through this target memory unit. For instance, it can write a transmission message to be sent into the target memory unit, and the second process can read the transmission message based on the corresponding memory address, thus achieving inter-process communication. It should be noted that this embodiment only illustrates one scenario where the first process triggers a memory allocation request. In practical applications, the first process can also trigger memory allocation requests in other scenarios, and this embodiment does not limit this.
[0143] The target memory unit refers to the specific memory region actually allocated from the cache space of the first process based on the memory capacity of the target message. The physical address of the target memory unit is located in the shared memory pool, but the management of the target memory unit belongs to the cache space of the first process. For example, if the target message capacity is 512 bytes, a free 512-byte memory block can be allocated from the linked list corresponding to capacity slots
[512] as the target memory unit.
[0144] In some embodiments, the cache space includes multiple types of memory units, and allocating a target memory unit corresponding to the memory capacity based on the memory capacity of the target message carried in the memory allocation request from the cache space includes:
[0145] Based on the memory capacity of the target message and the memory units of each type, memory units of the corresponding type are allocated from the cache space for the target message as the target memory units.
[0146] In some embodiments, several different types of memory units are predefined in the cache space, each type corresponding to a fixed capacity parameter. For example, the multiple types of memory units may include, but are not limited to: 64 bytes, 256 bytes, 512 bytes, 1024 bytes, 2048 bytes, etc.
[0147] In some embodiments, such as Figure 5 As shown, each process's corresponding cache space is pre-configured with small object allocators and large object allocators, such as... Figure 6 As shown, the small object allocator is used to manage cache areas for memory blocks smaller than 2KB, such as 64Bytes and 128Bytes, while the large object allocator is used to manage cache areas for memory pages larger than or equal to 2KB, such as 4KBytes and 8KBytes.
[0148] The memory capacity of the target message refers to the actual number of bytes of data that the first process needs to send.
[0149] It should be noted that when allocating target memory units, if the memory capacity of the target message does not match multiple predefined fixed capacity parameters, the memory unit with the smallest capacity parameter that can accommodate the target message is selected to reduce internal fragmentation.
[0150] In some embodiments, the most suitable memory unit type can be selected from multiple available types in the cache space for allocation according to a preset matching rule. For example, iterating through all predefined first preset capacity parameters in the cache space, a first preset capacity parameter that is closest to and not less than the memory capacity is found. For instance, if the target message capacity is 320 bytes, and the available types are 256, 512, and 1024, then a 512-byte memory unit is selected. A 256-byte memory unit is insufficient to hold the target message, while 512 bytes is the smallest capacity parameter that meets the condition. This preset matching rule can be a table lookup, such as the mapping relationship between different first preset capacity parameters and capacity slots, using binary search or indexing to determine the target capacity slot.
[0151] In some embodiments of this application, memory fragmentation can be reduced by using multiple predefined different capacity parameters, thereby improving the management efficiency of cache space; when the first process releases the target memory unit, it can quickly determine its type and capacity slot based on its memory address, so as to release it to the corresponding linked list for reuse by subsequent requests.
[0152] In some embodiments, the plurality of memory units include a first type of memory page for storing transmission messages smaller than a preset capacity threshold, and the plurality of first type memory pages are used to be divided into a plurality of memory blocks with different first preset capacity parameters.
[0153] It should be noted that the first type of memory page does not directly store data between processes, but is used to divide it into multiple memory blocks of the same size.
[0154] Multiple first preset capacity parameters are a predefined set of memory block size parameters, such as 64 bytes, 256 bytes, 512 bytes, 1024 bytes, 2048 bytes, etc. Each capacity slot corresponds to a first preset capacity parameter, that is, a type of memory block. Each capacity slot is used to manage the allocation and release of memory blocks of that size parameter through its own linked list.
[0155] In some embodiments, allocating memory units of the corresponding type from the cache space for the target message based on the memory capacity of the target message and memory units of each type, as the target memory unit, includes:
[0156] When the memory capacity is less than a preset capacity threshold, based on the memory capacity, a first target capacity parameter that matches the memory capacity is determined from multiple different first preset capacity parameters;
[0157] From the first type of memory pages corresponding to the first target capacity parameter, a target memory block is determined as the target memory unit.
[0158] The preset capacity threshold is a pre-defined critical value for memory size, used to differentiate the allocation strategies for small and large objects. This preset capacity threshold can be set to 2048 bytes (2KB) and can be adjusted according to specific application scenarios. When the memory capacity of the target message is less than this preset capacity threshold, the small object allocation strategy is used, preferentially allocating from pre-divided memory blocks in the cache space; when it is equal to or greater than this preset capacity threshold, the large object allocation strategy is used, allocating one or more memory pages.
[0159] In some embodiments, determining the target memory block from the first type of memory pages corresponding to the first target capacity parameter includes:
[0160] A first free page is determined from the first linked list corresponding to the first target capacity parameter; wherein, the first linked list is used to manage a portion of the free first type of memory pages corresponding to the first target capacity parameter;
[0161] From the plurality of memory blocks contained in the first free page, allocate a first target memory block in a free state as the target memory block.
[0162] The first target capacity parameter refers to the most suitable size parameter selected from a plurality of preset first capacity parameters based on the actual size of the target message.
[0163] The first linked list refers to a linked list corresponding to a certain first preset capacity parameter, used to manage partially free first-class memory pages. For example, the first linked list can also be called the hdr_partial_inuse linked list. Partially free first-class memory pages refer to memory pages containing multiple memory blocks divided according to a certain first preset capacity parameter, wherein at least one memory block is in a free state, and at least one memory block is in an allocated state.
[0164] It is understandable that each first preset capacity parameter corresponds to an independent first linked list.
[0165] In some embodiments, determining the first free page from the first linked list corresponding to the first target capacity parameter includes:
[0166] When the first linked list corresponding to the first target capacity parameter is a non-empty linked list, the step of determining the first free page from the first linked list corresponding to the first target capacity parameter is executed.
[0167] A non-empty linked list is one that contains at least one valid node, meaning that the first linked list has at least one partially free first-class memory page available for allocation.
[0168] In some embodiments, such as Figure 7 As shown, in order to optimize memory allocation efficiency and avoid invalid operations, before accessing the first linked list corresponding to the first target capacity parameter, it is first determined whether the first linked list contains valid nodes. If it is confirmed that the first linked list is not empty, the subsequent positioning operation is performed, thereby reducing unnecessary computational overhead.
[0169] In some embodiments, the first free page can be selected from the hdr_partial_inuse linked list corresponding to the first target capacity parameter. For example, when a 1024-byte memory block needs to be allocated, the first linked list slots
[1024] .hdr_partial_inuse is searched. If the first linked list has a node, the first type of memory page corresponding to the first node is taken out as the first free page.
[0170] In some embodiments, after determining the first free page, the list of memory blocks inside the first free page is traversed to find the first memory block in a free state and its usage status is marked as allocated.
[0171] In some embodiments of this application, by allocating a first target memory block from a first free page, the allocatable resources within a partially free first type of memory page can be utilized, reducing the frequency of new memory page requests and improving memory allocation speed and overall performance.
[0172] In some embodiments, after allocating a first target memory block in a free state, the method further includes:
[0173] Determine whether there are any free memory blocks in the first free page.
[0174] If there is no free memory block in the first free page, the first free page is removed from the first linked list.
[0175] In some embodiments, after allocating a first target memory block in a free state, the state of the first target memory block is updated to an allocated state, and it is determined whether there are any free memory blocks in the first free page: if all memory blocks in the first free page are marked as allocated, then the first free page is considered fully occupied. This can be achieved through looping, or by using a free counter (free_count) for each first free page; for example, when the counter reaches 0, it indicates that there are no free memory blocks in the first free page. Figure 7 As shown, determine whether all free memory blocks in the memory page (i.e., the first free page) have been allocated. If so, remove the memory page from the first linked list.
[0176] In some embodiments of this application, after allocation is completed, it is actively checked whether there are any free memory blocks in the first free page. This can update the linked list relationship in a timely manner when resources are exhausted, ensuring the real-time performance and consistency of memory management metadata, which is beneficial for memory management in high-concurrency and high-frequency allocation scenarios.
[0177] In some embodiments, the method further includes:
[0178] When the first linked list corresponding to the first target capacity parameter is an empty linked list, the second free page is determined from the second linked list corresponding to the first target capacity parameter; wherein, the second linked list is used to manage all free first type memory pages corresponding to the first target capacity parameter;
[0179] From the multiple memory blocks contained in the second free page, allocate a second target memory block that is in a free state, as the target memory block.
[0180] A completely free first-class memory page means that all memory blocks within that first-class memory page are in a free state. For example, if a 4KB first-class memory page is divided into four 1024-byte memory blocks, and all four blocks are in a free state, then the first-class memory page is linked to the slots
[1024] .free_pages linked list.
[0181] For example, when a 1024-byte memory block needs to be allocated and the corresponding hdr_partial_inuse first linked list is empty, a 4KB first-class memory page is taken from the slots
[1024] .free_pages second linked list as the second free page.
[0182] In some embodiments, since all memory blocks in the second free page are in a free state and can be used for allocation, the first memory block of the second free page (e.g., in address order) can be selected as the second target memory block, its state is marked as allocated, and its memory address is returned to the first process.
[0183] In some embodiments of this application, when a partially free first linked list is empty, the system automatically switches to a completely free second linked list to ensure the continuity and reliability of memory allocation.
[0184] In some embodiments, after allocating a second target memory block in a free state, the method further includes:
[0185] The second free page is marked as a partially free first type of memory page and added to the first linked list.
[0186] In some embodiments, after allocating a second target memory block in a free state, the metadata of the second free page can be updated to change its status from fully free to partially free. For example, this can be achieved by modifying the status field in the second free page.
[0187] In some embodiments of this application, if the first linked list is not updated after allocation, subsequent memory allocation requests of the same size will be unable to allocate the remaining free memory blocks in the page, which may lead to unnecessary requests for new memory pages, increasing memory fragmentation and system overhead. By adding them to the first linked list in a timely manner, the unified management and allocation of all partially free first-class memory pages are ensured, thereby improving resource reuse and allocation efficiency.
[0188] In some embodiments, the target message is the header of a transmission message or the body of the transmission message.
[0189] In actual communication processes, the first process usually needs to process the message header and message body separately. For message headers and message bodies with memory capacity less than a preset capacity threshold, the first process can allocate a corresponding memory block from multiple first-class memory pages to write the message header, and allocate another corresponding memory block to write the message body. The addresses of these two memory units can be sent to the second process as metadata.
[0190] In some embodiments, the plurality of memory units include a second type of memory page for storing transmission messages that are greater than or equal to a preset capacity threshold, and the plurality of second type memory pages are used to store transmission messages corresponding to different second preset capacity parameters respectively.
[0191] Different second preset capacity parameters are a predefined set of memory page block size parameters. Multiple different second preset capacity parameters may include, but are not limited to, 4KB, 8KB, 16KB, 32KB, 64KB, 128KB, 256KB, 512KB, 1MB, 2MB, and 4MB.
[0192] In some embodiments, allocating memory units of the corresponding type from the cache space for the target message based on the memory capacity of the target message and memory units of each type, as the target memory unit, includes:
[0193] When the memory capacity is greater than or equal to the preset capacity threshold, based on the memory capacity, a second target capacity parameter matching the memory capacity is determined from multiple different second preset capacity parameters;
[0194] From the second type of memory pages corresponding to the second target capacity parameter, a target memory page is determined as the target memory unit.
[0195] The second target capacity parameter refers to the most suitable size parameter selected from a variety of preset second capacity parameters based on the actual size of the target message.
[0196] In some embodiments, the second type of memory page with the most matching second preset capacity parameter can be selected according to the memory capacity of the target message. For example, if the first process needs to send a target message of size 70KB, the second preset capacity parameter that is closest to and not less than 70KB, i.e., 128KB, is selected. Then, an obj_pool_ele instance is obtained from data_pools[5] (128KB corresponds to index 6), and a 128KB second type of memory page is allocated from it.
[0197] The second preset capacity parameter for the cache space of each process includes 11 size parameters, namely 4K, 8K, 16K...4M, as shown in the data_pools array below. For example, data[0] is the cache area with the 4K size parameter. All memory managed by the large object allocator is managed by two linked lists, obj_pool_ele.cache and obj_pool_ele.free, as shown below. Figure 14 As shown.
[0198] Message transmission refers to the data content transferred between the first process and the second process via shared memory, including but not limited to control commands, status data, logs, configuration information, sensor data, or file content. When the memory capacity of the transmitted message exceeds a preset capacity threshold (e.g., 2KB), the allocation strategy switches to the second type of memory page. Based on the memory capacity, the smallest capacity parameter that can accommodate the target message is selected first to reduce memory waste.
[0199] like Figure 11 As shown, taking process A sending a message to process B as an example, `src_addr` stores relevant information about process A, `dst_addr` stores relevant information about process B, and `data_info` stores the actual data that process A wants to send to process B. After process B receives the data and finishes using it, it can release the memory to process A's cache space based on the information in `src_addr`.
[0200] In some embodiments, the allocation of the second type of memory pages is managed by an `obj_pool_ele` structure. Each `obj_pool_ele` instance corresponds to a second preset capacity parameter. For example, all second type memory pages of this size parameter are managed through two linked lists, `cache` and `free`, corresponding to the `obj_pool_ele` instance. The `free` linked list links currently free second type memory pages; the `cache` linked list links second type memory pages to be released.
[0201] The ptcp_id value is fixed and unique for each participant in the communication. That is, the ptcp_id value of process A remains unchanged after repeated restarts. Information such as the cache space of process A can be associated with the ptcp_id. The ns_id value of each participant is incremented by 1 upon termination. That is, when process A starts for the first time, its ns_id is equal to 1; after its first termination, its ns_id value becomes 2.
[0202] When the first process initiates a memory allocation request, it determines the second target preset capacity parameter based on the size of the memory capacity, locates the corresponding item in the data_pools array, and retrieves a second type of memory page as the target memory unit through the free linked list of its corresponding obj_pool_ele.
[0203] In some embodiments, determining the target memory page from the second type of memory pages corresponding to the second target capacity parameter includes:
[0204] The third free page is determined from the third linked list corresponding to the second target capacity parameter; wherein, the third linked list is used to manage all free second type memory pages corresponding to the second target capacity parameter;
[0205] The target memory page is obtained based on the third free page.
[0206] The third linked list refers to a linked list corresponding to a certain second preset capacity parameter, used to manage the second type of memory pages in a free state. Each second preset capacity parameter corresponds to an independent third linked list. Multiple third linked lists corresponding to second preset capacity parameters can be organized by the data_pools array. For example, data_pools[0] corresponds to a 4KB third linked list, data_pools[1] corresponds to an 8KB third linked list, and so on, with data_pools
[10] corresponding to a 4MB third linked list. Each node in the third linked list represents an unallocated second type of memory page, the size of which is equal to the corresponding second preset capacity parameter.
[0207] In some embodiments, determining a third free page from a third linked list corresponding to the second target capacity parameter includes:
[0208] When the third linked list corresponding to the second target capacity parameter is a non-empty linked list, the step of determining the third free page from the third linked list corresponding to the second target capacity parameter is executed.
[0209] In some embodiments, such as Figure 8 As shown, in order to optimize memory allocation efficiency and avoid invalid operations, before accessing the third linked list corresponding to the second target capacity parameter, it is first determined whether the third linked list contains valid nodes. If the third linked list is confirmed to be non-empty, the subsequent positioning operation is performed, thereby reducing unnecessary computational overhead.
[0210] In some embodiments, the identified third free page can be used as the final target memory unit for this allocation, and the status of the target memory page can be updated to "allocated". The third free page can be removed from the third linked list and returned to the first process based on the memory address of the third free page for writing the target message.
[0211] In some embodiments of this application, large-sized communication messages can be managed through multiple different second preset capacity parameters, avoiding frequent allocation or internal fragmentation problems caused by fixed 4KB pages. Furthermore, the linked list management of obj_pool_ele ensures efficient allocation and release, contributing to high-performance, low-latency inter-process communication.
[0212] In some embodiments, the method further includes:
[0213] When the third linked list corresponding to the second target capacity parameter is an empty linked list, the second type of memory pages contained in the fourth linked list corresponding to the second target capacity parameter are migrated to the third linked list; wherein, the fourth linked list is used to manage the second type of memory pages to be released corresponding to the second target capacity parameter.
[0214] The fourth linked list refers to a linked list corresponding to a certain second preset capacity parameter, used to manage the second type of memory pages that have been released but not yet allocated.
[0215] In some embodiments, when the third linked list corresponding to the second target capacity parameter is an empty linked list, the state of the second type of memory pages contained in the fourth linked list is updated to an idle state and migrated to the third linked list. For example... Figure 8 As shown, when the third linked list corresponding to the second target capacity parameter is an empty linked list, the data of the fourth linked list is moved to the third linked list.
[0216] In some embodiments of this application, a fourth linked list is used as a release buffer to receive the returned second-type memory pages from the second process, avoiding competition between release and allocation operations; while the third linked list provides readily available resources. When the third linked list is exhausted, the second-type memory pages of the fourth linked list are migrated to replenish them. This can effectively separate the timing of release and allocation, reduce lock conflicts, improve concurrency performance, and improve overall efficiency.
[0217] In some embodiments, the target message includes a message body that transmits the message.
[0218] In some embodiments, for a message body whose memory capacity is greater than or equal to a preset capacity threshold, the first process can allocate a corresponding memory page from a plurality of second-type memory pages for writing.
[0219] like Figure 9 As shown, for the first process that needs to send a transmission message, the memory capacity of the message header is small. The message header of the transmission message can be allocated in the small object allocator. It is also determined whether the memory capacity of the message body of the transmission message is greater than the preset capacity threshold. If so, it is allocated in the large object allocator; otherwise, it is allocated in the small object allocator.
[0220] For example, assigning a transport message includes:
[0221] The dds_hdr_t message header is allocated. Since dds_hdr_t is less than 2K, it will be allocated from the cache area of the memory card of the corresponding size in the small object allocator. For example, if the size of dds_hdr_t is 64 bytes, it will be allocated from the 64-byte cache area.
[0222] When allocating a message body, if the memory capacity of the message body is less than 2KB, it will be allocated from the cache area of the memory card of the corresponding size in the small object allocator. Otherwise, it will be allocated from the large object allocator of the corresponding size.
[0223] In some embodiments, the method further includes:
[0224] The message list of all allocated but not released memory units of the first process is traversed. When an abnormal event is detected in the message list corresponding to a transmission message, the memory unit corresponding to the abnormal transmission message is released.
[0225] In some embodiments, abnormal events include, but are not limited to, process crashes, process exits, process suspension timeouts, signal interruptions, or heartbeat loss.
[0226] A message list is a linked list used to record the transmitted messages in the memory units currently allocated by the first process. For example, each node in the message list corresponds to a transmitted message that has not yet been completed. Each node can contain metadata such as the memory address, size, preset capacity parameter, sending time, process ID (PID), and message status of the transmitted message.
[0227] In some embodiments, the message chain can be traversed starting from the head of the chain, and the transmission messages associated with each node in the chain can be checked one by one to determine whether the corresponding process is in a normal state.
[0228] In some embodiments of this application, the transmission messages of all unreleased memory units are monitored by traversing the message chain to ensure no omissions; and the memory units of abnormal transmission messages are reclaimed when the process is abnormal, so as to avoid the shared memory pool being occupied for a long time, improve stability and resource utilization, and thus avoid the problem of resource exhaustion caused by process crash.
[0229] In some embodiments, releasing the memory unit corresponding to the abnormal transmission message includes:
[0230] Determine whether the identifier parameter in the message header of the transmitted message satisfies a first preset condition with respect to the preset identifier parameter;
[0231] When the first preset condition is met, the transmission message is determined to be an abnormal transmission message and the memory unit corresponding to the abnormal transmission message is released.
[0232] An abnormal transmission message refers to a transmission message that has been allocated but not yet released in the message chain of the first process, and whose corresponding process is detected to have an abnormal time (such as crash or exit).
[0233] The identifier parameter is used to represent the lifecycle identifier of the process corresponding to the transmitted message.
[0234] The first preset condition is a criterion used to determine whether the process corresponding to the transmitted message has any abnormal events.
[0235] In some embodiments, the first preset condition includes:
[0236] When the first identifier parameter in the message header is inconsistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is a first preset value, it is determined that an abnormal event has occurred in the first process; or
[0237] If the first identifier parameter in the message header is consistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is inconsistent with the current identifier parameter of the process communicating with the first process, it is determined that an abnormal event has occurred in the process communicating with the first process.
[0238] The first identifier parameter refers to the lifecycle identifier recorded by the first process when sending the transmission message. In some embodiments, the first identifier parameter may be the src_addr.ns_id field in the message header of the transmission message.
[0239] The second identifier parameter refers to the lifecycle identifier written by the process communicating with the first process (hereinafter referred to as process B) after successfully receiving the transmission message. In some embodiments, the second identifier parameter may be the dst_addr.ns_id field in the message header of the transmission message.
[0240] The current identifier parameter of the first process refers to the lifecycle identifier of the first process currently being maintained.
[0241] The current identifier parameter of the process communicating with the first process refers to the lifecycle identifier of the process currently being maintained and communicating with the first process.
[0242] The first identifier parameter in the message header is initialized to its current lifecycle identifier (hereinafter referred to as ns_id) when the first process requests the message header. For example, if the current lifecycle identifier of the first process A is 100, then the first identifier parameter in the message header of all transmitted messages will be set to 100. The first identifier parameter is used to identify the initiator of the transmitted message and its lifecycle version for subsequent state comparison. The first identifier parameter can be obtained by directly reading the src_addr.ns_id field in the structure of the transmitted message header.
[0243] The current identifier parameter of the first process can be managed by a process monitoring service (e.g., Process Monitor). A new lifecycle identifier value is assigned each time the first process starts or restarts; for example, the new lifecycle identifier value is the previous lifecycle identifier value plus 1. For instance, if the first process A has an identifier parameter of ns_id of 100 when it first runs, and its current identifier parameter is 101 after a crash and restart, this current identifier parameter reflects the current lifecycle state of the first process. The current identifier parameter of the first process can be obtained by querying the process status table maintained by the process monitoring service and looking up the corresponding ns_id based on the first process ID.
[0244] The second identifier parameter in the message header is initialized to a first preset value when the message is sent, indicating that the message has not been received. When the process communicating with the first process successfully receives the message, it updates the identifier to its current lifecycle identifier as a "receive acknowledgment" signal. For example, if process B communicating with the first process has a current lifecycle identifier of 200, it will set the second identifier parameter to 200 after receiving the message. This second identifier parameter indicates whether the message has been received by the process communicating with the first process. The second identifier parameter is obtained by reading the `dst_addr.ns_id` field in the message header.
[0245] The first preset value is a specific numerical value used to indicate "not received" or "initial state". For example, the first preset value can be 0. The first preset value can be written to the dst_addr.ns_id field during message header initialization as the initial state for receiving confirmation. It should be noted that the first preset value can be defined by the system protocol.
[0246] Similar to the current identifier parameter of the first process, the value of this current lifecycle identifier can be dynamically maintained by the process monitoring service, reflecting the current lifecycle state of the process communicating with the first process. For example, process B has an ns_id of 200 when it is running, and it is updated to 201 when it is restarted after it dies. The current identifier parameter of process B is used to compare with the second identifier parameter in the message header to determine whether process B has died. The current identifier parameter of the second process is obtained by querying the process status table of the process monitoring service and looking up its ns_id based on the process ID communicating with the first process.
[0247] In some embodiments, the first preset condition includes: the first identifier parameter in the message header is inconsistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is a first preset value, used to detect a scenario where the first process has died and the message has not been delivered. For example, the current ns_id of the first process A is 101, while the src_addr.ns_id in the message header is 100, which are inconsistent; at the same time, dst_addr.ns_id is 0, indicating that the second process B has not received the message. In this case, it is determined that the transmission message has failed, and a release operation can be performed.
[0248] In some embodiments, the first preset condition may further include: the first identifier parameter in the message header is consistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is inconsistent with the current identifier parameter of the process communicating with the first process, used to detect a scenario where the receiver has died and the message has been delivered. For example, the current ns_id of the first process A is 100, which is consistent with src_addr.ns_id in the message header, indicating that the first process A is still alive; however, the dst_addr.ns_id in the message header is 200, while the current ns_id of process B is 201, which is inconsistent, indicating that process B communicating with the first process has died. In this case, the transmission message is determined to be expired, and a release operation can be performed.
[0249] In some embodiments, during the traversal of the message list, the first preset condition described above is checked for each transmitted message header. If any condition is met, the memory unit is released.
[0250] In some embodiments, the memory units corresponding to the abnormal transmission messages can be released based on a preset reclamation strategy. The preset reclamation strategy is a set of pre-configured rules used to release the memory units occupied by the abnormal transmission messages. The preset reclamation strategy may include: determining a first preset capacity parameter or a second preset capacity parameter based on the size of the abnormal transmission message, in order to release the corresponding memory units. For more information on the release operation, please refer to the relevant description above.
[0251] For example, for a transmission message belonging to the first preset capacity parameter in the message chain, if the first preset condition is met, the message is removed from the message chain corresponding to the transmission message, and its memory address is used to reverse locate the first type of memory page and the corresponding first preset capacity parameter, and then it is returned to the corresponding first linked list. Since the management granularity of small memory blocks is finer, when all memory blocks of a certain first type of memory page are in a free state, the first type of memory page can be moved from the first linked list to the second linked list.
[0252] In some embodiments, for a transmission message belonging to the first preset capacity parameter in the message chain, if the first preset condition is met, the corresponding message chain is removed from the message chain and added to the fourth chain corresponding to the second preset capacity parameter for subsequent allocation and reuse.
[0253] For example, process A sends a transmission message to process B. If the transmission message hasn't been sent to process B (i.e., process B hasn't received the message), process A dies. To allow the garbage collection service to filter out this type of transmission data from process A's cache message list for reclamation, after acquiring the `dds_hsr_t` message header, process A initializes `dst_addr.ns_id` to 0 and initializes `src_addr.ns_id` to process A's `ns_id`. When process B receives the message, it assigns `dst_addr.ns_id` to process B's `ns_id`. After the process monitor service detects process A's death, it increments process A's `ns_id` by 1. When the garbage collection service attempts to reclaim the memory of process A's message list, it checks if `src_addr.ns_id` equals process A's `ns_id` and if `dst_addr.ns_id` is 0. If so, it reclaims the memory corresponding to that transmission message (based on the message header, both the message header and message data can be reclaimed simultaneously).
[0254] If process A sends a message to process B, and process B receives it but dies abnormally before finishing using the message (i.e., B hasn't called the release interface), process A, when sending the message to process B, assigns the `dst_addr.ptcp_id` property of `dds_hdr_t` to process B's `ptcp_id`. When process B receives the message, it assigns the `dst_addr.ns_id` property to process B's `ns_id`. When the process monitor service detects that B has died, it increments B's `ns_id` value by 1. Then, when the GC service attempts to reclaim the memory of process A's message list, it checks whether `dst_addr.ns_id` of `dds_hdr_t` is equal to process B's `ns_id`. If they are not equal, it means that process B has died, and the memory corresponding to that message transmission is reclaimed.
[0255] In some embodiments, the method further includes:
[0256] When an abnormal event occurs in the first process, release the memory units corresponding to all transmitted messages in the message chain.
[0257] In some embodiments, detecting an abnormal event in the process corresponding to a transmitted message in the message chain includes:
[0258] Determine the specified file created by the process corresponding to the abnormal transmission message, wherein the process is a first process or a process communicating with the first process;
[0259] The system detects a closing operation on the specified file and determines that an abnormal event has occurred.
[0260] For example, such as Figure 10 As shown, after a process starts, it creates a file named ptcp_name in a specified directory (e.g., / tmp / bosdds / directory, etc.). Each process has a unique ptcp_name, which remains unchanged. Similar to ptcp_id, ptcp_name is a string, while ptcp_id is a uint8_t type. The kernel locks this file, and the file system can notify user space of the IN_CREATE event through the inotify mechanism. When a process dies, the kernel automatically releases the file handle held by the process, and the file system can also notify user space of the file's closure operation through the inotify mechanism. The process monitor listens for the IN_CREATE event in the specified directory (e.g., / tmp / bosdds / directory) and the IN_CLOSE event for all files in the specified directory (e.g., / tmp / bosdds / directory, etc.). It dynamically adds process objects to be monitored based on the IN_CREATE event and detects process death through the IN_CLOSE event.
[0261] In some embodiments, the method further includes:
[0262] Adjust the independent cache space requested by the first process from the shared memory pool.
[0263] In some embodiments, to adapt to dynamically changing communication load and resource usage requirements, the independent cache space requested by the first process from the shared memory pool is dynamically adjusted to expand or shrink the capacity of the first process's cache space.
[0264] For example, such as Figure 12As shown, when a process has not communicated for an extended period, its cache space is reclaimed. This includes: checking if any processes have not been active for a long time, for example, if the inactivity period is greater than or equal to a preset value; if so, releasing the process's cache space, for example, by traversing the process's message list and releasing the memory resources that need to be freed. After releasing the memory in the message list, all first and second linked lists are traversed, and the memory resources in these lists are released to the shared memory pool. Furthermore, all third and fourth linked lists are traversed, and the memory resources in these lists are similarly released to the shared memory pool.
[0265] For example, such as Figure 13 As shown, adjusting the cache space includes:
[0266] Initialize cache space capacity parameters. For example, set the cache space capacity parameters to initial values during system initialization or cache subsystem startup.
[0267] Determine if a threshold adjustment event has occurred. Threshold adjustment events may include, but are not limited to: changes in system load, memory usage exceeding a preset threshold, and user-triggered adjustment requests.
[0268] If a threshold adjustment event occurs, determine whether a memory reclamation event exists. Memory reclamation events may include: insufficient system memory requiring the release of some cache for other processes, expired or infrequently accessed data in the cache, which can be reclaimed, etc.
[0269] If a memory reclamation event occurs, determine whether memory was requested from the shared memory pool within a certain period of time. If memory was requested from the shared memory pool within a certain period of time, increase the value of the cache space capacity parameter appropriately based on the number of times memory was requested from the shared memory pool during this period. The demand for cache is high during this period, so the cache space needs to be increased to meet the demand.
[0270] If no threshold adjustment event occurs, or if a threshold adjustment event occurs but no memory reclamation event occurs, or if a memory reclamation event occurs but no memory is requested from the shared memory pool for a period of time, appropriately reducing the value of the cache space capacity parameter can help release unnecessary cache resources and improve the overall performance and resource utilization of the system.
[0271] In some embodiments of this application, elastic management of the cache space can be achieved by adjusting the size of the cache space: for example, the latency caused by frequent access to the global pool can be avoided by expanding the size; and the shared memory utilization rate can be improved by shrinking the size to release resources for other processes to use.
[0272] In some embodiments, adjusting the independent cache space requested by the first process from the shared memory pool includes:
[0273] If the communication frequency of the first process is less than or equal to a preset frequency threshold; or if no operation of the first process continuing to request memory from the shared memory pool is detected within a preset time period, the cache space is reduced.
[0274] In some embodiments, adjusting the independent cache space requested by the first process from the shared memory pool includes:
[0275] If the communication frequency of the first process is greater than a preset frequency threshold, or if the operation of the first process continuing to request memory from the shared memory pool is detected within a preset time period, the cache space is increased.
[0276] The communication frequency of the first process refers to the number of communication operations initiated by that process per unit of time.
[0277] The preset frequency threshold refers to a pre-configured lower limit for the communication frequency, used to determine whether the first process is in a low-load state. This preset frequency threshold can be flexibly configured according to the application scenario. When the communication frequency of the first process is less than or equal to the preset frequency threshold, it indicates that the first process may enter an idle or standby state, reducing the demand for cache space.
[0278] The preset duration refers to a pre-defined continuous time window used to detect whether the first process has not performed a memory allocation operation for an extended period. This duration is typically set to several seconds to tens of seconds, such as 30 seconds or 60 seconds. If no further memory allocation operation from the shared memory pool is detected within the preset duration, it indicates that the allocated memory of the first process may be in an idle or reclaimable state.
[0279] It should be noted that the above description of the process is for illustrative purposes only and does not limit the scope of this specification. Those skilled in the art can make various modifications and changes to the process under the guidance of this specification. However, these modifications and changes remain within the scope of this specification.
[0280] For details on the implementation of each of the above operations, please refer to the previous examples, which will not be repeated here.
[0281] Figure 15 This is a schematic diagram of the structure of an electronic device according to some embodiments of this application. For example... Figure 15 As shown, the electronic device 1500 may include a processor 1501 and a memory 1502. The electronic device 1500 may also include one or more of a multimedia component 1503, an input / output (I / O) component 1504, and a communication component 1505. In this embodiment, the electronic device 1500 may be a device that implements the memory management method provided in this embodiment.
[0282] The processor 1501 controls the overall operation of the electronic device 1500 to complete all or part of the steps in the memory management method described above. The memory 1502 stores various types of messages to support the operation of the electronic device 1500. These messages may include, for example, instructions for any application or method operating on the electronic device 1500, as well as application-related messages such as images, audio, video, etc. The memory 1502 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read Only Memory (EEPROM), Erasable Programmable Read Only Memory (EPROM), Programmable Read Only Memory (PROM), Read Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The multimedia component 1503 may include a screen and audio components. The screen may be, for example, a touchscreen, and the audio component is used to output and / or input audio signals. For example, the audio component may include a microphone for receiving external audio signals. The received audio signals may be further stored in memory 1502 or transmitted via communication component 1505. The audio component also includes at least one speaker for outputting audio signals. I / O component 1504 provides an interface between processor 1501 and other interface modules, such as a keyboard, mouse, buttons, etc. These buttons may be virtual or physical buttons. Communication component 1505 is used for wired or wireless communication between the electronic device 1500 and other devices. Wireless communication includes, for example, Wi-Fi, Bluetooth, Near Field Communication (NFC), 2G, 3G, 4G, Narrow Band Internet of Things (NB-IoT), Enhanced Machine Type Communication (eMTC), or other 5G technologies, or combinations thereof, without limitation. Therefore, the corresponding communication component 1505 may include: a Wi-Fi module, a Bluetooth module, an NFC module, etc.
[0283] In an exemplary embodiment, the electronic device 1500 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the memory management method described above.
[0284] In another exemplary embodiment, a computer-readable storage medium is also provided, on which program instructions are stored, which, when executed by a processor, implement the steps of the memory management method described above. For example, the computer-readable storage medium may be the memory 1502 containing the program instructions described above, which may be executed by the processor 1501 of the electronic device 1500 to implement or execute the methods, steps, and logic diagrams disclosed in the embodiments of this application;
[0285] Alternatively, when the instructions are executed by a computer, they may be used to implement or execute the methods, steps, and logic diagrams disclosed in the embodiments of this application.
[0286] In another exemplary embodiment, a computer program product is also provided, including a computer program or instructions that, when executed by a processor, implement the steps of the memory management method described above. For example, the computer program product may be the memory 1502 including the computer program described above, and the computer program may be executed by the processor 1501 of the electronic device 1500 to implement or execute the methods, steps, and logic diagrams disclosed in the embodiments of this application.
[0287] Alternatively, when the instructions are executed by a computer, they may be used to implement or execute the methods, steps, and logic diagrams disclosed in the embodiments of this application.
[0288] This application also provides a vehicle equipped with the electronic device provided in any of the above embodiments. The electronic device is used to execute the memory management method provided in any of the above embodiments. The vehicle may be a gasoline-powered vehicle, a plug-in hybrid electric vehicle, or a new energy vehicle, etc., and this specification does not specifically limit it.
[0289] In the description of this application, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more features. In the description of this application, "multiple" means two or more, unless otherwise explicitly specified.
[0290] The embodiments, implementation methods, and related technical features of this application can be combined and substituted for each other without conflict.
[0291] The above are merely preferred embodiments of this application and are not intended to limit this application in any way. Although the descriptions of each embodiment in this application have different focuses, and the parts not described in detail in a certain embodiment can be referred to the relevant embodiments of other embodiments, any simple modifications, equivalent changes and modifications made to the above embodiments based on the technical essence of this application without departing from the content of the technical solution of this application shall still fall within the scope of the technical solution of this application.
Claims
1. A memory management method characterized by comprising: The method comprises: In response to an operation request of a first process or a second process in communication with the first process, operating on independent cache space of the first process applied from a shared memory pool.
2. The method of claim 1, wherein, The operation request comprises a memory release request of the second process, and the operation on the independent cache space of the first process applied from the shared memory pool in response to the operation request of the first process or the second process in communication with the first process comprises: In response to the memory release request of the second process, releasing a memory unit corresponding to the memory release request to the cache space of the first process applying the memory unit.
3. The method of claim 2, wherein, The operation on the independent cache space of the first process applied from the shared memory pool in response to the operation request of the first process or the second process in communication with the first process comprises: According to an identifier of a memory unit to be released carried in the memory release request, releasing the memory unit to be released, and updating a linked list corresponding to the memory unit to be released.
4. The method of claim 3, wherein, The updating of the linked list corresponding to the memory unit to be released comprises: When the memory unit to be released is a memory block, determining a memory page to which the memory block to be released belongs according to a first memory capacity parameter of the memory block to be released; When all memory blocks of the memory page belong to an idle state, removing the memory page from a first linked list corresponding to the first memory capacity parameter, and adding the memory page to a second linked list corresponding to the first memory capacity parameter.
5. The method of claim 3, wherein, The updating of the linked list corresponding to the memory unit to be released comprises: When the memory unit to be released is a memory page, determining a fourth linked list corresponding to a second memory capacity parameter of the memory page to be released according to the second memory capacity parameter; Adding the released memory page to the fourth linked list corresponding to the second memory capacity parameter.
6. The method of claim 1, wherein, The operation request comprises a memory allocation request of the first process, and the operation on the independent cache space of the first process applied from the shared memory pool in response to the operation request of the first process or the second process in communication with the first process comprises: In response to the memory allocation request of the first process, allocating a target memory unit corresponding to a memory capacity of a target message carried in the memory allocation request from the cache space.
7. The method of claim 6, wherein, The cache space comprises a plurality of types of memory units, and the allocation of the target memory unit corresponding to the memory capacity of the target message from the cache space comprises: Based on the memory capacity of the target message and each type of memory unit, allocating a corresponding type of memory unit for the target message from the cache space as the target memory unit.
8. The method of claim 7, wherein, The plurality of types of memory units comprise a first type of memory page for storing a transmission message smaller than a preset capacity threshold, and a plurality of first type of memory pages are used to be segmented into a plurality of memory blocks with different first preset capacity parameters.
9. The method of claim 8, wherein, allocating, as the target memory unit, a memory unit of a corresponding type for the target message from the cache space based on a memory capacity of the target message and each of the types of memory units, comprises: when the memory capacity is less than a preset capacity threshold, determining, based on the memory capacity, a first target capacity parameter that matches the memory capacity from a plurality of different first preset capacity parameters; determining, as the target memory unit, a target memory block from a first type of memory page corresponding to the first target capacity parameter.
10. The method of claim 9, wherein, The step of determining, as the target memory unit, a target memory block from a first type of memory page corresponding to the first target capacity parameter, comprises: determining, from a first linked list corresponding to the first target capacity parameter, a first free page; wherein the first linked list is used to manage a part of free first type of memory pages corresponding to the first target capacity parameter; allocating, as the target memory block, a first target memory block in an idle state from a plurality of memory blocks included in the first free page.
11. The method of claim 10, wherein, The step of determining, from a first linked list corresponding to the first target capacity parameter, a first free page, comprises: when the first linked list corresponding to the first target capacity parameter is a non-empty linked list, performing the step of determining, from the first linked list corresponding to the first target capacity parameter, a first free page.
12. The method of claim 10, wherein, After allocating the first target memory block in an idle state, the method further comprises: determining whether the first free page still has a memory block in an idle state, when the first free page does not have a memory block in an idle state, removing the first free page from the first linked list.
13. The method of claim 10, wherein, The method further comprises: when the first linked list corresponding to the first target capacity parameter is an empty linked list, determining, from a second linked list corresponding to the first target capacity parameter, a second free page; wherein the second linked list is used to manage all free first type of memory pages corresponding to the first target capacity parameter; allocating, as the target memory block, a second target memory block in an idle state from a plurality of memory blocks included in the second free page.
14. The method of claim 13, wherein, After allocating the second target memory block in an idle state, the method further comprises: marking the second free page as a part of free first type of memory page and adding the second free page to the first linked list.
15. The method of claim 9, wherein, The target message is a message header of a transmission message or a message body of the transmission message.
16. The method of claim 7, wherein, The plurality of types of memory units include a second type of memory page used to store a transmission message greater than or equal to a preset capacity threshold, and a plurality of second type of memory pages are used to respectively store transmission messages corresponding to different second preset capacity parameters.
17. The method of claim 16, wherein, allocating, as the target memory unit, a memory unit of a corresponding type for the target message from the cache space based on a memory capacity of the target message and each of the types of memory units, comprises: when the memory capacity is greater than or equal to the preset capacity threshold, determining, based on the memory capacity, a second target capacity parameter that matches the memory capacity from a plurality of different second preset capacity parameters; determining, as the target memory unit, a target memory page from a second type of memory page corresponding to the second target capacity parameter.
18. The method of claim 17, wherein, Determining the target memory page from the second type of memory pages corresponding to the second target capacity parameter includes: The third free page is determined from the third linked list corresponding to the second target capacity parameter; wherein, the third linked list is used to manage all free second type memory pages corresponding to the second target capacity parameter; The target memory page is obtained based on the third free page.
19. The method of claim 18, wherein, The third free page is determined from the third linked list corresponding to the second target capacity parameter, including: When the third linked list corresponding to the second target capacity parameter is a non-empty linked list, the step of determining the third free page from the third linked list corresponding to the second target capacity parameter is executed.
20. The method of claim 19, wherein, The method further includes: When the third linked list corresponding to the second target capacity parameter is an empty linked list, the second type of memory pages contained in the fourth linked list corresponding to the second target capacity parameter are migrated to the third linked list; wherein, the fourth linked list is used to manage the second type of memory pages to be released corresponding to the second target capacity parameter.
21. The method of claim 17, wherein, The target message includes the message body that transmits the message.
22. The method of any one of claims 1-21, wherein, The method further includes: The message list of all allocated but not released memory units of the first process is traversed. When an abnormal event is detected in the message list corresponding to an abnormal transmission message, the memory unit corresponding to the abnormal transmission message is released.
23. The method of claim 22, wherein, The process of releasing the memory unit corresponding to the abnormal transmission message includes: Determine whether the identifier parameter in the message header of the transmitted message satisfies a first preset condition with respect to the preset identifier parameter; When the first preset condition is met, the transmission message is determined to be an abnormal transmission message and the memory unit corresponding to the abnormal transmission message is released.
24. The method of claim 23, wherein, The first preset conditions include: When the first identifier parameter in the message header is inconsistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is a first preset value, it is determined that an abnormal event has occurred in the first process; or If the first identifier parameter in the message header is consistent with the current identifier parameter of the first process, and the second identifier parameter in the message header is inconsistent with the current identifier parameter of the process communicating with the first process, it is determined that an abnormal event has occurred in the process communicating with the first process.
25. The method of claim 22, wherein, The detection of an abnormal transmission message in the message chain corresponding to a process experiencing an abnormal event includes: Determine the specified file created by the process corresponding to the abnormal transmission message, wherein the process is a first process or a process communicating with the first process; The system detects a closing operation on the specified file and determines that an abnormal event has occurred.
26. The method of claim 1, wherein, The method further includes: Adjust the independent cache space requested by the first process from the shared memory pool.
27. The method of claim 26, wherein, The adjustment of the independent cache space requested by the first process from the shared memory pool includes: If the communication frequency of the first process is less than or equal to a preset frequency threshold; or if no operation of the first process continuing to request memory from the shared memory pool is detected within a preset time period, the cache space is reduced.
28. The method of claim 26, wherein, The adjustment of the independent cache space requested by the first process from the shared memory pool includes: If the communication frequency of the first process is greater than a preset frequency threshold, or if an operation of the first process continuing to apply for memory from the shared memory pool is detected within a preset time length, the cache space is increased.
29. A computer readable storage medium having stored thereon a computer program or instructions, characterized in that, The computer program or instructions, when executed by a processor, implement the steps of the method of any one of claims 1 to 28.
30. A computer program product, characterised in that, A computer program or instructions, when executed by a processor, implement the steps of the method of any one of claims 1 to 28.
31. A controller having stored thereon computer programs or instructions, characterized in that, The computer program or instructions, when executed by a processor, implement the steps of the method of any one of claims 1 to 28.
32. A vehicle characterized by A controller as claimed in claim 31, or executing the steps of the method of any one of claims 1 to 28.