WO2025208243A1 - 内存分配方法、电子设备及车辆 - Google Patents

内存分配方法、电子设备及车辆

Info

Publication number
WO2025208243A1
WO2025208243A1 PCT/CN2024/085094 CN2024085094W WO2025208243A1 WO 2025208243 A1 WO2025208243 A1 WO 2025208243A1 CN 2024085094 W CN2024085094 W CN 2024085094W WO 2025208243 A1 WO2025208243 A1 WO 2025208243A1
Authority
WO
WIPO (PCT)
Prior art keywords
application
thread
memory
memory allocation
electronic device
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
PCT/CN2024/085094
Other languages
English (en)
French (fr)
Inventor
曹建龙
比亚西亚历山德罗
陈凛
廖泽平
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shenzhen Yinwang Intelligent Technology Co Ltd
Original Assignee
Shenzhen Yinwang Intelligent Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Shenzhen Yinwang Intelligent Technology Co Ltd filed Critical Shenzhen Yinwang Intelligent Technology Co Ltd
Priority to PCT/CN2024/085094 priority Critical patent/WO2025208243A1/zh
Priority to CN202480005566.5A priority patent/CN121175663A/zh
Publication of WO2025208243A1 publication Critical patent/WO2025208243A1/zh
Pending legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]

Definitions

  • the present application relates to the field of data processing technology, and in particular to a memory allocation method, electronic equipment, and vehicle.
  • 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.
  • a memory allocation method for example, in an electronic device.
  • 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.
  • 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.
  • 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.
  • the running result can represent the actual memory usage and/or CPU usage of at least one thread
  • 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.
  • the running result can represent an actual value
  • the application business indicator can represent an expected value
  • 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.
  • 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.
  • different memory allocation strategies correspond to different memory allocation algorithms
  • the multiple memory allocation strategies are stored in a registry.
  • the electronic device 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.
  • 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.
  • the first application is an application that implements an autonomous driving function.
  • 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.
  • an electronic device including:
  • 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;
  • 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.
  • 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.
  • 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; obtain the running result of the test case corresponding to each memory allocation strategy; the running result includes the memory allocation corresponding to at least one thread in the first application under the memory allocation strategy corresponding to the test case.
  • Memory occupancy and/or processor occupancy determining the memory allocation strategy corresponding to each thread in the first application according to the running results of the test cases corresponding to each memory allocation strategy and the application business indicators; generating a configuration file for the first application according to the memory allocation strategy corresponding to each thread in the first application.
  • 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.
  • 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.
  • a vehicle comprising:
  • 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;
  • 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;
  • 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.
  • an electronic device 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.
  • a vehicle 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.
  • a computer-readable storage medium 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.
  • a computer program product 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.
  • 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.
  • FIG3 is a schematic diagram of the structure of jemalloc shown in an embodiment of the present application.
  • FIG4 is a first structural diagram of an electronic device according to an embodiment of the present application.
  • FIG6 is a flowchart of a memory allocation method according to an embodiment of the present application.
  • FIG8 is a second flowchart of a memory allocation method according to an embodiment of the present application.
  • FIG9 is a second structural diagram of an electronic device according to an embodiment of the present application.
  • FIG10 is a third structural diagram of an electronic device according to an embodiment of the present application.
  • At least one of the following or similar expressions refers to any combination of these items, including any combination of single or plural items.
  • 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.
  • 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.
  • 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. Specifically, the use of words such as “exemplary” or “for example” is intended to present related concepts in a concrete way for easy understanding.
  • 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.
  • 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.
  • the main arena maintains only one heap. When the main arena's heap runs out of space, it expands via brk.
  • the thread arena may maintain multiple heaps. When the thread arena's heap runs out of space, it allocates memory for a separate, discontinuous heap structure via mmap. ptmalloc can use multiple arenas (heap areas) to reduce lock contention among threads.
  • 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.
  • a bin represents a list of free memory, centrally managing free chunks.
  • OS operating system
  • ptmalloc doesn't immediately return it to the operating system (OS). Instead, it assigns it to ptmalloc's own bin for management. This way, when a subsequent chunk needs to be allocated, it first considers finding a chunk of the appropriate size in the bin and returning it to the thread, avoiding frequent system calls.
  • bins are divided into the following categories: fast bin, unsorted bin, samll bin, large bin, etc.
  • the fast bin is a high-speed buffer for bins.
  • a thread frees a chunk no larger than 64 bytes, it is placed in the fast bin by default.
  • 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.
  • the unsorted bin is a buffer within the bin.
  • a thread frees a chunk larger than 64 bytes or a chunk merged from the fast bin, it is first placed in the unsorted bin. Chunks of any size can be added to the unsorted bin.
  • ptmalloc searches for a suitable free chunk in the unsorted bin.
  • 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.
  • the corresponding bin 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 freed chunk with the free chunk, removing them from their respective linked lists and merging them into a new chunk. The new chunk is added to the front of the unsorted bin linked list.
  • the large bin is a storage area within the bin that holds larger chunks, typically 512 bytes or larger. Each bin within the large bin contains chunks within a given range. Chunks within the large bin 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.
  • 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: the user chunk and the 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.
  • ptmalloc also introduces the concept of thread cache (tcache) to further improve the efficiency of memory allocation and release.
  • the heap area structures corresponding to the above main arena and thread arena can include the following chunks: allocated chunk (chunk in use), free chunk (idle chunk), top chunk, etc.
  • the top chunk is used to handle it. 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.
  • a thread arena needs to maintain multiple heaps, each with its own header, and heap_info represents the header information.
  • the main arena 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 structure information, malloc_state.
  • 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 this malloc_state is stored directly on the heap.
  • the key technologies involved in the ptmalloc related content shown above may include:
  • the autonomous driving system is an embedded real-time operating system and cannot provide large (such as several Hundreds of GB of memory are needed. Furthermore, because the complex autonomous driving algorithms used in autonomous driving systems frequently call memory operations, if the memory allocation algorithm does not properly manage the cache pool, it will cause a surge in memory noise, putting the autonomous driving system at risk of out of memory (OOM) errors, affecting memory usage of other modules and causing functional problems in the autonomous driving system.
  • OOM out of memory
  • the electronic device can obtain the memory request corresponding to the thread and implement the memory allocation method for each thread.
  • the memory allocation method in the embodiment of the present application is described by taking the processing of the first thread as an example.
  • 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.
  • the running result can represent the actual memory usage and/or CPU usage of at least one thread
  • 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.
  • 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.
  • 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 can meet the standards when the thread is actually running.
  • the electronic device 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.
  • the application business indicator i.e., the performance of the electronic device does not meet the standard
  • 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.
  • 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.
  • 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.
  • the application business indicators 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 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.
  • the electronic device when it generates the configuration file of the first application, it may also be based on a machine learning model or a machine learning algorithm, etc.
  • the electronic device when it generates the configuration file of the first application, it may also be based on a machine learning model or a machine learning algorithm, etc.
  • a machine learning model or a machine learning algorithm, etc.
  • 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.
  • 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.
  • the electronic device can call the corresponding strategy and execute it according to the identification information of the first memory allocation strategy.
  • 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.
  • the electronic device can obtain the first memory allocation strategy from the registry according to the identification information of the first memory allocation strategy.
  • the electronic device 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.
  • 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.
  • 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.
  • the configuration file in the above embodiment can be configured by the electronic device and stored locally in the electronic device; It is configured by other devices, and after configuration, it is uploaded to the server by other devices and obtained from the server by electronic devices.
  • the above content takes the memory allocation method applied to electronic devices as an example.
  • the memory allocation method can also be applied to vehicles.
  • 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
  • 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.
  • 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.
  • steps in the method embodiment may be equivalently replaced by other possible steps.
  • some steps in the method embodiment may be optional and may be deleted in certain usage scenarios.
  • other possible steps may be added to the method embodiment. Step.
  • 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 .
  • 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.
  • 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.
  • 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: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), magnetic disk or optical disk and other media that can store program code.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

提供一种内存分配方法、电子设备及车辆,涉及数据处理技术领域,可以基于多个内存分配策略共同对应用中的线程进行内存分配,能让电子设备或者车辆的性能符合应用的业务场景或者业务需求。该方法中,一个应用包括至少一个线程,以及,一个应用对应多个内存分配策略,至少一个线程中的每个线程对应多个内存分配策略中的任一个。也就是说,一个应用中的不同线程都各自对应有一个内存分配策略,不同线程在运行时都能基于合适的内存分配策略获得内存。这样,可以实现为线程动态分配内存分配策略的目的,而不必使所有线程都对应相同的内存分配策略。这样基于多策略共同进行内存分配的方式,能让电子设备或车辆的性能符合应用的业务场景、业务需求等。

Description

