Memory allocation method, electronic device, and vehicle
By dynamically assigning memory allocation strategies to threads in electronic devices and vehicles, the problem of existing memory allocation algorithms failing to match business needs is resolved, improving system performance and stability, and in particular, improving real-time response capabilities in autonomous driving systems.
Patent Information
- Application Number
- PCT/CN2024/085094
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2024-03-30
- Publication Date
- 2025-10-09
AI Technical Summary
Existing memory allocation algorithms are difficult to match the business scenarios or business needs of applications, resulting in poor performance of electronic devices and vehicles, affecting user experience, especially in autonomous driving systems, which may cause memory exhaustion and real-time issues.
Use multiple memory allocation strategies to dynamically allocate memory for threads in electronic devices or vehicles, set a suitable memory allocation strategy for each thread based on the application's business scenarios and needs, and optimize the memory allocation process through configuration files and machine learning models.
It improves the performance of electronic equipment and vehicles, meets business scenarios and business needs, reduces memory fragmentation, reduces memory usage and CPU usage, and improves system stability and real-time responsiveness.
Smart Images

Figure CN2024085094_09102025_PF_FP_ABST
Abstract
Description
Memory allocation method, electronic device, and vehicle Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a memory allocation method, electronic equipment, and vehicle. Background Art
[0002] Applications in electronic devices often implement their functionality by running threads. Each thread, while running, must request memory from the electronic device to ensure the proper operation of the thread and the application. Currently, electronic devices often use one of the memory allocation algorithms (or memory allocators) such as ptmalloc, tcmalloc, and jemalloc to allocate memory for all threads in an application.
[0003] However, after allocating memory in this way, the performance of the electronic device is difficult to match the business scenarios or business requirements corresponding to the application, thereby affecting the user experience.
[0004] Summary of the Invention
[0005] An embodiment of the present application provides a memory allocation method, an electronic device, and a vehicle, which jointly allocate memory to threads in an application based on multiple memory allocation strategies, so that the performance of the electronic device or vehicle can meet the business scenarios or business requirements of the application.
[0006] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[0007] In a first aspect, a memory allocation method is provided, for example, in an electronic device. In this method, the electronic device obtains a first memory request corresponding to a first thread in a first application, and in response to the first memory request, obtains a first memory allocation policy corresponding to the first thread. While the first thread is running, the electronic device allocates memory to the first thread based on the first memory allocation policy.
[0008] The above-mentioned first application includes at least one thread, the first thread is any one of the at least one thread, and the first application corresponds to multiple memory allocation strategies, and each thread in the at least one thread corresponds to any one of the multiple memory allocation strategies. That is to say, different threads in an application each correspond to a memory allocation strategy, and different threads can obtain memory based on the appropriate memory allocation strategy during operation. Among them, illustratively, the appropriate memory allocation strategy corresponding to each thread can be set based on the business scenario, business requirements, etc. of the first application. In this way, the purpose of dynamically allocating memory allocation strategies for threads in the electronic device can be achieved without having to make all threads correspond to the same memory allocation strategy. This method of jointly allocating memory based on multiple strategies can make the performance of the electronic device meet the business scenario, business requirements, etc. of the first application.
[0009] In one implementation of the first aspect, when the electronic device obtains the first memory allocation policy corresponding to the first thread, the electronic device may obtain a configuration file of the first application in response to the first memory request, wherein the configuration file of the first application is used to indicate the memory allocation policy corresponding to each thread in the first application. The electronic device may then obtain the first memory allocation policy corresponding to the first thread based on the configuration file of the first application.
[0010] In this implementation, when the threads in the application are running, the electronic device can directly determine the memory allocation strategy corresponding to the first thread based on the configuration file, without having to perform a series of business indicator evaluations on the application or thread, and then select the memory allocation strategy based on the evaluation results, thereby reducing the time for selecting the memory allocation strategy and greatly improving the efficiency of memory allocation.
[0011] In one possible implementation of the first aspect, before obtaining a first memory request corresponding to a first thread in a first application, the electronic device may obtain an application service indicator for the first application, where the application service indicator indicates memory usage and / or processor usage corresponding to running at least one thread in the first application. The electronic device then configures a memory allocation policy for each thread in the first application based on the application service indicator and generates a configuration file for the first application.
[0012] In this implementation, pre-configuring the memory allocation strategy for the thread and pre-generating the configuration file for the application are both beneficial for directly determining the memory allocation strategy corresponding to the thread based on the configuration file when the thread actually runs subsequently, thereby reducing the time for selecting the memory allocation strategy and greatly improving the efficiency of memory allocation.
[0013] In one implementation of the first aspect, when generating a configuration file for a first application, the electronic device may run a test case corresponding to the first application based on each of a plurality of memory allocation policies, and obtain the running results of the test case corresponding to each memory allocation policy, wherein the running results include the memory usage and / or processor occupancy corresponding to at least one thread in the first application under the memory allocation policy corresponding to the test case. The electronic device then determines the memory allocation policy corresponding to each thread in the first application based on the running results of the test case corresponding to each memory allocation policy and the application business indicators, and generates a configuration file for the first application based on the memory allocation policy corresponding to each thread in the first application.
[0014] In this implementation, the running result can represent the actual memory usage and / or CPU usage of at least one thread, while the application business indicator can represent the indicator that at least one thread needs to meet in order to meet the business scenarios, business needs, etc. of the application. In other words, the running result can represent an actual value, and the application business indicator can represent an expected value. By comparing the actual value with the expected value, it can be determined whether the operation of at least one thread can make the performance of the electronic device meet the standard under each memory allocation strategy, and according to the corresponding standard of each memory allocation strategy, the memory allocation strategy corresponding to each thread is adaptively adjusted so that when each thread runs based on its corresponding memory allocation strategy, the performance of the electronic device meets the standard, such as ensuring the memory priority of the electronic device, the CPU performance priority of the electronic device, and the performance balance of the electronic device.
[0015] Thereafter, after generating the configuration file of the first application, the electronic device directly obtains the memory allocation policy corresponding to each thread according to the configuration file and runs it, thereby ensuring that the performance of the electronic device meets the standards when the thread is actually running.
[0016] In one implementation method of the first aspect, when the electronic device determines the memory allocation strategy corresponding to each thread in the first application, if the running result does not meet the application business indicators, the electronic device allocates the corresponding memory allocation strategy to each thread in the first application until the running result meets the application business indicators, and determines the memory allocation strategy currently corresponding to each thread.
[0017] In this implementation, the memory allocation strategy corresponding to the thread can be repeatedly tested during the file configuration phase, so as to pre-configure a memory allocation strategy and configuration file that can meet the business scenario or business needs of the first application, so that the memory allocation strategy can be obtained more quickly and accurately when the process is actually running, making memory allocation more accurate and faster.
[0018] In an implementation method of the first aspect, the above-mentioned operating results meet the application business indicators, including: the memory usage corresponding to at least one thread in the first application is less than or equal to the preset memory usage corresponding to the memory usage, and / or the processor usage corresponding to at least one thread in the first application is less than or equal to the preset usage corresponding to the processor usage.
[0019] In an implementation manner of the first aspect, the configuration file of the first application includes identification information of the first application, identification information of each thread in the at least one thread, and identification information of a memory allocation policy corresponding to each thread.
[0020] In one possible implementation of the first aspect, different memory allocation strategies correspond to different memory allocation algorithms, and the multiple memory allocation strategies are stored in a registry. Furthermore, when the electronic device obtains a first memory allocation strategy corresponding to a first thread, it may obtain identification information of the first memory allocation strategy corresponding to the first thread from a configuration file of the first application, and obtain the first memory allocation strategy from the registry based on the identification information of the first memory allocation strategy.
[0021] In this implementation, the registry can be viewed as a tool for managing a memory allocator in the electronic device, making it easier for the electronic device or a developer to expand and update all memory allocation strategies used by the electronic device.
[0022] In one possible implementation of the first aspect, the first application is an application that implements an autonomous driving function. In this implementation, the electronic device may be a module or device in the vehicle that provides data processing functions, thereby ensuring that the vehicle's performance meets the business scenarios or business requirements of the autonomous driving application, even for threads that provide the autonomous driving function.
[0023] In a second aspect, an electronic device is provided, including:
[0024] a request acquisition module, configured to acquire a first memory request corresponding to a first thread in a first application; the first application includes at least one thread, and the first thread is any one of the at least one threads;
[0025] a policy selection module, configured to obtain, in response to the first memory request, a first memory allocation policy corresponding to the first thread; the first application corresponds to a plurality of memory allocation policies, and each thread in the at least one thread corresponds to any one of the plurality of memory allocation policies;
[0026] The policy execution module is used to allocate memory to the first thread based on the first memory allocation policy during the execution of the first thread.
[0027] In an implementation method of the second aspect, the policy selection module is specifically used to obtain a configuration file of the first application in response to a first memory request; the configuration file of the first application is used to indicate the memory allocation policy corresponding to each thread in the first application; and according to the configuration file of the first application, obtain the first memory allocation policy corresponding to the first thread.
[0028] In an implementation method of the second aspect, the electronic device also includes: a file configuration module, used to obtain application business indicators of the first application; the application business indicators are used to indicate the memory occupancy and / or processor occupancy corresponding to running at least one thread in the first application; based on the application business indicators, the memory allocation strategy corresponding to each thread in the first application is configured, and a configuration file for the first application is generated.
[0029] In an implementation method of the second aspect, the file configuration module is specifically used to run the test case corresponding to the first application based on each memory allocation strategy of multiple memory allocation strategies; obtain the running results of the test case corresponding to each memory allocation strategy; the running results include the memory usage and / or processor occupancy corresponding to at least one thread in the first application under the memory allocation strategy corresponding to the test case; determine the memory allocation strategy corresponding to each thread in the first application according to the running results of the test case corresponding to each memory allocation strategy and the application business indicators; generate a configuration file for the first application according to the memory allocation strategy corresponding to each thread in the first application.
[0030] In one implementation method of the second aspect, the file configuration module is specifically used to allocate a corresponding memory allocation strategy to each thread in the first application if the running result does not meet the application business indicators, until the running result meets the application business indicators, and determine the current memory allocation strategy corresponding to each thread.
[0031] In one implementation method of the second aspect, different memory allocation strategies correspond to different memory allocation algorithms, and multiple memory allocation strategies are saved in a registry; the strategy selection module is specifically used to obtain the identification information of the first memory allocation strategy corresponding to the first thread from the configuration file of the first application; and obtain the first memory allocation strategy from the registry based on the identification information of the first memory allocation strategy.
[0032] In a third aspect, a vehicle is provided, comprising:
[0033] a request acquisition module, configured to acquire a first memory request corresponding to a first thread in a first application; the first application includes at least one thread, and the first thread is any one of the at least one threads;
[0034] a policy selection module, configured to obtain, in response to the first memory request, a first memory allocation policy corresponding to the first thread; the first application corresponds to a plurality of memory allocation policies, and each thread in the at least one thread corresponds to any one of the plurality of memory allocation policies;
[0035] The policy execution module is used to allocate memory to the first thread based on the first memory allocation policy during the execution of the first thread.
[0036] In a fourth aspect, an electronic device is provided, comprising a memory and one or more processors; the memory is coupled to the processor; wherein computer program code is stored in the memory, and the computer program code includes computer instructions, and when the computer instructions are executed by the processor, the electronic device executes the memory allocation method as in the first aspect and any one of its implementations.
[0037] In a fifth aspect, a vehicle is provided, comprising a memory and one or more processors; the memory is coupled to the processor; wherein computer program code is stored in the memory, and the computer program code comprises computer instructions, which, when executed by the processor, enable the vehicle to execute the memory allocation method as in the first aspect and any one of its implementations.
[0038] In a sixth aspect, a computer-readable storage medium is provided, comprising computer instructions, which, when the computer instructions are executed on an electronic device, cause the electronic device to execute the memory allocation method as in the first aspect and any one of its implementations; or, when the computer instructions are executed on a vehicle, cause the vehicle to execute the memory allocation method as in the first aspect and any one of its implementations.
[0039] In the seventh aspect, a computer program product is provided, which, when the computer program product is run on an electronic device, enables the electronic device to execute the memory allocation method as in the first aspect and any one of its implementations; or, when the computer program product is run on a vehicle, enables the vehicle to execute the memory allocation method as in the first aspect and any one of its implementations.
[0040] The beneficial effects that can be achieved by the electronic device provided by the second aspect, the vehicle provided by the third aspect, the electronic device provided by the fourth aspect, the vehicle provided by the fifth aspect, the computer-readable storage medium provided by the sixth aspect, and the computer program product provided by the seventh aspect can be referred to the beneficial effects that can be achieved by the first aspect and any one of its implementation methods, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] FIG1 is a schematic diagram of the structure of ptmalloc shown in an embodiment of the present application;
[0042] FIG2 is a schematic diagram of the structure of tcmalloc shown in an embodiment of the present application;
[0043] FIG3 is a schematic diagram of the structure of jemalloc shown in an embodiment of the present application;
[0044] FIG4 is a first structural diagram of an electronic device according to an embodiment of the present application;
[0045] FIG5 is a schematic diagram of an implementation process of a memory allocation method according to an embodiment of the present application;
[0046] FIG6 is a flowchart of a memory allocation method according to an embodiment of the present application;
[0047] FIG7 is a schematic diagram of a configuration file according to an embodiment of the present application;
[0048] FIG8 is a second flowchart of a memory allocation method according to an embodiment of the present application;
[0049] FIG9 is a second structural diagram of an electronic device according to an embodiment of the present application;
[0050] FIG10 is a third structural diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0051] The technical solutions in the embodiments of the present application will be described below in conjunction with the drawings in the embodiments of the present application. Among them, in the description of the present application, unless otherwise specified, " / " indicates that the objects associated before and after are in an "or" relationship. For example, A / B can represent A or B; "and / or" in the present application is only a description of the association relationship of the associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. In addition, in the description of the present application, unless otherwise specified, "multiple" refers to two or more than two. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple. In addition, in order to facilitate the clear description of the technical solutions of the embodiments of the present application, in the embodiments of the present application, words such as "first" and "second" are used to distinguish between identical or similar items with substantially the same functions and effects. Those skilled in the art will understand that words such as "first" and "second" do not limit the quantity and execution order, and words such as "first" and "second" do not necessarily limit differences. At the same time, in the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or explanations. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or design schemes. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a concrete way for easy understanding.
[0052] In addition, the business scenarios described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. Ordinary technicians in this field can know that with the emergence of new business scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0053] Devices like electronic devices and vehicles equipped with operating systems typically have various applications installed to provide various functions. Applications typically provide their functionality by running processes (a process consists of at least one thread) or threads. Furthermore, each thread must request memory from the device to ensure the proper operation of the thread and application.
[0054] Currently, in the above devices, any one of the memory allocation algorithms such as ptmalloc, tcmalloc, and jemalloc is often used to allocate memory for all threads running in a device.
[0055] 1. ptmalloc
[0056] ptmalloc supports multiple threads. Each thread maintains its own heap (including a free memory list), and threads can concurrently request memory from the free memory list. As shown in Figure 1, the contiguous heap area maintained by the main thread is called the main arena, and the heap area maintained by child threads is called the thread arena.
[0057] The main arena maintains only one heap. When the main arena's heap runs out of space, it is expanded using brk. The thread arena, on the other hand, may maintain multiple heaps. When the thread arena's heap runs out of space, it uses mmap to allocate memory for a separate, discontinuous heap structure. ptmalloc can use multiple arenas (heap areas) to reduce lock contention among multiple threads.
[0058] In addition, chunk is the smallest data structure for managing memory in the heap. A heap is divided into multiple different types of chunks. Different types of chunks are organized together in the form of linked lists and are also called bins.
[0059] A bin represents a free memory list structure, centrally managing free chunks. When a thread releases a chunk, ptmalloc does not immediately return it to the operating system (OS). Instead, it assigns it to ptmalloc's own bin for management. This allows ptmalloc to prioritize finding a chunk of appropriate size from the bin and returning it to the thread when a subsequent chunk is needed, avoiding frequent system calls.
[0060] Based on the size of the chunk, bins are divided into the following categories: fast bin, unsorted bin, samll bin, large bin, etc.
[0061] The fast bin is a high-speed buffer for bins. When a thread frees a chunk smaller than 64 bytes, it is placed in the fast bin by default. When a thread needs to allocate a chunk smaller than or equal to 64 bytes, ptmalloc first searches the fast bin for a suitable chunk. Unless otherwise specified, two adjacent free chunks are not merged into a single free chunk.
[0062] The unsorted bin is a buffer within the bin. Chunks larger than 64 bytes released by a thread or merged from the fast bin are first placed in the unsorted bin. Chunks of any size can be added to the unsorted bin without any size restrictions. When a chunk needs to be allocated to a thread, if no suitable chunk is found in the fast bin or the samll bin, ptmalloc searches for a suitable free chunk in the unsorted bin.
[0063] The "samll bin" is a storage area within a bin that holds smaller chunks, typically smaller than 512 bytes. Each small bin contains a doubly linked list of free chunks (also called a bin list). Freed chunks are added to the front of the list, while needed chunks are removed from the back. Furthermore, two adjacent free chunks are merged into a single free chunk.
[0064] When a chunk needs to be allocated to a thread, if the samll bin is not empty, the corresponding bin removes the last chunk in the bin list and returns it to the thread. When releasing a chunk, ptmalloc checks whether the previous or next chunk is free. If so, it merges the free chunk with the released chunk, removing them from their respective linked lists and merging them into a new chunk. The new chunk is then added to the front of the unsorted bin linked list.
[0065] The large bin is a storage area within the bin that holds larger chunks, typically 512 bytes or larger. Each large bin contains chunks within a given range. Chunks are sorted by descending size, and chunks of the same size are sorted by most recently used. Two adjacent free chunks are merged into a single free chunk.
[0066] When a chunk needs to be allocated to a thread, the large bin is traversed from the top to the bottom to find a chunk that is closest in size to the thread's needs. Once found, the chunk is split into two parts: a user chunk and a remainder chunk. The user chunk is returned to the thread for use, and the remainder chunk is added to the unsorted bin. The process for releasing a chunk is similar to that for releasing chunks from the small bin.
[0067] In addition, ptmalloc also introduces the concept of thread cache (tcache) to further improve the efficiency of memory allocation and release.
[0068] The heap area structures corresponding to the above main arena and thread arena may include the following chunks: allocated chunk (chunk in use), free chunk (idle chunk), top chunk, etc.
[0069] When all bins are unable to respond to a thread's memory allocation request, the top chunk is used to respond. If the requested data is larger than the top chunk, the top chunk is expanded using brk. If the requested data is smaller than the top chunk, memory of the appropriate size is allocated from the top chunk and returned to the thread, with the remaining portion continuing to be used as the top chunk.
[0070] Furthermore, a thread arena needs to maintain multiple heaps, each with its own heap header, and heap_info represents the heap header information. The main arena, however, does not need to maintain multiple heaps and therefore does not require this structure. Each chunk has its own malloc_chunk, which stores its own malloc_state structure. The main arena's malloc_state is stored as a global variable in the data area, while the thread arena's multiple heaps share the same malloc_state, so it is stored directly on the heap.
[0071] The key technologies involved in the ptmalloc related content shown above may include:
[0072] 1. Splitting and merging chunks: When a memory allocation request comes, ptmalloc will find a suitable chunk in the corresponding arena to satisfy the request, and may need to adjust the chunk's size and status. When a chunk is released, it will be put back into the arena to which it belongs and may be merged with adjacent free chunks.
[0073] 2. Use mmap to allocate large memory: For large memory allocation requests, ptmalloc will directly use the mmap() system call to obtain memory from the system instead of allocating it from the arena.
[0074] 3. Delayed merging of fast bins: To improve efficiency, chunks placed in the fast bin will not be merged until they are used again.
[0075] However, because later allocated memory is released first, ptmalloc shrinks memory starting with the top chunk. If adjacent chunks can't be released, chunks below the top chunk can't be released either. This results in memory being allocated from other arenas or from additional resources, resulting in reduced memory utilization. Furthermore, while ptmalloc supports multithreading, it's not very multithread-friendly. Frequent memory allocation and locking can lead to lock contention and increased memory fragmentation.
[0076] Memory fragmentation refers to unusable memory blocks within a system. Excessive memory fragmentation can increase the overall system's memory noise. Multithreaded concurrency further exacerbates memory fragmentation, reducing memory usage efficiency.
[0077] 2. tcmalloc
[0078] In terms of macro-architecture, tcmalloc is divided into front-end, middle-end and back-end, as shown in Figure 2 for example.
[0079] The front-end is a per-thread cache pool that provides fast memory allocation and reallocation to applications. The middle-end is responsible for filling or reclaiming the cache for the front-end, and the back-end is responsible for acquiring or releasing memory from the operating system.
[0080] The front-end can use either a per-CPU cache or the older per-thread cache. The back-end can support either a hugepage-aware page heap or the older legacy page heap. The middle-end includes a transfer cache and a central free list.
[0081] In the front-end, the thread cache is a cache owned independently by each thread. A cache contains multiple free memory lists (size classes). Each list (size-class) has its own object, and each object is the same size.
[0082] tcmalloc divides memory from 0KB to 256KB into 86 size classes. When requesting small memory (less than 256KB), tcmalloc maps it to a size class based on the requested memory size. For example, a request for 0 to 8 bytes is mapped to size class 1 and allocated 8 bytes; a request for 9 to 16 bytes is mapped to size class 2, and so on. If no memory is found, the middle-end is requested for memory. If the requested memory block is too large (over 256KB), the back-end is bypassed and the middle-end is requested directly for memory.
[0083] The middle-end is responsible for providing cache for the front-end. In other words, when the front-end cache memory is insufficient, it requests memory from the middle-end.
[0084] The transfer cache is an array of pointers that quickly moves pointers to objects (or memory) released by the front-end into this array, or retrieves objects (or memory) from this array. (This structure can be thought of as a cache layer of the central free list.)
[0085] The central free list manages spans (tcmalloc divides memory blocks into pages; one or more pages can form a span). Spans are managed using the emtpy and nonempty lists. The central free list is divided by size class, so only requests of the same size class will conflict with locks.
[0086] The central cache provides memory for the thread cache when it runs out of memory. It maintains a linked list of free blocks, which has the same number of free blocks as the thread cache. When the thread cache becomes overwhelmed with memory, it can be returned to the central cache. The central free list extracts spans from the page heap and splits them into fixed-size objects according to a predetermined size for use by the thread cache.
[0087] When neither the front-end nor the middle-end has a suitable memory block, the memory allocation request goes to the back-end. The back-end's primary data structure is the page heap, which also stores linked lists, but these lists store spans. A span is the unit of memory management within the page heap, consisting of a group of consecutive pages. For example, a span of two pages is used. Multiple spans are managed using linked lists. Of course, spans of four pages can also be used.
[0088] The page heap is primarily implemented using linked lists and sets. Both linked lists and sets store spans. A span consisting of one page is stored in the first linked list, a span consisting of two pages is stored in the second linked list, and so on. Spans consisting of more than 128 pages are stored in the set. When the middle-end cannot satisfy a memory allocation request, it requests a span from the back-end, which then manages the span.
[0089] The key technologies involved in the tcmalloc related content shown above may include:
[0090] 1. Stacked cache (multi-layer cache design): Reduce lock conflicts through a multi-layer cache mechanism while improving the efficiency of memory allocation and internal management.
[0091] 2. Memory Fragmentation Control: By classifying small memory blocks into 86 different size classes and designing a mechanism for transferring memory blocks across CPUs, we reduce fragmentation and improve memory utilization. The size-class division method designed by tcmalloc can keep the size of memory fragmentation within 12.5% of the total page size.
[0092] However, in scenarios where medium- to large-scale memory allocations are frequent, intense competition for spin locks can lead to increased CPU utilization and reduced CPU performance. Furthermore, single-threaded scenarios offer little benefit; instead, the increased maintenance operations within the L3 cache pool can worsen CPU performance. Memory allocation for multiple threads can increase memory fragmentation, increase memory noise, and reduce memory utilization.
[0093] jemalloc
[0094] jemalloc involves arena, tcache, bins, runs, page, and chunk.
[0095] Among them, arena: jemalloc divides the memory into multiple arenas to reduce lock conflicts in high-concurrency scenarios. Each thread has its own arena, which can reduce the lock overhead in concurrent scenarios.
[0096] tcache: tcache is a thread-level cache. When there is a memory allocation request, it will first go to tcache. If tcache does not have a satisfactory memory block, it will be allocated to the arena.
[0097] bin: jemalloc divides small memory into multiple size-classes (similar to the division rules of tcmalloc).
[0098] Runs, pages, and chunks: Arena will be divided into multiple chunks. The default size of a chunk is 4MB, and chunks are managed in units of pages. The first few pages of each chunk are used to store the metadata of the chunk, followed by runs of one or more pages of different specifications. Multiple small objects together form a run. When a chunk is used, it will be divided into many runs and recorded in a bin. Classes of different sizes correspond to different bins. In the bin, there will be a red-black tree to maintain free runs, and in the run, a bitmap is used to record the allocation status. In addition, each arena maintains a set of red-black trees of available runs arranged by address.
[0099] The key technologies involved in the jemalloc related content shown above may include:
[0100] 1. Arena Division: jemalloc introduces arenas, which divide memory into a number of arenas. Threads are ultimately bound to a particular arena. Because the two arenas have virtually no connection in address space, allocation can be performed in a lock-free state. Also, because the arenas are discontinuous, the probability of them falling into the same cache line is minimal, ensuring their independence.
[0101] 2. Classification of tcache and small objects, large objects, and huge objects: As shown in Figure 3, jemalloc divides memory allocation requests into three types: small objects (small memory allocation requests), large objects (large memory allocation requests), and huge objects (large memory allocation requests). For small objects and large objects, it will try to obtain memory blocks from tcache. For huge objects, it will directly use mmap to apply for system memory from the system.
[0102] However, the arena mechanism can reduce lock contention when memory allocation requests miss a tcache hit, but it also adds additional memory noise. The default number of arenas is four times the number of CPUs. Using tcache also incurs additional memory consumption and consumes a large amount of memory.
[0103] In addition, in addition to the shortcomings of the three memory allocation methods mentioned above, the comparison of the processing performance of these three memory allocation methods in different aspects can also be seen in Table 1 below:
[0104] Table 1
[0105] Among them, jemalloc is designed for multi-threaded scenarios with frequent large memory allocation and release. It performs well in multi-core scenarios, but suffers from high memory noise. tcmalloc is also designed for multi-threaded scenarios, but its memory noise is lower than jemalloc. ptmalloc is a relatively basic memory allocation algorithm, but it suffers from poor CPU performance and increased memory fragmentation when used in multi-threaded scenarios.
[0106] Regardless of the memory allocation method, there are defects or shortcomings in one or more aspects, such as high memory usage of the electronic device (or vehicle), high memory noise floor of the electronic device (or vehicle), high CPU occupancy of the electronic device (or vehicle), and inability to accommodate multi-threaded scenarios.
[0107] Currently, the memory allocation algorithms used for an application are fixed, such as those set based on the operating system of the electronic device (or vehicle). Due to different business scenarios and business needs, different applications will also require the electronic device (or vehicle) to have higher performance in one or more aspects during operation. However, since the memory allocation algorithm itself has its own shortcomings and cannot be adjusted according to the application's business scenario or business needs, using a fixed memory allocation algorithm to allocate memory for threads in the application often makes it difficult to ensure that the performance of the electronic device (or vehicle) matches the business scenario or business needs of the application, thus affecting the user experience.
[0108] Vehicles, in particular, often operate autonomous driving systems. These systems utilize onboard sensors to perceive environmental changes in real time and make vehicle control decisions. These systems aim to improve traffic safety and efficiency, placing high demands on real-time performance.
[0109] Autonomous driving systems typically process sensor data at regular intervals and make vehicle control decisions. To ensure high real-time system performance, the interval between sensor data processing must be shortened, which requires high CPU performance when processing data. The complexity of the autonomous driving algorithms used in data processing results in a large number of data distribution nodes, resulting in significant memory operation overhead. This impacts the real-time response of the autonomous driving system in emergency scenarios (for example, a pedestrian suddenly intruding), potentially compromising the system's safety. Therefore, autonomous driving systems place strong demands on memory allocation performance.
[0110] However, autonomous driving systems are embedded real-time operating systems and don't offer the large memory footprint (e.g., hundreds of GB) that some servers offer. Furthermore, because the complex autonomous driving algorithms used in these systems frequently call memory operations, improper memory allocation algorithms can lead to a surge in memory noise, exposing the system to the risk of out-of-memory (OOM) errors, impacting memory usage in other modules and potentially causing functional issues in the autonomous driving system.
[0111] Based on the above, an embodiment of the present application provides a memory allocation method. In this method, an electronic device can obtain a first memory request corresponding to a first thread in a first application and, in response to the first memory request, obtain a first memory allocation policy corresponding to the first thread. While the first thread is running, the electronic device allocates memory to the first thread based on the first memory allocation policy.
[0112] The above-mentioned first application includes at least one thread, the first thread is any one of the at least one thread, and the first application corresponds to multiple memory allocation strategies, and each thread in the at least one thread corresponds to any one of the multiple memory allocation strategies. That is to say, different threads in an application each correspond to a memory allocation strategy, and different threads can obtain memory based on the appropriate memory allocation strategy during operation. Among them, illustratively, the appropriate memory allocation strategy corresponding to each thread can be set based on the business scenario, business requirements, etc. of the first application. In this way, the purpose of dynamically allocating memory allocation strategies for threads in the electronic device can be achieved without having to make all threads correspond to the same memory allocation strategy. This method of jointly allocating memory based on multiple strategies can make the performance of the electronic device meet the business scenario, business requirements, etc. of the first application.
[0113] In addition, the above method can also be applied to vehicles, and the vehicle's performance can also be made to meet the business scenarios and business requirements of the first application.
[0114] It is understandable that the above memory allocation algorithm can also be used as a memory allocator, or called a memory allocator.
[0115] Taking the above method applied to an electronic device as an example, as shown in FIG4 , the electronic device may include a memory allocation scheduler (arbitrator), a profiler manager (profiler manager), a memory allocation manager (allocator manager) and a memory allocator registry (allocator registry).
[0116] Among them, when the application is compiled, the file manager can select the memory allocator with the best performance from the memory allocator registry according to the application business indicators of the application, and create a configuration file (profile) corresponding to the application.
[0117] For example, the file manager can save profile 1 (expressed as profile-app1) corresponding to application 1, profile 2 (expressed as profile-app2) corresponding to application 2, and profile 3 (expressed as profile-app3) corresponding to application 3.
[0118] Among them, the application business indicator can be given by the user for the application when the application is compiled, or it can be pre-set for the application. In addition, since the application can include at least one thread, the application business indicator of the application can also indicate the memory occupancy and / or CPU occupancy corresponding to at least one thread in the running application. Further, the application business indicator can be given or set according to the business scenario, business needs, etc. of the application. For example, for application 1, the memory performance of the electronic device is required to be prioritized, such as a small memory occupancy. In this way, the business indicator corresponding to application 1 can indicate the memory occupancy; for application 2, the CPU performance of the electronic device is required to be prioritized, such as a low CPU occupancy. In this way, the business indicator corresponding to application 2 can indicate the CPU occupancy; for application 3, the performance of the electronic device is required to be balanced, such as a small memory occupancy and a low CPU occupancy. In this way, the business indicator corresponding to application 3 can indicate the memory occupancy and CPU occupancy.
[0119] In some possible implementations, the application configuration file may include at least the following information:
[0120] Application identification information: used to uniquely identify an application, which may include the application name, version information, path information, etc.
[0121] Thread identification information: This may include the identification of each thread in the application, the memory requirements of the thread, etc.
[0122] Memory allocation mode: This indicates the timing of memory allocation and memory release for each thread during execution. For example, a thread may allocate and release memory periodically, or it may allocate and release memory when a specific event occurs.
[0123] Memory allocator (or memory allocation policy) identification information: This information indicates the memory allocator (or memory allocation policy) corresponding to each thread in the application. The memory allocators (or memory allocation policies) corresponding to any two threads can be the same or different. Furthermore, an application configuration file can correspond to one or more memory allocators (or memory allocation policies), and an application can correspond to one or more memory allocators (or memory allocation policies).
[0124] Furthermore, the file manager may also use a machine learning algorithm or a machine learning model to select a memory allocator corresponding to a thread from a memory allocator registry, wherein the machine learning model may be implemented based on a machine learning algorithm. For example, the file manager may use an application's business metrics as input to the machine learning model and obtain the output of the machine learning model, i.e., obtain a configuration file for the application.
[0125] In some possible implementations, the file manager can convert the above-mentioned application business indicators into standard files as input to the machine learning model. In addition, when the machine learning model generates the configuration file corresponding to the application, it starts running the test cases related to the application based on the input standard file, and then gives the running results of the test cases under different memory allocators, such as the actual memory usage, the actual CPU usage, etc. In addition, the machine learning model selects the most suitable or optimal memory allocator for each thread based on the running results under different memory allocators and the expected application business indicators. The machine learning model can determine the corresponding memory allocator for each thread in an application, and finally, combined with the memory allocator corresponding to each thread, it generates a configuration file corresponding to the application.
[0126] Furthermore, in some possible implementations, the above-mentioned machine learning model can be established based on a method similar to the training of a neural network model.
[0127] In an embodiment of the present application, the memory allocator corresponding to a thread can be any one of multiple memory allocators, for example, any one of jemalloc, tcmalloc, ptmalloc, and mimalloc. An application can correspond to multiple memory allocators such as jemalloc, tcmalloc, ptmalloc, and mimalloc.
[0128] The memory allocation scheduler selects the appropriate memory allocator for a thread based on the configuration file of the application to which the thread belongs. This improves the accuracy and efficiency of memory allocation.
[0129] The memory allocator registry stores all available memory allocator resources for the electronic device, allowing the file manager to select them during application compilation and the memory allocation scheduler to select them during thread execution. The memory allocator registry is a memory management tool that allows for easy expansion and updating of memory allocators.
[0130] The memory allocation manager can manage all memory allocators, including creating memory allocators, initializing memory allocators, configuring memory allocators, destroying memory allocators, etc. The memory allocation manager can be regarded as a global memory management tool that can improve the efficiency and flexibility of memory allocators.
[0131] In conjunction with the structure shown in FIG4 above, the implementation process of the memory allocation method in the embodiment of the present application can be seen in FIG5. Among them, the machine learning model (machine learning module) outputs a configuration file corresponding to the application based on the application business indicators of the input application. The memory allocator corresponding to each thread in the configuration file (such as allocator 1, allocator 2, allocator 3, etc.) is saved by the memory allocator registry, and the memory allocator corresponding to each thread in the configuration file is managed by the memory allocation manager. A memory request (or memory allocation request) is initiated by a thread, and the memory allocation scheduler responds to the memory request and obtains the memory allocator corresponding to the thread from the memory allocator registry according to the configuration file of the application to which the thread belongs.
[0132] The memory allocation scheduler may further include a selection module for specifically obtaining the memory allocator corresponding to the thread from the memory allocator registry according to the identification information of the memory allocator corresponding to the thread in the configuration file.
[0133] It can be understood that the contents indicated by the aforementioned application business indicators are only some examples. In some other possible implementation methods, the memory performance of the electronic device is required to be prioritized, and it may also specifically include low memory noise floor, low memory utilization, etc. There is no specific restriction on this in the embodiments of the present application.
[0134] It can be seen from the above content that in the memory allocation method in the embodiment of the present application, the application in the electronic device may include at least one thread, the application corresponds to multiple memory allocators, and each thread in the application corresponds to any one of the multiple memory allocators. In other words, different threads in an application each correspond to a memory allocator, and different threads can obtain memory based on the appropriate memory allocator during operation. Among them, the appropriate memory allocator corresponding to each thread can be set based on the business scenario, business requirements, etc. of the application. In this way, the purpose of dynamically allocating memory allocators for threads in the electronic device can be achieved without having to make all threads correspond to the same memory allocator. This method of jointly allocating memory based on multiple allocators can make the performance of the electronic device meet the business scenario or business requirements of the application, and also improve the efficiency and performance of memory management.
[0135] Taking the above memory allocation method applied to an electronic device as an example, as shown in FIG6 , the memory allocation method may include the following steps S601 - S603 .
[0136] S601: The electronic device obtains a first memory request corresponding to a first thread in a first application.
[0137] The first application is any application in the electronic device, the first application includes at least one thread, and the first thread is any one of the at least one thread.
[0138] It is understandable that for each thread running in the electronic device, the electronic device can obtain the memory request corresponding to the thread and implement the memory allocation method for each thread. In steps S601-S603, the memory allocation method in the embodiment of the present application is described by taking the processing of the first thread as an example.
[0139] S602: In response to the first memory request, the electronic device obtains a first memory allocation policy corresponding to the first thread.
[0140] Among them, the first application can correspond to multiple memory allocation strategies, different memory allocation strategies are strategies implemented based on different memory allocation algorithms (or memory allocators), or different memory allocation strategies correspond to different memory allocation algorithms (or memory allocators), such as jemalloc, tcmalloc, ptmalloc, mimalloc, etc. in the aforementioned embodiments. In addition, each thread in the first application can correspond to any one of the multiple memory allocation strategies, and the memory allocation strategies corresponding to different threads can be the same or different. For example, the first thread corresponds to jemalloc, the second thread corresponds to tcmalloc, or the first thread corresponds to ptmalloc, and the second thread also corresponds to ptmalloc.
[0141] In the embodiment of the present application, each thread has its own corresponding memory allocation strategy. For example, the first thread corresponds to the first memory allocation strategy, and the second thread corresponds to the second memory allocation strategy. Thus, in the electronic device, each thread runs based on the most suitable memory allocation strategy, so that the performance of the electronic device can meet the business scenario or business requirements of the first application, such as having higher CPU performance and less memory usage.
[0142] In some embodiments, the correspondence between threads and memory allocation strategies can be pre-configured. In this case, the electronic device can obtain the configuration file of the first application to which the first thread belongs in response to the first memory request. The configuration file of the first application is used to indicate the memory allocation strategy corresponding to each thread in the first application, that is, the correspondence between the first thread and the first memory allocation strategy is saved in the configuration file of the first application. Afterwards, the electronic device can obtain the first memory allocation strategy corresponding to the first thread based on the configuration file. In this way, when the threads in the application are running, the electronic device can directly determine the memory allocation strategy corresponding to the first thread based on the correspondence in the configuration file, without having to perform a series of business indicator evaluations on the application or thread, and then select the memory allocation strategy based on the evaluation results, thereby reducing the time for selecting the memory allocation strategy and greatly speeding up the efficiency of memory allocation.
[0143] The configuration file of the first application is pre-configured or generated. When generating the configuration file of the first application, the electronic device may obtain an application service indicator of the first application to which the first thread belongs, wherein the application service indicator may indicate memory usage and / or processor usage corresponding to running at least one thread in the first application.
[0144] The electronic device then configures a memory allocation policy for each thread in the first application based on the application service indicators of the first application and generates a configuration file for the first application. The configuration file for the first application can then include the correspondence between each thread and the memory allocation policy. Pre-configuring memory allocation policies for threads and pre-generating configuration files for applications facilitates determining the memory allocation policy corresponding to a thread directly from the configuration file when the thread actually runs. This reduces the time required to select a memory allocation policy and significantly improves memory allocation efficiency.
[0145] For example, in the configuration file shown in FIG7 , the identification information of the first application can be saved, such as “my application” indicated by “name”, the identification information of the two threads in the first application can be saved, such as “1”, “2”, etc. indicated by “id”, and the identification information of the memory allocation strategy corresponding to each thread can also be saved, such as “allocator1”, “allocator2”, etc. indicated by “allocator”.
[0146] In addition, in some possible implementations, the configuration file shown in Figure 7 can also save the memory requirements corresponding to each thread, such as "1024", "512", etc. indicated by "size", and the unit corresponding to the numerical value is the unit of memory size, such as bit, byte, KB, MB, GB, etc.; and, the configuration file can also save the event information of memory allocation and memory release for each thread, such as "allocations" indicates that memory is allocated to the thread, and "on start" indicated by "event" indicates the timing of memory allocation.
[0147] In some possible implementations, when generating a configuration file for the first application, the electronic device may, based on each memory allocation strategy of the multiple memory allocation strategies, run a test case corresponding to the first application, and obtain the running results of the test case corresponding to each memory allocation strategy, wherein the running results include the memory usage and / or CPU usage corresponding to at least one thread in the first application under the memory allocation strategy corresponding to the test case. Furthermore, the electronic device may determine the memory allocation strategy corresponding to each thread in the first application based on the running results of the test case corresponding to each memory allocation strategy and the application business indicators, and generate a configuration file for the first application based on the memory allocation strategy corresponding to each thread in the first application.
[0148] Among them, the running result can represent the actual memory usage and / or CPU usage of at least one thread, and the application business indicator can represent the indicator that at least one thread needs to meet in order to meet the business scenarios, business needs, etc. of the application. That is, the running result can represent an actual value, and the application business indicator can represent an expected value. By comparing the actual value with the expected value, it can be determined whether the operation of at least one thread can make the performance of the electronic device meet the standard under each memory allocation strategy, and according to the corresponding standard of each memory allocation strategy, the memory allocation strategy corresponding to each thread is adaptively adjusted so that when each thread runs based on its corresponding memory allocation strategy, the performance of the electronic device meets the standard, such as ensuring the memory priority of the electronic device, the CPU performance priority of the electronic device, and the performance balance of the electronic device.
[0149] Thereafter, after generating the configuration file of the first application, the electronic device directly obtains the memory allocation policy corresponding to each thread according to the configuration file and runs it, thereby ensuring that the performance of the electronic device meets the standards when the thread is actually running.
[0150] In some possible implementations, as shown in FIG8 , when the electronic device generates a configuration file for the first application, if the above-mentioned operation result (represented as result 1) does not meet the application business indicator (i.e., the performance of the electronic device does not meet the standard), a memory allocation strategy is first assigned to each thread. Then, based on the memory allocation strategy currently corresponding to each thread, the test case of the first application is run again, and the operation result (represented as result 2) is obtained again. If the result 2 meets the application business indicator, it means that the performance of the electronic device meets the standard. In this case, the electronic device uses the memory allocation strategy currently corresponding to each thread to generate the configuration file for the first application; and if the result 2 still does not meet the application business indicator, the memory allocation strategy corresponding to each thread is adjusted again, and based on the memory allocation strategy currently corresponding to each thread, the test case of the first application is run again, and the operation result (represented as result 3) is obtained again. If the result 3 meets the application business indicator, it means that the performance of the electronic device meets the standard. In this case, the electronic device uses the memory allocation strategy currently corresponding to each thread to generate the configuration file for the first application; and if the result 3 still does not meet the application business indicator, the above process is repeated until the operation result meets the application business indicator.
[0151] This method of generating a configuration file for the first application can repeatedly test the memory allocation strategy corresponding to the thread during the file configuration phase, thereby pre-configuring a memory allocation strategy and configuration file that can meet the business scenarios or business needs of the first application. This makes it easier to obtain the memory allocation strategy more quickly and accurately when the process is actually running, making memory allocation more accurate and faster.
[0152] Exemplarily, the above-mentioned operating results meet the application business indicators, which may include: the memory usage corresponding to at least one thread in the first application is less than or equal to the preset memory usage corresponding to the memory usage (i.e., the memory of the electronic device is prioritized), the processor usage corresponding to at least one thread in the first application is less than or equal to the preset usage corresponding to the processor usage (i.e., the CPU performance of the electronic device is prioritized), the memory usage is less than or equal to the preset memory usage and the processor usage is less than or equal to the preset usage (i.e., the performance of the electronic device is balanced), etc.
[0153] It is understandable that in order to ensure balanced performance of the electronic device, some memory or CPU performance may be sacrificed. That is, compared with memory priority, the memory usage will increase when performance is balanced, and the CPU usage will also increase when performance is balanced compared with CPU performance priority. Based on this, in some possible implementations, the preset memory amount corresponding to the above-mentioned electronic device when performance is balanced may be greater than the preset memory amount when memory priority is given, and the preset usage may be greater than the preset usage when CPU performance priority is given.
[0154] In some possible implementations, when the electronic device generates the configuration file of the first application, it may also be based on a machine learning model or a machine learning algorithm, etc. For details, please refer to the aforementioned embodiments.
[0155] In some possible implementations, the above application service indicators may be specified by a technician or developer when the application is compiled, and this is not specifically limited in the embodiments of the present application.
[0156] In some embodiments, the configuration file of the above-mentioned first application may only store identification information of the memory allocation strategies corresponding to each thread, etc., to obtain the first memory allocation strategy corresponding to the first thread, that is, to obtain the identification information of the first memory allocation strategy. After that, the electronic device can call the corresponding strategy and execute it according to the identification information of the first memory allocation strategy. Exemplarily, a registry (or the memory allocator registry in the aforementioned embodiment) can also be stored in the electronic device, and the above-mentioned multiple memory allocation strategies are stored in the registry. When obtaining the first memory allocation strategy corresponding to the first thread, the electronic device can obtain the first memory allocation strategy from the registry according to the identification information of the first memory allocation strategy.
[0157] In addition, when the electronic device generates a configuration file for the first application, the electronic device can also obtain the memory allocation policy corresponding to each thread in the first application from the registry according to the identification information of the memory allocation policy corresponding to each thread, and generate the configuration file for the first application based on the memory allocation policy corresponding to each thread.
[0158] The above registry can be regarded as a tool for managing the memory allocator in the electronic device, which facilitates the electronic device or the developer to expand and update all memory allocation strategies used by the electronic device.
[0159] S603: When the first thread is running, the electronic device allocates memory to the first thread based on the first memory allocation policy.
[0160] It is understood that the electronic device can determine a memory allocation policy corresponding to each thread in the first application. When each thread is running, each thread can obtain memory based on the memory allocation policy that suits it. Furthermore, when each thread is running, the performance of the electronic device can be aligned with the business scenario or business requirements of the first application, and the efficiency and performance of memory management can be improved.
[0161] In addition, the configuration file in the above embodiment can be configured by the electronic device and stored locally on the electronic device; or it can be configured by another device, uploaded to the server by the other device after configuration, and retrieved from the server by the electronic device. The embodiments of this application do not specifically limit the location where the configuration file is generated and the method for obtaining it.
[0162] The above content takes the memory allocation method applied to electronic devices as an example. In some other embodiments, the memory allocation method can also be applied to vehicles. For specific embodiments and methods, reference can be made to the contents of the above embodiments.
[0163] Also, taking the application of the above method to a certain type of vehicle as an example, for the autonomous driving application (or autonomous driving system) in the vehicle, the process of running the autonomous driving application based on this method can be described as follows.
[0164] Table 2 illustrates the performance of selected threads in an autonomous driving application after running them using tcmalloc and jemalloc, respectively. The CPU usage shows how jemalloc reduces CPU usage compared to tcmalloc, and the memory usage shows how jemalloc reduces memory usage compared to tcmalloc. Table 3 also illustrates the overall performance comparison of the vehicle system after running threads in an autonomous driving application using tcmalloc and jemalloc, respectively.
[0165] Table 2
[0166] Table 3
[0167] Combining the results in Table 1, we can see that for thread 1, jemalloc reduced the CPU usage by 2.95%, but used an additional 101MB of memory. For thread 2, jemalloc reduced the CPU usage by 1.72%, while also reducing memory usage by 21MB. Looking at the vehicle system as a whole, jemalloc used 700MB more memory than tcmalloc, but reduced the overall CPU usage by 1.3%.
[0168] Based on the above, the vehicle can consider memory priority, CPU performance priority, and performance balance, and configure corresponding memory allocation strategies for each thread in the autonomous driving application. This will generate profile 1 for ensuring memory priority, profile 2 for ensuring CPU performance priority, and profile 3 for ensuring performance balance, respectively, for the autonomous driving application, as shown in Table 4 below.
[0169] Table 4
[0170] Profile 1 prioritizes vehicle memory, increasing the vehicle's overall system CPU usage by 0.5% and reducing memory usage by 200MB. Profile 2 prioritizes vehicle CPU performance, increasing overall system memory usage by 50MB and reducing CPU usage by 3%. Profile 3 ensures balanced vehicle performance, reducing overall system memory usage by 20MB and reducing CPU usage by 0.5%.
[0171] The actual configuration file saved or retrieved by the vehicle can be determined based on the application business indicators corresponding to the autonomous driving application (such as memory priority, CPU performance priority, performance balance, etc.). For example, if the application business indicators corresponding to the autonomous driving application require memory priority, the vehicle will save configuration file 1. Subsequently, when the threads in the autonomous driving application are running, the vehicle will determine the memory allocation strategy corresponding to the threads based on configuration file 1. As a result, when the threads are running, the vehicle's memory usage is relatively small.
[0172] In some schemes, multiple embodiments of the present application can be combined, and the combined scheme can be implemented. Optionally, some operations in the process of each method embodiment are optionally combined, and / or the order of some operations is optionally changed. In addition, the execution order between the steps of each process is only exemplary and does not constitute a restriction on the execution order between the steps. There can also be other execution orders between the steps. It is not intended to indicate that the execution order is the only order in which these operations can be performed. Those of ordinary skill in the art will think of many ways to reorder the operations described in the embodiments of the present application. In addition, it should be noted that the process details involved in a certain embodiment of the present application are also applicable to other embodiments in a similar manner, or different embodiments can be used in combination.
[0173] Furthermore, some steps in the method embodiments may be equivalently replaced with other possible steps. Alternatively, some steps in the method embodiments may be optional and may be deleted in certain usage scenarios. Alternatively, other possible steps may be added to the method embodiments.
[0174] Furthermore, the various method embodiments may be implemented separately or in combination.
[0175] It is understandable that in order to achieve the above functions, the aforementioned electronic device or vehicle includes hardware and / or software modules corresponding to the execution of each function. In combination with the algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to be beyond the scope of this application.
[0176] In this embodiment, the electronic device or vehicle can be divided into functional modules according to the above-mentioned method example. For example, each functional module can be divided according to each function, or two or more functions can be integrated into a single processing module. The above-mentioned integrated modules can be implemented in the form of hardware. It should be noted that the module division in this embodiment is schematic and is only a logical functional division. In actual implementation, other division methods may be used.
[0177] An embodiment of the present application provides an electronic device, as shown in FIG9 , which may include a request acquisition module 901 , a policy selection module 902 , and a policy execution module 903 .
[0178] The request acquisition module 901 is configured to acquire a first memory request corresponding to a first thread in a first application; the first application includes at least one thread, and the first thread is any one of the at least one thread, for example, implementing the relevant contents of step S601 above.
[0179] A policy selection module 902 is configured to, in response to the first memory request, obtain a first memory allocation policy corresponding to the first thread; the first application corresponds to multiple memory allocation policies, and each of the at least one thread corresponds to any one of the multiple memory allocation policies. For example, this module implements the relevant contents of step S602 above.
[0180] The policy execution module 903 is configured to allocate memory to the first thread based on the first memory allocation policy during the execution of the first thread, for example, implementing the relevant contents of the above step S603.
[0181] The embodiment of the present application further provides a vehicle, which may also include a request acquisition module, a strategy selection module, a strategy execution module, etc. Also, the functions of these modules can be referred to the above embodiments and will not be repeated here.
[0182] An embodiment of the present application further provides an electronic device, as shown in FIG10 , which may include one or more processors 1001 , a memory 1002 , and a communication interface 1003 .
[0183] The memory 1002 and the communication interface 1003 are coupled to the processor 1001. For example, the memory 1002, the communication interface 1003 and the processor 1001 may be coupled together via a bus 1004.
[0184] The communication interface 1003 is used to transmit data with other devices. The memory 1002 stores computer program code. The computer program code includes computer instructions. When the computer instructions are executed by the processor 1001, the electronic device executes the memory allocation method in the embodiment of the present application.
[0185] The processor 1001 may be a processor or a controller, such as a central processing unit (CPU), a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the present disclosure. The processor may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and the like.
[0186] The bus 1004 may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. The bus 1004 may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, FIG10 shows only one thick line, but this does not mean that there is only one bus or only one type of bus.
[0187] An embodiment of the present application also provides a vehicle, which may include a processor, memory, communication interface, etc. as shown in Figure 10 above, and the structure is the same as that shown in Figure 10 above. In addition, the functions of the processor, memory, and communication interface can refer to the previous embodiment and will not be repeated here.
[0188] An embodiment of the present application also provides a computer-readable storage medium, which includes computer instructions. When the computer instructions are executed on an electronic device or a vehicle, the electronic device or the vehicle executes the relevant method steps in the above method embodiment.
[0189] An embodiment of the present application further provides a computer program product, which, when executed on a computer, enables the computer to execute the relevant method steps in the above method embodiment.
[0190] Among them, the electronic device, vehicle, computer-readable storage medium or computer program product provided in this application is used to execute the corresponding method provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding method provided above, and will not be repeated here.
[0191] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0192] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0193] The units described as separate components may or may not be physically separate, and the components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple places. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0194] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0195] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the contributing part or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a device (which can be a single-chip microcomputer, chip, etc.) or a processor to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0196] The above content is only a specific embodiment of this application, but the scope of protection of this application is not limited to this. Any changes or replacements within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A memory allocation method, characterized in that: The method comprises: Obtaining a first memory request corresponding to a first thread in a first application; the first application includes at least one thread, and the first thread is any one of the at least one thread; In response to the first memory request, obtaining a first memory allocation policy corresponding to the first thread; the first application corresponds to multiple memory allocation policies, and each of the at least one thread corresponds to any one of the multiple memory allocation policies; During the execution of the first thread, memory is allocated to the first thread based on the first memory allocation policy.
2. The method according to claim 1, characterized in that The obtaining, in response to the first memory request, a first memory allocation policy corresponding to the first thread, includes: In response to the first memory request, obtaining a configuration file of the first application; the configuration file of the first application is used to indicate a memory allocation policy corresponding to each thread in the first application; According to the configuration file of the first application, a first memory allocation policy corresponding to the first thread is obtained.
3. The method according to claim 2, characterized in that Before obtaining the first memory request corresponding to the first thread in the first application, the method further includes: Obtaining an application service indicator of the first application; the application service indicator is used to indicate a memory occupancy and / or a processor occupancy corresponding to running the at least one thread in the first application; Based on the application business indicators, a memory allocation policy corresponding to each thread in the first application is configured, and a configuration file for the first application is generated.
4. The method according to claim 3, characterized in that The configuring a memory allocation policy corresponding to each thread in the first application based on the application business indicator and generating a configuration file for the first application includes: Based on each memory allocation strategy of the multiple memory allocation strategies, respectively run a test case corresponding to the first application; Obtaining an execution result of the test case corresponding to each memory allocation strategy; the execution result includes a memory usage and / or a processor usage rate corresponding to the at least one thread in the first application under the memory allocation strategy corresponding to the test case; Determine, based on the running results of the test cases corresponding to each memory allocation strategy and the application business indicators, a memory allocation strategy corresponding to each thread in the first application; A configuration file for the first application is generated according to a memory allocation strategy corresponding to each thread in the first application.
5. The method according to claim 3 or 4, characterized in that Determining the memory allocation strategy corresponding to each thread in the first application according to the running result of the test case corresponding to each memory allocation strategy and the application business indicator includes: If the running result does not meet the application business indicator, a corresponding memory allocation strategy is allocated to each thread in the first application until the running result meets the application business indicator, and the current memory allocation strategy corresponding to each thread is determined.
6. The method according to claim 5, characterized in that The running result meets the application business indicator, including: the memory usage corresponding to the at least one thread in the first application is less than or equal to the preset memory usage corresponding to the memory usage, and / or the processor occupancy corresponding to the at least one thread in the first application is less than or equal to the preset occupancy corresponding to the processor occupancy.
7. The method according to any one of claims 2 to 6, characterized in that: The configuration file of the first application includes identification information of the first application, identification information of each thread in the at least one thread, and identification information of a memory allocation strategy corresponding to each thread.
8. The method according to claim 7, characterized in that Different memory allocation strategies correspond to different memory allocation algorithms, and the multiple memory allocation strategies are stored in a registry; The acquiring, according to the configuration file of the first application, a first memory allocation policy corresponding to the first thread includes: Obtaining identification information of a first memory allocation strategy corresponding to the first thread from a configuration file of the first application; The first memory allocation policy is obtained from the registration table according to the identification information of the first memory allocation policy.
9. The method according to any one of claims 1 to 8, characterized in that The first application is an application for realizing an autonomous driving function.
10. An electronic device, characterized in that: The electronic device comprises: a request acquisition module, configured to acquire a first memory request corresponding to a first thread in a first application; the first application includes at least one thread, and the first thread is any one of the at least one thread; a policy selection module, configured to obtain, in response to the first memory request, a first memory allocation policy corresponding to the first thread; the first application corresponds to multiple memory allocation policies, and each of the at least one thread corresponds to any one of the multiple memory allocation policies; A policy execution module is used to allocate memory to the first thread based on the first memory allocation policy during the execution of the first thread.
11. The electronic device according to claim 10, wherein: The policy selection module is specifically configured to obtain a configuration file of the first application in response to the first memory request; the configuration file of the first application is configured to indicate a memory allocation policy corresponding to each thread in the first application; According to the configuration file of the first application, a first memory allocation policy corresponding to the first thread is obtained.
12. The electronic device according to claim 11, wherein: The electronic device further comprises: A file configuration module is used to obtain application business indicators of the first application; the application business indicators are used to indicate the memory occupancy and / or processor occupancy corresponding to the at least one thread running in the first application; based on the application business indicators, a memory allocation policy corresponding to each thread in the first application is configured, and a configuration file for the first application is generated.
13. The electronic device according to claim 12, wherein: The file configuration module is specifically configured to run a test case corresponding to the first application based on each memory allocation strategy of the multiple memory allocation strategies; Obtaining an execution result of the test case corresponding to each memory allocation strategy; the execution result includes a memory usage and / or a processor usage rate corresponding to the at least one thread in the first application under the memory allocation strategy corresponding to the test case; Determine, based on the running results of the test cases corresponding to each memory allocation strategy and the application business indicators, a memory allocation strategy corresponding to each thread in the first application; A configuration file for the first application is generated according to a memory allocation strategy corresponding to each thread in the first application.
14. The electronic device according to claim 12 or 13, characterized in that: The file configuration module is specifically used to allocate a corresponding memory allocation strategy to each thread in the first application if the running result does not meet the application business indicator until the running result meets the application business indicator, and determine the current memory allocation strategy corresponding to each thread.
15. The electronic device according to claim 14, characterized in that The running result meets the application business indicator, including: the memory usage corresponding to the at least one thread in the first application is less than or equal to the preset memory usage corresponding to the memory usage, and / or the processor occupancy corresponding to the at least one thread in the first application is less than or equal to the preset occupancy corresponding to the processor occupancy.
16. The electronic device according to any one of claims 11 to 15, characterized in that: The configuration file of the first application includes identification information of the first application, identification information of each thread in the at least one thread, and identification information of a memory allocation strategy corresponding to each thread.
17. The electronic device according to claim 16, wherein: Different memory allocation strategies correspond to different memory allocation algorithms, and the multiple memory allocation strategies are stored in a registry; The policy selection module is specifically configured to obtain identification information of a first memory allocation policy corresponding to the first thread from a configuration file of the first application; and obtain the first memory allocation policy from the registry based on the identification information of the first memory allocation policy.
18. The electronic device according to any one of claims 10 to 17, characterized in that: The first application is an application for realizing an autonomous driving function.
19. A vehicle, characterized in that: The vehicle comprises: a request acquisition module, configured to acquire a first memory request corresponding to a first thread in a first application; the first application includes at least one thread, and the first thread is any one of the at least one thread; a policy selection module, configured to obtain, in response to the first memory request, a first memory allocation policy corresponding to the first thread; the first application corresponds to multiple memory allocation policies, and each of the at least one thread corresponds to any one of the multiple memory allocation policies; A policy execution module is used to allocate memory to the first thread based on the first memory allocation policy during the execution of the first thread.
20. An electronic device, characterized in that: The electronic device comprises a memory and one or more processors; the memory is coupled to the processor; wherein computer program code is stored in the memory, and the computer program code includes computer instructions, and when the computer instructions are executed by the processor, the electronic device executes the memory allocation method as described in any one of claims 1 to 9.
21. A vehicle, characterized in that: It includes a memory and one or more processors; the memory is coupled to the processor; wherein a computer program code is stored in the memory, and the computer program code includes computer instructions, and when the computer instructions are executed by the processor, the vehicle executes the memory allocation method as described in any one of claims 1 to 9.
22. A computer-readable storage medium, characterized in that The method comprises computer instructions, which, when executed on an electronic device, cause the electronic device to execute the memory allocation method as described in any one of claims 1 to 9; or, when executed on a vehicle, cause the vehicle to execute the memory allocation method as described in any one of claims 1 to 9.
23. A computer program product, characterized in that When the computer program product runs on an electronic device, the electronic device executes the memory allocation method as described in any one of claims 1 to 9; or, when the computer program product runs on a vehicle, the vehicle executes the memory allocation method as described in any one of claims 1 to 9.
Citation Information
Patent Citations
Resource allocation method, storage medium and terminal equipment
CN113535378A
Memory management method and device, electronic equipment and storage medium
CN116680080A
Resource management and control method, electronic equipment and medium
CN117170857A
Quota-based adaptive resource balancing in a scalable heap allocator for multithreaded applications
US20140282589A1