内存分配方法、电子设备及车辆 技术领域
本申请涉及数据处理技术领域,尤其涉及一种内存分配方法、电子设备及车辆。
背景技术
电子设备中的应用多通过运行线程的方式实现应用的功能。每个线程在运行时,都要向电子设备申请内存,以保证线程和应用的正常运行。目前,电子设备中多采用ptmalloc、tcmalloc、jemalloc等内存分配算法(或称为内存分配器)中的任一种来对一个应用的所有线程进行内存分配。
然而,这样分配内存后,电子设备的性能却难以匹配应用所对应的业务场景或者业务需求,从而影响用户的体验。
发明内容
本申请实施例提供一种内存分配方法、电子设备及车辆,基于多个内存分配策略共同对应用中的线程进行内存分配,能让电子设备或者车辆的性能符合应用的业务场景或者业务需求。
为达到上述目的,本申请的实施例采用如下技术方案:
第一方面,提供一种内存分配方法,以应用于电子设备为例,在该方法中,电子设备获取第一应用中的第一线程对应的第一内存请求,并响应于第一内存请求,获取第一线程对应的第一内存分配策略。电子设备在第一线程运行的过程中,基于第一内存分配策略,为第一线程分配内存。
上述第一应用包括至少一个线程,第一线程为至少一个线程中的任一个,以及,第一应用对应多个内存分配策略,至少一个线程中的每个线程对应多个内存分配策略中的任一个。也就是说,一个应用中的不同线程都各自对应有一个内存分配策略,不同线程在运行时都能基于合适的内存分配策略获得内存。其中,示例性的,各个线程对应的合适的内存分配策略可以基于第一应用的业务场景、业务需求等进行设置。这样,可以实现为电子设备中的线程动态分配内存分配策略的目的,而不必使所有线程都对应相同的内存分配策略。这样基于多策略共同进行内存分配的方式,能让电子设备的性能符合第一应用的业务场景、业务需求等。
在第一方面的一种可实现方式中,电子设备获取第一线程对应的第一内存分配策略时,可以响应于第一内存请求,获取第一应用的配置文件,其中,第一应用的配置文件用于指示第一应用中的每个线程对应的内存分配策略。之后,根据第一应用的配置文件,获取第一线程对应的第一内存分配策略。
这种实现方式中,当应用中的线程运行时,电子设备可以直接根据配置文件确定出第一线程对应的内存分配策略,而无需对应用或者线程进行一系列的业务指标评价,再根据评价结果选择内存分配策略,从而减少选择内存分配策略的时间,大大加快内存分配的效率。
在第一方面的一种可实现方式中,在获取第一应用中的第一线程对应的第一内存请求之前,电子设备可以获取第一应用的应用业务指标,其中,应用业务指标用于指示运行第一应用中的至少一个线程所对应的内存占用情况和/或处理器占用情况。之后,电子设别基于应用业务指标,配置第一应用中每个线程对应的内存分配策略,并生成第一应用的配置文件。
这种实现方式中,预先为线程配置内存分配策略,并预先为应用生成配置文件,都有利于在后续线程实际运行时,直接根据配置文件确定出线程对应的内存分配策略,从而减少选择内存分配策略的时间,大大加快内存分配的效率。
在第一方面的一种可实现方式中,电子设备生成第一应用的配置文件时,可以基于多个内存分配策略的每个内存分配策略,分别运行第一应用对应的测试用例,以及,获取每个内存分配策略对应的测试用例的运行结果,其中,运行结果包括在测试用例对应的内存分配策略下,第一应用中的至少一个线程所对应的内存占用量和/或处理器占用率。电子设备再根据每个内存分配策略对应的测试用例的运行结果和应用业务指标,分别确定第一应用中每个线程对应的内存分配策略, 以及,根据第一应用中每个线程分别对应的内存分配策略,生成第一应用的配置文件。
这种实现方式中,运行结果可以表示至少一个线程的实际的内存占用量和/或CPU占用率,而应用业务指标则可以表示至少一个线程为了满足应用的业务场景、业务需求等所需满足的指标,也就是说,运行结果可以表示一个实际值,应用业务指标可以表示一个预期值。将实际值与预期值进行对比,可以确定出在每个内存分配策略下,至少一个线程的运行是否能使电子设备的性能达标,并且根据每个内存分配策略对应的达标情况,适应性调整每个线程对应的内存分配策略,使得每个线程基于各自对应的内存分配策略运行时,电子设备的性能达标,如保证电子设备的内存优先、电子设备的CPU性能优先,电子设备的性能均衡等。
此后,生成第一应用的配置文件之后,电子设备直接根据配置文件获取到每个线程对应的内存分配策略并运行,进而保证在线程在真正运行时,电子设备的性能也能达标。
在第一方面的一种可实现方式中,电子设备确定第一应用中每个线程对应的内存分配策略时,若运行结果不满足应用业务指标,电子设备为第一应用中每个线程分配对应的内存分配策略,直至运行结果满足应用业务指标,并确定每个线程当前对应的内存分配策略。
这种实现方式中,可以在文件配置阶段反复地对线程对应的内存分配策略进行测试,从而预先配置出能满足第一应用的业务场景或者业务需求的内存分配策略和配置文件,便于在进程实际运行时,能更加快速和准确地获得内存分配策略,使得内存分配更加准确和快速。
在第一方面的一种可实现方式中,上述运行结果满足应用业务指标,包括:第一应用中的至少一个线程所对应的内存占用量小于或者等于内存占用情况对应的预设内存量,和/或,第一应用中的至少一个线程对应的处理器占用率小于或者等于处理器占用情况对应的预设占用率。
在第一方面的一种可实现方式中,第一应用的配置文件包括第一应用的标识信息、至少一个线程中每个线程的标识信息和每个线程分别对应的内存分配策略的标识信息。
在第一方面的一种可实现方式中,不同的内存分配策略对应不同的内存分配算法,多个内存分配策略保存于注册表中。以及,电子设备获取第一线程对应的第一内存分配策略时,可以从第一应用的配置文件中,获取第一线程对应的第一内存分配策略的标识信息,以及根据第一内存分配策略的标识信息,从注册表中获取第一内存分配策略。
这种实现方式中,注册表可以看作是电子设备中管理内存分配器的一种工具,便于电子设备或者开发人员对电子设备所使用的全部内存分配策略进行扩展和更新。
在第一方面的一种可实现方式中,第一应用为实现自动驾驶功能的应用。这种实现方式中,电子设备可以是车辆中提供数据处理功能的模块或者设备,从而使得针对提供自动驾驶功能的线程,也能保证车辆的性能满足自动驾驶应用的业务场景或者业务需求。
第二方面,提供一种电子设备,包括:
请求获取模块,用于获取第一应用中的第一线程对应的第一内存请求;第一应用包括至少一个线程,第一线程为至少一个线程中的任一个;
策略选择模块,用于响应于第一内存请求,获取第一线程对应的第一内存分配策略;第一应用对应多个内存分配策略,至少一个线程中的每个线程对应多个内存分配策略中的任一个;
策略运行模块,用于在第一线程运行的过程中,基于第一内存分配策略,为第一线程分配内存。
在第二方面的一种可实现方式中,策略选择模块,具体用于响应于第一内存请求,获取第一应用的配置文件;第一应用的配置文件用于指示第一应用中的每个线程对应的内存分配策略;根据第一应用的配置文件,获取第一线程对应的第一内存分配策略。
在第二方面的一种可实现方式中,电子设备还包括:文件配置模块,用于获取第一应用的应用业务指标;应用业务指标用于指示运行第一应用中的至少一个线程所对应的内存占用情况和/或处理器占用情况;基于应用业务指标,配置第一应用中每个线程对应的内存分配策略,并生成第一应用的配置文件。
在第二方面的一种可实现方式中,文件配置模块,具体用于基于多个内存分配策略的每个内存分配策略,分别运行第一应用对应的测试用例;获取每个内存分配策略对应的测试用例的运行结果;运行结果包括在测试用例对应的内存分配策略下,第一应用中的至少一个线程所对应的内 存占用量和/或处理器占用率;根据每个内存分配策略对应的测试用例的运行结果和应用业务指标,分别确定第一应用中每个线程对应的内存分配策略;根据第一应用中每个线程分别对应的内存分配策略,生成第一应用的配置文件。
在第二方面的一种可实现方式中,文件配置模块,具体用于若运行结果不满足应用业务指标,为第一应用中每个线程分配对应的内存分配策略,直至运行结果满足应用业务指标,并确定每个线程当前对应的内存分配策略。
在第二方面的一种可实现方式中,不同的内存分配策略对应不同的内存分配算法,多个内存分配策略保存于注册表中;策略选择模块,具体用于从第一应用的配置文件中,获取第一线程对应的第一内存分配策略的标识信息;根据第一内存分配策略的标识信息,从注册表中获取第一内存分配策略。
第三方面,提供一种车辆,包括:
请求获取模块,用于获取第一应用中的第一线程对应的第一内存请求;第一应用包括至少一个线程,第一线程为至少一个线程中的任一个;
策略选择模块,用于响应于第一内存请求,获取第一线程对应的第一内存分配策略;第一应用对应多个内存分配策略,至少一个线程中的每个线程对应多个内存分配策略中的任一;
策略运行模块,用于在第一线程运行的过程中,基于第一内存分配策略,为第一线程分配内存。
第四方面,提供一种电子设备,包括存储器、一个或多个处理器;存储器与处理器耦合;其中,存储器中存储有计算机程序代码,计算机程序代码包括计算机指令,当计算机指令被处理器执行时,使得电子设备执行如第一方面及其任一种实现方式中的内存分配方法。
第五方面,提供一种车辆,包括存储器、一个或多个处理器;存储器与处理器耦合;其中,存储器中存储有计算机程序代码,计算机程序代码包括计算机指令,当计算机指令被处理器执行时,使得车辆执行如第一方面及其任一种实现方式中的内存分配方法。
第六方面,提供一种计算机可读存储介质,包括计算机指令,当计算机指令在电子设备上运行时,使得电子设备执行如第一方面及其任一种实现方式中内存分配方法;或者,当计算机指令在车辆上运行时,使得车辆执行如第一方面及其任一种实现方式中的内存分配方法。
第七方面,提供一种计算机程序产品,当计算机程序产品在电子设备上运行时,使得电子设备执行如第一方面及其任一种实现方式中的内存分配方法;或者,当计算机程序产品在车辆上运行时,使得车辆执行如第一方面及其任一种实现方式中的内存分配方法。
上述第二方面提供的电子设备、第三方面提供的车辆、第四方面提供的电子设备、第五方面提供的车辆、第六方面提供的计算机可读存储介质和第七方面提供的计算机程序产品,所能达到的有益效果可参考第一方面及其任一种实现方式所能实现的有益效果,此处不再赘述。
附图说明
图1为本申请实施例示出的ptmalloc的结构示意图;
图2为本申请实施例示出的tcmalloc的结构示意图;
图3为本申请实施例示出的jemalloc的结构示意图;
图4为本申请实施例示出的电子设备的结构示意图一;
图5为本申请实施例示出的内存分配方法的实现过程示意图;
图6为本申请实施例示出的内存分配方法的流程图一;
图7为本申请实施例示出的配置文件的示意图;
图8为本申请实施例示出的内存分配方法的流程图二;
图9为本申请实施例示出的电子设备的结构示意图二;
图10为本申请实施例示出的电子设备的结构示意图三。
具体实施方式
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行描述。其中,在本申请的描述中,除非另有说明,“/”表示前后关联的对象是一种“或”的关系,例如,A/B可以表 示A或B;本申请中的“和/或”仅仅是一种描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况,其中A,B可以是单数或者复数。并且,在本申请的描述中,除非另有说明,“多个”是指两个或多于两个。“以下至少一项(个)”或其类似表达,是指的这些项中的任意组合,包括单项(个)或复数项(个)的任意组合。例如,a,b,或c中的至少一项(个),可以表示:a,b,c,a-b,a-c,b-c,或a-b-c,其中a,b,c可以是单个,也可以是多个。另外,为了便于清楚描述本申请实施例的技术方案,在本申请的实施例中,采用了“第一”、“第二”等字样对功能和作用基本相同的相同项或相似项进行区分。本领域技术人员可以理解“第一”、“第二”等字样并不对数量和执行次序进行限定,并且“第一”、“第二”等字样也并不限定一定不同。同时,在本申请实施例中,“示例性的”或者“例如”等词用于表示作例子、例证或说明。本申请实施例中被描述为“示例性的”或者“例如”的任何实施例或设计方案不应被解释为比其它实施例或设计方案更优选或更具优势。确切而言,使用“示例性的”或者“例如”等词旨在以具体方式呈现相关概念,便于理解。
此外,本申请实施例描述的业务场景是为了更加清楚的说明本申请实施例的技术方案,并不构成对于本申请实施例提供的技术方案的限定,本领域普通技术人员可知,随着新业务场景的出现,本申请实施例提供的技术方案对于类似的技术问题,同样适用。
电子设备或者车辆等搭载有操作系统的设备上,通常可以安装各种应用,来为设备提供各种各样的功能。其中,应用通常通过运行进程(进程包括至少一个线程)或者线程来提供对应的功能。并且,每个线程在运行时,都要向设备申请内存,以保证线程和应用的正常运行。
目前,在上述设备中多采用ptmalloc、tcmalloc、jemalloc等内存分配算法中的任一种来对一个设备中运行的所有线程进行内存分配。
一、ptmalloc
ptmalloc支持多线程(thread),每个线程都维护单独的堆(包含空闲内存管理列表),各个线程可以并发的从空闲内存列表中申请内存。参见图1所示,在主线程中维护的连续堆区称为main arena,在子线程中维护的堆区称thread arena。
其中,main arena只会维护一个堆,当当前维护的main arena堆空间不足时,通过brk来扩充;而thread arena可能维护多个堆,thread arena堆空间不足时,通过mmap申请另一段和当前不连续的堆结构的内存。ptmalloc可以使用多个arena(堆区),来减小多线程之间的锁竞争。
并且,chunk是管理堆中内存的最小数据结构,一个堆会被分隔成多个不同种类的chunk,不同种类的chunk通过链表的形式组织在一起又被称为bin。
bin表示空闲内存列表结构,统一管理free chunk。当线程释放chunk后,ptmalloc不会把chunk马上归还给操作系统(operating system,OS),而是将它交给ptmalloc本身的bin管理起来,这样后续需要分配一块chunk时,会首先考虑从bin中寻找一块大小合适的chunk返回给线程,避免频繁的系统调用。
基于chunk的大小,bin分为以下几类:fast bin、unsorted bin、samll bin、large bin等。
其中,fast bin是bin的高速缓冲区,当线程释放一块不大于64B的chunk的时候,会默认会被放到fast bin上。当需要给线程分配的chunk小于或等于64B时,ptmalloc首先会到fast bin上寻找是否有合适的chunk;除非特定情况,两个毗连的空闲chunk并不会被合并成一个空闲chunk。
其中,unsorted bin是bin的一个缓冲区。当线程释放的chunk大于64B或者fast bin合并后的chunk,都会首先放到unsorted bin上。chunk大小无尺寸限制,任何大小chunk都可以添加到unsorted bin。当需要给线程分配chunk时,如果在fast bin和samll bin中没有找到合适的chunk,则ptmalloc会在unsorted bin中查找合适的空闲chunk。
其中,samll bin是bin中保存较小chunk的存储区,通常这个chunk的大小小于512字节。每个small bin都包括一个空闲区块的双向循环链表(也称bin list)。释放掉的chunk添加在链表的前端,而所需的chunk从链表后端摘除。以及,两个毗连的空闲chunk会被合并成一个空闲chunk。
当需要给线程分配chunk时,若samll bin非空,则相应的bin会摘除bin list中最后一个chunk并返回给线程。在释放一个chunk的时候,ptmalloc检查其前或其后的chunk是否空闲,若空闲,则将释放的chunk和空闲的chunk合并,也即把它们从所属的链表中摘除并合并成一个新的chunk,新chunk会添加在unsorted bin链表的前端。
其中,large bin是bin中保存较大chunk的存储区,通常这个chunk的大小大于或者等于512字节。large bin中的每一个bin分别包含了一个给定范围内的chunk,其中的chunk按大小递减排序,大小相同则按照最近使用时间排列。两个毗连的空闲chunk会被合并成一个空闲chunk。
当需要给线程分配chunk时,从large bin的顶部遍历到底部以找到一个大小最接近线程需求的chunk。一旦找到,相应chunk就会分成两块,即user chunk和remainder chunk,其中user chunk返回给线程使用。remainder chunk添加到unsorted bin。释放chunk时的过程与small bin中释放chunk的过程类似。
另外,ptmalloc还引入了线程缓存(thread cache,tcache)的概念,以进一步提高内存分配和释放的效率。
上述main arena和thread arena分别对应的堆区结构中可以包括如下chunk:allocated chunk(正在使用中的chunk)、free chunk(空闲的chunk)、top chunk等。
当所有的bin都无法响应线程的内存分配请求时,就会使用top chunk来响应内存分配请求。当内存分配请求的数据比top chunk大时,top chunk使用brk扩容;请求数据比top chunk小时,在top chunk中分配适合大小的内存返回给线程,剩余部分继续用作top chunk。
此外,一个thread arena需要维护多个堆,每个堆都有自己的堆头,heap_info即堆头信息;而main arena无需维护多个堆,因此无需此结构。每个chunk都有保存自己结构信息malloc_state的malloc_chunk,以及,main arena的malloc_state是作为一个全局变量保存在data区,而对于thread arena来说,它的多个堆共享同一个malloc_state,所以这个malloc_state直接保存在堆中。
上述所示的ptmalloc的相关内容中,涉及的关键技术可以包括:
1、分割和合并chunk:当一个内存分配请求来临时,ptmalloc会在相应的arena中找到一个合适的chunk来满足这个请求,并可能需要调整chunk的大小和状态。当一个chunk被释放时,它会被放回到它所属的arena,并可能会和相邻的空闲chunk合并。
2、使用mmap分配大内存:对于大的内存分配请求,ptmalloc会直接使用mmap()系统调用来从系统获取内存,而不是从arena中分配。
3、延迟合并fast bin:为了提高效率,被放入fast bin的chunk在被再次使用之前不会被合并。
然而,因为后分配的内存先释放,ptmalloc收缩内存是从top chunk开始,如果与top chunk相邻的chunk不能释放,top chunk以下的chunk都无法释放,这样会从其他arena或者额外申请内存,从而导致内存使用率降低。并且,ptmalloc可以支持多线程,但是对多线程却并不友好,频繁分配内存,频繁加锁,会导致锁竞争,内存碎片增高。
其中,内存碎片指的是一个系统中不可使用的内存块。大量的内存碎片可能就会导致整系统的内存底噪上升。而多线程的并发场景会进一步加剧内存碎片的产生,降低了内存的使用效率。
二、tcmalloc
在宏观架构上,tcmalloc分为前端(front-end)、中端(middle-end)和后端(back-end),示例性的,参见图2所示。
其中,front-end是一个每线程的缓存池,提供了快速分配和重分配内存给应用的功能。middle-end负责为front-end填充或回收缓存。back-end负责从os获取或释放内存。
front-end可以采取per-cpu cache(每cpu缓存)模式,也可以采取旧版的per-thread cache(每线程缓存)模式。back-end可以支持hugepage aware page heap(大页面感知页面堆),也可以支持旧版的legacy page heap(传统页面堆)。middle-end包括transfer cache和central free list(中央自由列表)。
在front-end中,thread cache是每个线程各自独立拥有的cache,一个cache包含多个空 闲内存链表(size classes),每一个链表(size-class)都有自己的object,每个object都是大小相同的。
tcmalloc会把0KB-256KB大小的内存划分为86个size-class。在申请小内存时(小于256K),tcmalloc会根据申请的内存大小映射到某个size-class中。比如,申请0到8个字节的大小时,会被映射到size-class1中,分配8个字节大小;申请9到16字节大小时,会被映射到size-class2中,以此类推。如果找不到,则向middle-end请求内存,如果申请的内存块过大(超过256KB),则会越过middle-end直接到back-end申请内存。
middle-end职责是给front-end提供缓存。也就是说,当front-end缓存内存不够用时,从middle-end申请内存。
transfer cache是一个指针数组,可以快速地把前端释放掉的对象(或内存)指针移动到这个数组中,或者从这个数组中获取对象(或者内存)。(可以将这个结构理解为central free list的一层缓存)。
central free list负责管理span(tcmalloc将内存块按页划分、一个或多个页可以组成一个span)。通过emtpy和nonempty两个链表实现对span的管理。且central free list是按照size-class进行划分的,所以只有相同size-class的申请才会有冲突加锁的场景。
central cache是当thread cache内存不足时,提供内存供其使用。它保持的是空闲块链表,链表数量和thread cache数量相同。thread cache中内存过多时,可以放回central cache中。central free list的作用就是从page-heap中取出部分span,然后按照预定大小将其拆分成固定大小的object,提供给thread cache使用。
当front-end和middle-end都没有合适的内存块时,内存分配申请会走到back-end。back-end的主要数据结构是page-heap,page-heap保存的也是若干链表,不过链表保存的是span。span是page-heap中管理内存页的单位,它是由一组连续的page组成,比如2个page组成的span,多个这样的span就用链表来管理。当然,还可以有4个page组成的span等。
page-heap主要有链表和set来实现。链表和set中存储的都是span,由1个page组成的span会被存在第一个链表中,2个page组成的span会被存在第二个链表,以此类推。超过128个page组成的span会被存在set里面。当middle-end无法满足内存分配申请时,会向back-end获取span,然后middle-end负责管理span。
上述所示的tcmalloc的相关内容中,涉及的关键技术可以包括:
1、stacked cache(多层缓存设计):通过多层缓存的机制减少锁冲突的同时,提高内存分配和内部管理的效率。
2、内存碎片化的控制:通过对小内存块划分为86个不同的size-class,并且设计了内存块跨CPU转移的机制,减少碎片化,提升内存使用率。tcmalloc设计的划分size-class的方法能把内存碎片的大小控制在整个page大小的12.5%以内。
然而,在中大型内存频繁分配的场景下,对自旋锁的竞争及激烈,会导致CPU占用率的提升,降低CPU的性能。并且,单线程场景收益不大,反而由于增加了三级缓存池内相关的维护操作,使得CPU的性能更差。针对多线程而言进行内存分配,会增加内存碎片,使得内存底噪变大,内存使用率降低。
三、jemalloc
jemalloc中会涉及arena、tcache、bins、runs、page和chunk等。
其中,arena:jemalloc为了高并发场景下减少锁的冲突,把内存划分为了多个arena,每个线程单独享有一块arena,这样能减少并发场景下锁的开销。
tcache:tcache是一层线程级别的缓存,当有内存分配请求时,会首先走到tcache,如果tcache没有可满足的内存块,才会到arena里面划分。
bin:jemalloc把小内存划分为了多个不同的size-class(与tcmalloc划分的规则类似)。
runs、page和chunk:arena会被划分为多个chunk,chunk默认的大小是4MB,chunk又以page为单位进行管理,每个chunk的前几个page用于存储chunk的元数据,后面跟着一个或者多规格page的runs。多个小对象在一起组成run。在使用某一个chunk的时候,会把它分割成很 多个run,并记录到bin中。不同size的class对应着不同的bin,在bin里,都会有一个红黑树来维护空闲的run,并且在run里,使用了bitmap来记录了分配状态。此外,每个arena里面维护一组按地址排列的可获得的run的红黑树。
上述所示的jemalloc的相关内容中,涉及的关键技术可以包括:
1、arena的划分:jemalloc引入了arena。将内存划分成若干数量的arenas。线程最终会与某一个arena绑定。由于两个arena在地址空间上几乎不存在任何联系,就可以在无锁的状态下完成分配。同样由于空间不连续,落到同一个cache-line中的几率也很小,保证了各自独立。
2、tcache以及small object、large object、huge object的划分:参见图3所示,jemalloc把内存分配申请分为small object(小内存分配申请)、large object(大内存分配申请)以及huge object(较大内存分配申请)三种类型,对于small object和large object会尝试从tcache获取内存块,对于huge object则会直接使用mmap向系统申请系统内存(system memory)。
然而,arena的机制能让内存分配申请在tcache没命中的时候减少锁的竞争,但是也会增加额外的内存底噪。arena的默认数量是cpu个数的四倍,使用tcache的机制同样也是增加了额外的内存损耗,且内存占用量较多。
另外,除了上述所示例的三种内存分配方式各自的缺点以外,这三种内存分配方式在不同方面的处理性能对比还可以参见下表1所示:
表1
其中,jemalloc面向多线程、频繁分配和释放大量内存的场景,多核场景性能表现较好,但是内存底噪高。tcmalloc同样面向多线程,但内存底噪相较jemalloc会低一些。ptmalloc是一个相对基础的内存分配算法,但面向多线程时,CPU性能较差,内存碎片也会增加。
无论哪种内存分配方式,都有其在某一方面或者某几个方面上的缺陷或者短板,如使得电子设备(或车辆)的内存使用量高、电子设备(或车辆)的内存底噪较高、电子设备(或车辆)的CPU占用率高、无法面向多线程场景等。
而目前针对一个应用所使用的内存分配算法都是固定的,如基于电子设备(或车辆)所搭载的操作系统而设置内存分配算法。不同应用由于业务场景、业务需求等不同,运行时也会要求电子设备(或车辆)在某一个或多个方面有较高的性能,而由于内存分配算法本身也有各自的缺陷,又无法根据应用的业务场景或者业务需求进行调整,因此,使用固定的内存分配算法为应用中的线程分配内存后,往往都难以保证电子设备(或车辆)的性能匹配应用所对应的业务场景或者业务需求,从而影响用户的体验。
尤其对于车辆而言,车辆中通常运行有自动驾驶系统。自动驾驶系统是一个利用车辆上的传感器实时感知周围环境变化,做出车辆控制决策的系统。自动驾驶系统的目的是提高交通安全和通行效率,对实时性的要求非常高。
自动驾驶系统一般会每隔一段时间处理一次传感器采集到的数据,并作出车控决策。想要系统的实时性高,就要缩短处理传感器数据的时间间隔,这就要求CPU在处理数据的时候要做到高性能。在处理数据时用到的自动驾驶算法的复杂性造成数据分发节点非常多,内存操作的时间开销很大,影响到在突发事件的场景下(例如,有行人突然闯入)自动驾驶系统响应的实时性,很可能会影响自动驾驶系统的安全。所以自动驾驶系统对内存分配的性能需求也是十分强烈的。
而自动驾驶系统又是一个嵌入式的实时操作系统,不会像一些服务器一样能提供很大(如几 百个GB)的内存。又因为自动驾驶系统上使用到的复杂的自动驾驶算法对内存操作的高频调用,如果内存分配算法对缓存池管理不当,就会导致内存底噪的飙升,让自动驾驶系统出现内存耗尽(out of memory,OOM)之类的风险,影响其他模块对内存的使用,进而导致自动驾驶系统出现功能性问题。
基于上述内容,本申请实施例提供了一种内存分配方法。在该方法中,电子设备可以获取第一应用中的第一线程对应的第一内存请求,并响应于第一内存请求,获取第一线程对应的第一内存分配策略。电子设备在第一线程运行的过程中,基于第一内存分配策略,为第一线程分配内存。
上述第一应用包括至少一个线程,第一线程为至少一个线程中的任一个,以及,第一应用对应多个内存分配策略,至少一个线程中的每个线程对应多个内存分配策略中的任一个。也就是说,一个应用中的不同线程都各自对应有一个内存分配策略,不同线程在运行时都能基于合适的内存分配策略获得内存。其中,示例性的,各个线程对应的合适的内存分配策略可以基于第一应用的业务场景、业务需求等进行设置。这样,可以实现为电子设备中的线程动态分配内存分配策略的目的,而不必使所有线程都对应相同的内存分配策略。这样基于多策略共同进行内存分配的方式,能让电子设备的性能符合第一应用的业务场景、业务需求等。
另外,上述方法也可以应用于车辆中。同样可以使得车辆的性能符合第一应用的业务场景、业务需求等。
可以理解的是,上述内存分配算法还可以作为内存分配器使用,或者称为内存分配器。
以上述方法应用于电子设备为例,参见图4所示,该电子设备可以包括内存分配调度器(arbitrator)、文件管理器(profiler manager)、内存分配管理器(allocator manager)和内存分配器注册表(allocator registry)。
其中,文件管理器可以在应用编译时,根据应用的应用业务指标,使用从内存分配器注册表中选择性能最优的内存分配器,并创建应用对应的配置文件(profile)。
例如,文件管理器中可以保存应用1对应的配置文件1(表示为profile-app1)、应用2对应的配置文件2(表示为profile-app2)和应用3对应的配置为文件3(表示为profile-app3)。
其中,应用业务指标可以是用户在应用编译时针对应用给定的,也可以是为应用预先设置的。另外,由于应用中可以包括至少一个线程,因此应用的应用业务指标,也可以指示运行应用中的至少一个线程所对应的内存占用情况和/或CPU占用情况。进一步的,应用业务指标可以根据应用的业务场景、业务需求等进行给定或者设置,例如,针对应用1而言,要求电子设备的内存性能优先,如内存占用量较少,这样,应用1对应的业务指标可以指示内存占用情况;针对应用2而言,要求电子设备的CPU性能优先,如CPU占用率较低,这样,应用2对应的业务指标可以指示CPU占用情况;针对应用3而言,要求电子设备的性能均衡,如内存占用量较少且CPU占用率较低,这样,应用3对应的业务指标可以指示内存占用情况和CPU占用情况。
在一些可能的实现方式中,应用的配置文件可以至少包括如下信息:
应用的标识信息:用于唯一标识一个应用,具体可以包括应用的名称、版本信息、路径信息等。
线程的标识信息:具体可以包括应用中每个线程的标识、线程的内存需求等。
内存分配模式:用于表示每个线程在运行过程中的内存分配时机和内存释放时机。例如,线程可能会周期性地分配内存、释放内存等,或者可能会在特定事件发生时分配内存、释放内存等。
内存分配器(或内存分配策略)的标识信息:用于表示应用中每个线程对应的内存分配器(或内存分配策略),其中,任意两个线程对应的内存分配器(或内存分配策略)可以相同,也可以不同。进而,应用的配置文件可以对应一个或多个内存分配器(或内存分配策略),或者,一个应用可以对应一个或多个内存分配器(或内存分配策略)。
以及,文件管理器还可以使用机器学习算法或者机器学习模型等,从内存分配器注册表中选择线程对应的内存分配器,其中,机器学习模型可以基于机器学习算法实现。示例性的,文件管理器可以将一个应用的应用业务指标作为机器学习模型的输入,获取机器学习模型的输出,即获得应用的配置文件。
在一些可能的实现方式中,文件管理器可以将上述应用业务指标转化为标准文件,作为机器 学习模型的输入。以及,机器学习模型在生成应用对应的配置文件时,基于输入的标准文件开始运行与应用相关的测试用例,进而给出测试用例在不同内存分配器下的运行结果,如实际的内存占用量、实际的CPU占用率等。以及,机器学习模型根据不同内存分配器下的运行结果和所期望实现的应用业务指标,选择针对每个线程的最合适或者最优的内存分配器。机器学习模型针对一个应用中的每个线程都可以确定出对应的内存分配器,最后,再结合每个线程对应的内存分配器,生成一个应用对应的配置文件。
以及,在一些可能的实现方式中,上述对于机器学习模型可以基于类似于神经网络模型训练的方式进行建立。
本申请实施例中,一个线程对应的内存分配器可以是多个内存分配器中任一种,例如,jemalloc、tcmalloc、ptmalloc、mimalloc中的任一种,一个应用可以对应jemalloc、tcmalloc、ptmalloc、mimalloc等多种内存分配器。
内存分配调度器可以在线程运行时,根据线程所属的应用对应的配置文件,为该线程选择合适的内存分配器。内存分配调度器可以提高内存分配的精确性和效率。
内存分配器注册表可以保存电子设备所有可用的内存分配器资源,供文件管理器在应用编译时选择,以及供内存分配调度器在线程运行时选择。内存分配器注册表是一种内存管理工具,可以方便地扩展和更新内存分配器。
内存分配管理器可以管理所有的内存分配器,具体包括创建内存分配器、初始化内存分配器、配置内存分配器、销毁内存分配器等。内存分配管理器可以看作是一种全局内存管理工具,可以提高内存分配器的使用效率和灵活性。
结合上述图4所示的结构,本申请实施例中的内存分配方法的实现过程可以参见图5所示。其中,机器学习模型(machine learning module)根据输入的应用的应用业务指标,输出应用对应的配置文件。配置文件中每个线程对应的内存分配器(如分配器1、分配器2、分配器3等)由内存分配器注册表进行保存,以及,配置文件中每个线程对应的内存分配器由内存分配管理器进行管理。由线程发起内存请求(或者内存分配请求),内存分配调度器响应于内存请求,根据线程所属应用的配置文件,从内存分配器注册表中获取到线程对应的内存分配器。
其中,内存分配调度器中还可以包括选择模块,用于具体实现根据配置文件中线程对应的内存分配器的标识信息,从内存分配器注册表中获取到线程对应的内存分配器。
可以理解的是,前述所说的应用业务指标所指示的内容仅是一些示例,在其他一些可能的实现方式中,要求电子设备的内存性能优先,还可以具体包括内存底噪较低、内存使用率较低等,本申请实施例中对此不做具体限制。
由上述内容可知,本申请实施例中的内存分配方法中,电子设备中的应用可以包括至少一个线程,应用对应多个内存分配器,应用中的每个线程对应多个内存分配器中的任一个。也就是说,一个应用中的不同线程都各自对应有一个内存分配器,不同线程在运行时都能基于合适的内存分配器获得内存。其中,各个线程对应的合适的内存分配器可以基于应用的业务场景、业务需求等进行设置。这样,可以实现为电子设备中的线程动态分配内存分配器的目的,而不必使所有线程都对应相同的内存分配器。这样基于多分配器共同进行内存分配的方式,能让电子设备的性能符合应用的业务场景或者业务需求,也提高内存管理的效率和性能。
以上述内存分配方法应用于电子设备为例,参见图6所示,该内存分配方法可以包括如下步骤S601-S603。
S601、电子设备获取第一应用中的第一线程对应的第一内存请求。
其中,第一应用为电子设备中的任一个应用,第一应用包括至少一个线程,以及,第一线程为至少一个线程中的任一个。
可以理解的是,针对于电子设备中运行的每个线程,电子设备都可以获取到线程对应的内存请求,以及对每个线程都实施内存分配方法。在步骤S601-S603中,仅以针对第一线程进行处理为例,对本申请实施例中的内存分配方法进行说明。
S602、响应于第一内存请求,电子设备获取第一线程对应的第一内存分配策略。
其中,第一应用可以对应多个内存分配策略,不同的内存分配策略为基于不同的内存分配算 法(或内存分配器)所实现的策略,或者,不同的内存分配策略对应不同的内存分配算法(或者内存分配器),例如对应前述实施例中的jemalloc、tcmalloc、ptmalloc、mimalloc等。以及,第一应用中的每个线程都可以对应多个内存分配策略中的任一个,不同线程对应的内存分配策略可以相同,也可以不同。例如,第一线程对应jemalloc,第二线程对应tcmalloc,或者第一线程对应ptmalloc,第二线程也对应ptmalloc。
本申请实施例中,每个线程都有其各自对应的内存分配策略,例如,第一线程对应第一内存分配策略,第二线程对应第二内存分配策略。从而,在电子设备中,每个线程都基于最适合的内存分配策略运行,从而使得电子设备的性能可以满足第一应用的业务场景或者业务需求,例如具有较高的CPU性能、较少的内存占用量等。
在一些实施例中,线程与内存分配策略之间的对应关系可以是预先配置的。这种情况下,电子设备可以响应于第一内存请求,获取第一线程所属的第一应用的配置文件。其中,第一应用的配置文件用于指示第一应用中的每个线程分别对应的内存分配策略,也就是说,第一线程与第一内存分配策略之间的对应关系保存在第一应用的配置文件中。之后,电子设备根据配置文件,可以获取到第一线程对应的第一内存分配策略。这样,当应用中的线程运行时,电子设备可以直接根据配置文件的对应关系确定出第一线程对应的内存分配策略,而无需对应用或者线程进行一系列的业务指标评价,再根据评价结果选择内存分配策略,从而减少选择内存分配策略的时间,大大加快内存分配的效率。
其中,第一应用的配置文件是预先配置或者生成的。在生成第一应用的配置文件时,电子设备可以获取第一线程所属的第一应用的应用业务指标,其中,应用业务指标可以指示运行第一应用中的至少一个线程所对应的内存占用情况和/或处理器占用情况。
之后,电子设备基于第一应用的应用业务指标,配置第一应用中每个线程对应的内存分配策略,并生成第一应用的配置文件。从而第一应用的配置文件中可以包括每个线程与内存分配策略之间的对应关系。其中,预先为线程配置内存分配策略,并预先为应用生成配置文件,都有利于在后续线程实际运行时,直接根据配置文件确定出线程对应的内存分配策略,从而减少选择内存分配策略的时间,大大加快内存分配的效率。
示例性的,如图7所示的配置文件中,可以保存第一应用的标识信息,如“name”所指示的“my application”,保存第一应用中两个线程分别的标识信息,如“id”所指示的“1”、“2”等,还保存每个线程对应的分别对应的内存分配策略的标识信息,如“allocator”所指示的“allocator1”、“allocator2”等。
以及,在一些可能的实现方式中,图7所示的配置文件中还可以保存每个线程对应的内存需求,如“size”所指示的“1024”、“512”等,数值对应的单位为表示内存大小单位、如bit、byte、KB、MB、GB等;以及,配置文件中还可以保存每个线程分配内存和释放内存的事件信息,如“allocations”所指示的是为线程分配内存,其中的“event”所指示的“on start”表示分配内存的时机。
在一些可能的实现方式中,在生成第一应用的配置文件时,电子设备可以基于应用多个内存分配策略的每个内存分配策略,分别运行第一应用对应的测试用例,并获取每个内存分配策略对应的测试用例的运行结果,其中,运行结果包括在测试用例对应的内存分配策略下,第一应用中的至少一个线程所对应的内存占用量和/或CPU占用率。以及,电子设备根据每个内存分配策略对应的测试用例的运行结果和应用业务指标,分别确定第一应用中每个线程对应的内存分配策略,并根据第一应用中每个线程分别对应的内存分配策略,生成第一应用的配置文件。
其中,运行结果可以表示至少一个线程的实际的内存占用量和/或CPU占用率,而应用业务指标则可以表示至少一个线程为了满足应用的业务场景、业务需求等所需满足的指标,也就是说,运行结果可以表示一个实际值,应用业务指标可以表示一个预期值。将实际值与预期值进行对比,可以确定出在每个内存分配策略下,至少一个线程的运行是否能使电子设备的性能达标,并且根据每个内存分配策略对应的达标情况,适应性调整每个线程对应的内存分配策略,使得每个线程基于各自对应的内存分配策略运行时,电子设备的性能达标,如保证电子设备的内存优先、电子设备的CPU性能优先,电子设备的性能均衡等。
此后,生成第一应用的配置文件之后,电子设备直接根据配置文件获取到每个线程对应的内存分配策略并运行,进而保证在线程在真正运行时,电子设备的性能也能达标。
在一些可能的实现方式中,参见图8所示,电子设备生成第一应用的配置文件时,若上述运行结果(表示为结果1)不满足应用业务指标(即电子设备的性能不达标),则先为每个线程分配一个内存分配策略,之后,基于每个线程当前分别对应的内存分配策略,再次运行第一应用的测试用例,再次获得运行结果(表示为结果2),若此次的结果2满足应用业务指标,则表示电子设备的性能达标,这种情况下,电子设备利用每个线程当前分别对应的内存分配策略,生成第一应用的配置文件;而若是结果2仍不满足应用业务指标,则再次调整每个线程分别对应的内存分配策略,并基于每个线程当前分别对应的内存分配策略,再次运行第一应用的测试用例,再次获得运行结果(表示为结果3),若此次的结果3满足应用业务指标,则表示电子设备的性能达标,这种情况下,电子设备利用每个线程当前分别对应的内存分配策略,生成第一应用的配置文件;而若是结果3仍不满足应用业务指标,则重复上述的过程,直至运行结果满足应用业务指标。
这种生成第一应用的配置文件的方式,可以在文件配置阶段反复地对线程对应的内存分配策略进行测试,从而预先配置出能满足第一应用的业务场景或者业务需求的内存分配策略和配置文件,便于在进程实际运行时,能更加快速和准确地获得内存分配策略,使得内存分配更加准确和快速。
示例性的,上述运行结果满足应用业务指标,可以包括:第一应用中的至少一个线程所对应的内存占用量小于或者等于内存占用情况对应的预设内存量(即使得电子设备的内存优先)、第一应用中的至少一个线程对应的处理器占用率小于或者等于处理器占用情况对应的预设占用率(即使得电子设备的CPU性能优先)、内存占用量小于或者等于预设内存量且处理器占用率小于或者等于预设占用率(即使得电子设备的性能均衡)等。
可以理解的是,为了保证电子设备的性能均衡,可能会牺牲掉一些内存或者CPU性能,即,相比与内存优先而言,性能均衡时的内存占用量也会增多,相比与CPU性能优先而言,性能均衡时的CPU占用率也会增高。基于此,在一些可能的实现方式中,上述电子设备性能均衡时所对应的预设内存量可能会大于内存优先时的预设内存量,而预设占用率可能会大于CPU性能优先时的预设占用率。
在一些可能的实现方式中,电子设备在生成上述第一应用的配置文件时,还可以基于机器学习模型或者机器学习算法等进行,具体可以参见前述实施例。
在一些可能的实现方式中,上述应用业务指标可以在应用编译时,技术人员或者开发人员指定的。本申请实施例中对此不做具体限制。
在一些实施例中,上述第一应用的配置文件中,可能只保存与各个线程分别对应的内存分配策略的标识信息等,获取第一线程对应的第一内存分配策略,也就是获取第一内存分配策略的标识信息,之后,电子设备可以根据第一内存分配策略的标识信息调用到对应的该策略并执行。示例性的,电子设备中还可以保存有注册表(或者前述实施例中的内存分配器注册表),以及,上述多个内存分配策略保存于注册表中。在获取第一线程对应的第一内存分配策略时,电子设备可以根据第一内存分配策略的标识信息,从注册表中获取第一内存分配策略。
以及,电子设备在生成第一应用的配置文件时,电子设备也可以根据每个线程对应的内存分配策略的标识信息,从注册表中获取第一应用中每个线程对应的内存分配策略,并基于每个线程对应的内存分配策略,生成第一应用的配置文件。
上述注册表可以看作是电子设备中管理内存分配器的一种工具,便于电子设备或者开发人员对电子设备所使用的全部内存分配策略进行扩展和更新。
S603、电子设备在第一线程运行的过程中,基于第一内存分配策略,为第一线程分配内存。
可以理解的是,电子设备可以确定出第一应用中每个线程分别对应的内存分配策略,当每个线程都运行时,每个线程都可以基于适合自身的内存分配策略获得内存。并且,在每个线程都运行时,能让电子设备的性能符合第一应用的业务场景或者业务需求,也提高内存管理的效率和性能。
另外,上述实施例中的配置文件可以是由电子设备配置的,并保存在电子设备本地;也可以 是由其他设备配置的,配置好之后,由其他设备上传到服务器中,并由电子设备从服务器中获取。本申请实施例中对配置文件生成位置以及获取方式不做具体限制。
前述内容以内存分配方法应用于电子设备为例,在其他一些实施例中,内存分配方法还可以应用于车辆中。具体实施例方法可以参考前述实施例的内容。
以及,以上述方法应用于某车型的车辆为例,针对该车辆中的自动驾驶应用(或者自动驾驶系统)而言,基于该方法运行自动驾驶应用的过程可以如下所述。
其中,表2示例性的示出了自动驾驶应用中部分线程分别基于tcmalloc和jemalloc运行后获得的运行结果,其中的CPU占用率表示jemalloc与tcmalloc相比,CPU占用率减少的情况,内存占用情况表示jemalloc与tcmalloc相比,内存占用量减少的情况。表3示例性的示出了自动驾驶应用中的线程基于tcmalloc和jemalloc分别运行后,车辆系统整体的性能对比情况。
表2
表3
结合表1中的结果可知,针对线程1,jemalloc让线程1对应CPU占用率减少了2.95%,但是多用了101MB的内存。针对线程2,jemalloc让CPU占用率减少了1.72%,同时还让少用了21MB的内存。从车辆系统整体来看,jemalloc对比tcmalloc多使用了700MB的内存,但是让系统整体的CPU占用率减少了1.3%。
基于上述内容,车辆可以分别从内存优先、CPU性能优先、性能均衡等方面考虑,针对自动驾驶应用中的每个线程分配配置对应的内存分配策略,从而生成自动驾驶应用分别对应的保证内存优先的配置文件1、保证CPU性能优先的配置文件2、保证性能均衡的配置文件3,参见下表4所示。
表4
其中,配置文件1可以保证车辆的内存优先,实现车辆整系统CPU占用率上升0.5%,内存使用量下降200MB。配置文件2可以保证车辆的CPU性能优先,实现车辆整系统内存使用量上升50MB,cpu占用率下降3%。配置文件3可以保证车辆的性能均衡,实现车辆整系统内存使用量下降20MB,cpu占用率下降0.5%。
车辆真实保存或者获取的配置文件,可以根据自动驾驶应用所对应的应用业务指标(如要求内存优先、CPU性能优先、性能均衡等)来确定的。例如,自动驾驶应用所对应的应用业务指标要求内存优先,那么车辆会保存配置文件1,此后,当自动驾驶应用中的线程运行时,车辆会根据配置文件1,确定线程对应的内存分配策略,进而线程运行时,使得车辆的内存占用量比较小。
在一些方案中,可以对本申请的多个实施例进行组合,并实施组合后的方案。可选的,各方法实施例的流程中的一些操作任选地被组合,并且/或者一些操作的顺序任选地被改变。并且,各流程的步骤之间的执行顺序仅是示例性的,并不构成对步骤之间执行顺序的限制,各步骤之间还可以是其他执行顺序。并非旨在表明所述执行次序是可以执行这些操作的唯一次序。本领域的普通技术人员会想到多种方式来对本申请实施例所描述的操作进行重新排序。另外,应当指出的是,本申请某个实施例涉及的过程细节同样以类似的方式适用于其他实施例,或者,不同实施例之间可以组合使用。
此外,方法实施例中的某些步骤可等效替换成其他可能的步骤。或者,方法实施例中的某些步骤可以是可选的,在某些使用场景中可以删除。或者,可以在方法实施例中增加其他可能的步 骤。
并且,各方法实施例之间可以单独实施,或结合起来实施。
可以理解的是,为了实现上述功能,前述电子设备或者车辆包含了执行各个功能相应的硬件和/或软件模块。结合本文中所公开的实施例描述的各示例的算法步骤,本申请能够以硬件或硬件和计算机软件的结合形式来实现。某个功能究竟以硬件还是计算机软件驱动硬件的方式来执行,取决于技术方案的特定应用和设计约束条件。本领域技术人员可以结合实施例对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本申请的范围。
本实施例可以根据上述方法示例对电子设备或者车辆进行功能模块的划分,例如,可以对应各个功能划分各个功能模块,也可以将两个或两个以上的功能集成在一个处理模块中。上述集成的模块可以采用硬件的形式实现。需要说明的是,本实施例中对模块的划分是示意性的,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式。
本申请实施例提供了一种电子设备,如图9所示,该电子设备可以包括请求获取模块901、策略选择模块902、策略运行模块903。
其中,请求获取模块901,用于获取第一应用中的第一线程对应的第一内存请求;所述第一应用包括至少一个线程,所述第一线程为所述至少一个线程中的任一个。例如实现上述步骤S601的相关内容。
策略选择模块902,用于响应于所述第一内存请求,获取所述第一线程对应的第一内存分配策略;所述第一应用对应多个内存分配策略,所述至少一个线程中的每个线程对应所述多个内存分配策略中的任一个。例如实现上述步骤S602的相关内容。
策略运行模块903,用于在所述第一线程运行的过程中,基于所述第一内存分配策略,为所述第一线程分配内存。例如实现上述步骤S603的相关内容。
本申请实施例还提供一种车辆,该车辆也可以包括请求获取模块、策略选择模块、策略运行模块等。以及,这些模块的作用可以参考上述实施例,此处不再赘述。
本申请实施例还提供一种电子设备,参见图10所示,该电子设备可以包括一个或者多个处理器1001、存储器1002和通信接口1003。
其中,存储器1002、通信接口1003与处理器1001耦合。例如,存储器1002、通信接口1003与处理器1001可以通过总线1004耦合在一起。
其中,通信接口1003用于与其他设备进行数据传输。存储器1002中存储有计算机程序代码。计算机程序代码包括计算机指令,当计算机指令被处理器1001执行时,使得电子设备执行本申请实施例中的内存分配方法。
其中,处理器1001可以是处理器或控制器,例如可以是中央处理器(Central Processing Unit,CPU),通用处理器,数字信号处理器(Digital Signal Processor,DSP),专用集成电路(Application-Specific Integrated Circuit,ASIC),现场可编程门阵列(Field Programmable Gate Array,FPGA)或者其他可编程逻辑器件、晶体管逻辑器件、硬件部件或者其任意组合。其可以实现或执行结合本公开内容所描述的各种示例性的逻辑方框,模块和电路。所述处理器也可以是实现计算功能的组合,例如包含一个或多个微处理器组合,DSP和微处理器的组合等等。
其中,总线1004可以是外设部件互连标准(Peripheral Component Interconnect,PCI)总线或扩展工业标准结构(Extended Industry Standard Architecture,EISA)总线等。上述总线1004可以分为地址总线、数据总线、控制总线等。为便于表示,图10中仅用一条粗线表示,但并不表示仅有一根总线或一种类型的总线。
本申请实施例还提供一种车辆,可以包括如上图10所示的处理器、存储器、通信接口等,并且结构与上述图10所示相同,以及,处理器、存储器、通信接口的作用可以参考前述实施例,此处不再赘述。
本申请实施例还提供一种计算机可读存储介质,该计算机可读存储介质包括计算机指令,当计算机指令在电子设备或者车辆上运行时,电子设备或者车辆执行上述方法实施例中的相关方法步骤。
本申请实施例还提供了一种计算机程序产品,当该计算机程序产品在计算机上运行时,使得 计算机执行上述方法实施例中的相关方法步骤。
其中,本申请提供的电子设备、车辆、计算机可读存储介质或者计算机程序产品均用于执行上文所提供的对应的方法,因此,其所能达到的有益效果可参考上文所提供的对应的方法中的有益效果,此处不再赘述。
通过以上实施方式的描述,所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,仅以上述各功能模块的划分进行举例说明,实际应用中,可以根据需要而将上述功能分配由不同的功能模块完成,即将装置的内部结构划分成不同的功能模块,以完成以上描述的全部或者部分功能。
在本申请所提供的几个实施例中,应该理解到,所揭露的装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述模块或单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个装置,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。
所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是一个物理单元或多个物理单元,即可以位于一个地方,或者也可以分布到多个不同地方。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。
另外,在本申请各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。
所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个可读取存储介质中。基于这样的理解,本申请实施例的技术方案本质上或者说做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该软件产品存储在一个存储介质中,包括若干指令用以使得一个设备(可以是单片机,芯片等)或处理器(processor)执行本申请各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(read only memory,ROM)、随机存取存储器(random access memory,RAM)、磁碟或者光盘等各种可以存储程序代码的介质。
以上内容,仅为本申请的具体实施方式,但本申请的保护范围并不局限于此,任何在本申请揭露的技术范围内的变化或替换,都应涵盖在本申请的保护范围之内。因此,本申请的保护范围应以所述权利要求的保护范围为准。

Claims (23)

  1. 一种内存分配方法,其特征在于,所述方法包括:
    获取第一应用中的第一线程对应的第一内存请求;所述第一应用包括至少一个线程,所述第一线程为所述至少一个线程中的任一个;
    响应于所述第一内存请求,获取所述第一线程对应的第一内存分配策略;所述第一应用对应多个内存分配策略,所述至少一个线程中的每个线程对应所述多个内存分配策略中的任一个;
    在所述第一线程运行的过程中,基于所述第一内存分配策略,为所述第一线程分配内存。
  2. 根据权利要求1所述的方法,其特征在于,所述响应于所述第一内存请求,获取所述第一线程对应的第一内存分配策略,包括:
    响应于所述第一内存请求,获取所述第一应用的配置文件;所述第一应用的配置文件用于指示所述第一应用中的每个线程对应的内存分配策略;
    根据所述第一应用的配置文件,获取所述第一线程对应的第一内存分配策略。
  3. 根据权利要求2所述的方法,其特征在于,在获取第一应用中的第一线程对应的第一内存请求之前,所述方法还包括:
    获取所述第一应用的应用业务指标;所述应用业务指标用于指示运行所述第一应用中的所述至少一个线程所对应的内存占用情况和/或处理器占用情况;
    基于所述应用业务指标,配置所述第一应用中每个线程对应的内存分配策略,并生成所述第一应用的配置文件。
  4. 根据权利要求3所述的方法,其特征在于,所述基于所述应用业务指标,配置所述第一应用中每个线程对应的内存分配策略,并生成所述第一应用的配置文件,包括:
    基于所述多个内存分配策略的每个内存分配策略,分别运行所述第一应用对应的测试用例;
    获取每个内存分配策略对应的所述测试用例的运行结果;所述运行结果包括在所述测试用例对应的内存分配策略下,所述第一应用中的所述至少一个线程所对应的内存占用量和/或处理器占用率;
    根据每个内存分配策略对应的所述测试用例的所述运行结果和所述应用业务指标,分别确定所述第一应用中每个线程对应的内存分配策略;
    根据所述第一应用中每个线程分别对应的内存分配策略,生成所述第一应用的配置文件。
  5. 根据权利要求3或4所述的方法,其特征在于,所述根据每个内存分配策略对应的所述测试用例的所述运行结果和所述应用业务指标,分别确定所述第一应用中每个线程对应的内存分配策略,包括:
    若所述运行结果不满足所述应用业务指标,为所述第一应用中每个线程分配对应的内存分配策略,直至所述运行结果满足所述应用业务指标,并确定每个线程当前对应的内存分配策略。
  6. 根据权利要求5所述的方法,其特征在于,所述运行结果满足所述应用业务指标,包括:所述第一应用中的所述至少一个线程所对应的内存占用量小于或者等于所述内存占用情况对应的预设内存量,和/或,所述第一应用中的所述至少一个线程对应的处理器占用率小于或者等于所述处理器占用情况对应的预设占用率。
  7. 根据权利要求2-6任一项所述的方法,其特征在于,所述第一应用的配置文件包括所述第一应用的标识信息、所述至少一个线程中每个线程的标识信息和每个线程分别对应的内存分配策略的标识信息。
  8. 根据权利要求7所述的方法,其特征在于,不同的内存分配策略对应不同的内存分配算法,所述多个内存分配策略保存于注册表中;所述根据所述第一应用的配置文件,获取所述第一线程对应的第一内存分配策略,包括:
    从所述第一应用的配置文件中,获取所述第一线程对应的第一内存分配策略的标识信息;
    根据所述第一内存分配策略的标识信息,从所述注册表中获取所述第一内存分配策略。
  9. 根据权利要求1-8任一项所述的方法,其特征在于,所述第一应用为实现自动驾驶功能的应用。
  10. 一种电子设备,其特征在于,所述电子设备包括:
    请求获取模块,用于获取第一应用中的第一线程对应的第一内存请求;所述第一应用包括至少一个线程,所述第一线程为所述至少一个线程中的任一个;
    策略选择模块,用于响应于所述第一内存请求,获取所述第一线程对应的第一内存分配策略;所述第一应用对应多个内存分配策略,所述至少一个线程中的每个线程对应所述多个内存分配策略中的任一个;
    策略运行模块,用于在所述第一线程运行的过程中,基于所述第一内存分配策略,为所述第一线程分配内存。
  11. 根据权利要求10所述的电子设备,其特征在于,
    所述策略选择模块,具体用于响应于所述第一内存请求,获取所述第一应用的配置文件;所述第一应用的配置文件用于指示所述第一应用中的每个线程对应的内存分配策略;根据所述第一应用的配置文件,获取所述第一线程对应的第一内存分配策略。
  12. 根据权利要求11所述的电子设备,其特征在于,所述电子设备还包括:
    文件配置模块,用于获取所述第一应用的应用业务指标;所述应用业务指标用于指示运行所述第一应用中的所述至少一个线程所对应的内存占用情况和/或处理器占用情况;基于所述应用业务指标,配置所述第一应用中每个线程对应的内存分配策略,并生成所述第一应用的配置文件。
  13. 根据权利要求12所述的电子设备,其特征在于,所述文件配置模块,具体用于基于所述多个内存分配策略的每个内存分配策略,分别运行所述第一应用对应的测试用例;获取每个内存分配策略对应的所述测试用例的运行结果;所述运行结果包括在所述测试用例对应的内存分配策略下,所述第一应用中的所述至少一个线程所对应的内存占用量和/或处理器占用率;根据每个内存分配策略对应的所述测试用例的所述运行结果和所述应用业务指标,分别确定所述第一应用中每个线程对应的内存分配策略;根据所述第一应用中每个线程分别对应的内存分配策略,生成所述第一应用的配置文件。
  14. 根据权利要求12或13所述的电子设备,其特征在于,所述文件配置模块,具体用于若所述运行结果不满足所述应用业务指标,为所述第一应用中每个线程分配对应的内存分配策略,直至所述运行结果满足所述应用业务指标,并确定每个线程当前对应的内存分配策略。
  15. 根据权利要求14所述的电子设备,其特征在于,所述运行结果满足所述应用业务指标,包括:所述第一应用中的所述至少一个线程所对应的内存占用量小于或者等于所述内存占用情况对应的预设内存量,和/或,所述第一应用中的所述至少一个线程对应的处理器占用率小于或者等于所述处理器占用情况对应的预设占用率。
  16. 根据权利要求11-15任一项所述的电子设备,其特征在于,所述第一应用的配置文件包括所述第一应用的标识信息、所述至少一个线程中每个线程的标识信息和每个线程分别对应的内存分配策略的标识信息。
  17. 根据权利要求16所述的电子设备,其特征在于,不同的内存分配策略对应不同的内存分配算法,所述多个内存分配策略保存于注册表中;
    所述策略选择模块,具体用于从所述第一应用的配置文件中,获取所述第一线程对应的第一内存分配策略的标识信息;根据所述第一内存分配策略的标识信息,从所述注册表中获取所述第一内存分配策略。
  18. 根据权利要求10-17任一项所述的电子设备,其特征在于,所述第一应用为实现自动驾驶功能的应用。
  19. 一种车辆,其特征在于,所述车辆包括:
    请求获取模块,用于获取第一应用中的第一线程对应的第一内存请求;所述第一应用包括至少一个线程,所述第一线程为所述至少一个线程中的任一个;
    策略选择模块,用于响应于所述第一内存请求,获取所述第一线程对应的第一内存分配策略;所述第一应用对应多个内存分配策略,所述至少一个线程中的每个线程对应所述多个内存分配策略中的任一;
    策略运行模块,用于在所述第一线程运行的过程中,基于所述第一内存分配策略,为所述第一线程分配内存。
  20. 一种电子设备,其特征在于,包括存储器、一个或多个处理器;所述存储器与所述处理器耦合;其中,所述存储器中存储有计算机程序代码,所述计算机程序代码包括计算机指令,当所述计算机指令被所述处理器执行时,使得所述电子设备执行如权利要求1-9任一项中所述的内存分配方法。
  21. 一种车辆,其特征在于,包括存储器、一个或多个处理器;所述存储器与所述处理器耦合;其中,所述存储器中存储有计算机程序代码,所述计算机程序代码包括计算机指令,当所述计算机指令被所述处理器执行时,使得所述车辆执行如权利要求1-9任一项中所述的内存分配方法。
  22. 一种计算机可读存储介质,其特征在于,包括计算机指令,当所述计算机指令在电子设备上运行时,使得所述电子设备执行如权利要求1-9任一项中所述的内存分配方法;或者,当所述计算机指令在车辆上运行时,使得所述车辆执行如权利要求1-9任一项中所述的内存分配方法。
  23. 一种计算机程序产品,其特征在于,当所述计算机程序产品在电子设备上运行时,使得所述电子设备执行如权利要求1-9任一项中所述的内存分配方法;或者,当所述计算机程序产品在车辆上运行时,使得所述车辆执行如权利要求1-9任一项中所述的内存分配方法。
PCT/CN2024/085094 2024-03-30 2024-03-30 内存分配方法、电子设备及车辆 Pending WO2025208243A1 (zh)

Priority Applications (2)

Application Number Priority Date Filing Date Title
PCT/CN2024/085094 WO2025208243A1 (zh) 2024-03-30 2024-03-30 内存分配方法、电子设备及车辆
CN202480005566.5A CN121175663A (zh) 2024-03-30 2024-03-30 内存分配方法、电子设备及车辆

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
PCT/CN2024/085094 WO2025208243A1 (zh) 2024-03-30 2024-03-30 内存分配方法、电子设备及车辆

Publications (1)

Publication Number Publication Date
WO2025208243A1 true WO2025208243A1 (zh) 2025-10-09

Family

ID=97265754

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2024/085094 Pending WO2025208243A1 (zh) 2024-03-30 2024-03-30 内存分配方法、电子设备及车辆

Country Status (2)

Country Link
CN (1) CN121175663A (zh)
WO (1) WO2025208243A1 (zh)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20140282589A1 (en) * 2013-03-13 2014-09-18 Samsung Electronics Company, Ltd. Quota-based adaptive resource balancing in a scalable heap allocator for multithreaded applications
CN113535378A (zh) * 2020-04-20 2021-10-22 深圳Tcl数字技术有限公司 一种资源调配方法、存储介质及终端设备
CN116680080A (zh) * 2023-06-19 2023-09-01 武汉达梦数据库股份有限公司 一种内存管理方法、装置、电子设备和存储介质
CN117170857A (zh) * 2022-05-27 2023-12-05 华为技术有限公司 一种资源管控方法、电子设备及介质

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20140282589A1 (en) * 2013-03-13 2014-09-18 Samsung Electronics Company, Ltd. Quota-based adaptive resource balancing in a scalable heap allocator for multithreaded applications
CN113535378A (zh) * 2020-04-20 2021-10-22 深圳Tcl数字技术有限公司 一种资源调配方法、存储介质及终端设备
CN117170857A (zh) * 2022-05-27 2023-12-05 华为技术有限公司 一种资源管控方法、电子设备及介质
CN116680080A (zh) * 2023-06-19 2023-09-01 武汉达梦数据库股份有限公司 一种内存管理方法、装置、电子设备和存储介质

Also Published As

Publication number Publication date
CN121175663A (zh) 2025-12-19

Similar Documents

Publication Publication Date Title
US12045515B2 (en) Memory system and control method
CN110120940B (zh) 一种面向Docker容器的文件系统资源隔离方法
US9122575B2 (en) Processing system having memory partitioning
US9430388B2 (en) Scheduler, multi-core processor system, and scheduling method
WO2023116910A1 (zh) 一种计算资源和缓存资源调度方法、装置及系统
JPH10254756A (ja) リファレンスされたオブジェクトを管理するための3状態リファレンスの使用
US10049035B1 (en) Stream memory management unit (SMMU)
WO2024099448A1 (zh) 内存释放、内存恢复方法、装置、计算机设备及存储介质
US8954969B2 (en) File system object node management
CN118210622B (zh) 一种内存分配方法及计算设备
CN114924999B (zh) 一种高速缓存管理方法、装置、系统、设备及介质
US20060218557A1 (en) Method and apparatus for switching between per-thread and per-processor resource pools in multi-threaded programs
CN106537321B (zh) 存取文件的方法、装置和存储系统
CN107291371B (zh) 一种读写锁的实现方法及装置
Deligiannis et al. Adaptive memory management scheme for MMU-less embedded systems
CN110447019B (zh) 存储器分配管理器及由其执行的用于管理存储器分配的方法
CN120407201B (zh) 内存资源的分配方法、装置、电子设备、介质及产品
WO2023029982A1 (zh) 一种内存的分配方法及系统
WO2025208243A1 (zh) 内存分配方法、电子设备及车辆
US20090320036A1 (en) File System Object Node Management
CN111125070A (zh) 一种数据交换方法及平台
US20260003779A1 (en) Storage controller and operation method thereof
CN119781964A (zh) 一种内存分配方法、装置、电子设备和存储介质
CN115729466A (zh) 一种保障zns-ssd访问公平性的缓存管理方法
CN121785540A (zh) Kv虚拟地址确定方法及电子设备

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 24933207

Country of ref document: EP

Kind code of ref document: A1