Memory allocation method, apparatus, and electronic device
By pre-configuring contiguous memory regions and a memory reclamation mechanism, the problem of low memory allocation efficiency is solved, enabling fast allocation of large blocks of memory and improving application startup speed.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
- Filing Date
- 2022-08-18
- Publication Date
- 2026-05-05
AI Technical Summary
Existing memory allocation methods still need to be improved in terms of allocation efficiency, especially when large blocks of memory need to be allocated, which may require memory consolidation, leading to reduced efficiency.
By pre-configuring a first memory region with contiguous addresses, memory is allocated directly from this region in response to the application's memory requirements. This leverages the contiguous address characteristic to quickly meet the demand, and performs memory reclamation and cache optimization when insufficient.
It improves the speed of large memory allocation, reduces the need for memory consolidation, and enhances memory allocation efficiency, especially in terms of performance during application startup.
Smart Images

Figure CN115421908B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a memory allocation method, apparatus, and electronic device. Background Technology
[0002] Main memory, also known as RAM, is the storage space that the CPU can directly address, and it is made of semiconductor devices. Applications in electronic devices typically rely on main memory to process data during operation. However, the efficiency of memory allocation methods still needs improvement. Summary of the Invention
[0003] In view of the above problems, this application proposes a memory allocation method, apparatus, and electronic device to improve the above problems.
[0004] In a first aspect, this application provides a memory allocation method, characterized in that the method includes: in response to an instruction to allocate memory to an application, obtaining the memory requirement corresponding to the instruction; if the remaining memory in a pre-configured first memory region meets the memory requirement, obtaining memory from the remaining memory and allocating it to the application; wherein the addresses of the memory included in the first memory region are contiguous.
[0005] Secondly, this application provides a memory allocation apparatus, characterized in that the apparatus includes: an allocation requirement determination unit, configured to obtain the memory requirement corresponding to an instruction to allocate memory for an application; and a memory allocation unit, configured to obtain memory from the remaining memory in a pre-configured first memory region and allocate it to the application if the remaining memory in the first memory region satisfies the memory requirement; wherein the addresses of the memory included in the first memory region are contiguous.
[0006] Thirdly, this application provides an electronic device including one or more processors and a memory; one or more programs are stored in the memory and configured to be executed by the one or more processors to implement the above-described method.
[0007] Fourthly, this application provides a computer-readable storage medium storing program code, wherein the above-described method is executed when the program code is run by a processor.
[0008] This application provides a memory allocation method, apparatus, and electronic device. In response to an instruction to allocate memory to an application, after obtaining the memory requirement corresponding to the instruction, if the remaining memory in a pre-configured first memory region containing contiguous memory addresses satisfies the memory requirement, memory can be allocated to the application from the remaining memory. Thus, by using a pre-configured first memory region with contiguous addresses, when memory needs to be allocated to an application, memory can be directly selected from this first memory region for allocation. This leverages the contiguous address characteristics of the memory in the first memory region to allocate the required memory to the application more quickly. Attached Figure Description
[0009] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0010] Figure 1 A flowchart of a memory allocation method proposed in an embodiment of this application is shown;
[0011] Figure 2 A schematic diagram of the first memory region in an embodiment of this application is shown;
[0012] Figure 3 A flowchart of a memory allocation method according to another embodiment of this application is shown;
[0013] Figure 4 This illustration shows a schematic diagram of selecting memory from a first memory region and allocating it to an application, according to an embodiment of this application.
[0014] Figure 5 A flowchart of a memory allocation method according to yet another embodiment of this application is shown;
[0015] Figure 6 A flowchart of a memory allocation method according to yet another embodiment of this application is shown;
[0016] Figure 7 A structural block diagram of a memory allocation device according to an embodiment of this application is shown;
[0017] Figure 8 This invention illustrates a structural block diagram of an electronic device for performing a memory allocation method according to an embodiment of the present invention.
[0018] Figure 9This application shows a storage unit for storing or carrying program code that implements the memory allocation method according to the embodiments of this application. Detailed Implementation
[0019] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0020] Main memory, also known as RAM, is the storage space that the CPU can directly address, and it is made of semiconductor devices. Applications in electronic devices typically rely on main memory to process data during operation. However, the efficiency of memory allocation methods still needs improvement. For example, in situations where memory fragmentation is severe in electronic devices, allocating large blocks of memory (greater than 64KB) may require memory consolidation before providing contiguous blocks to the required programs, thus impacting memory allocation efficiency.
[0021] Therefore, the inventors have proposed the memory allocation method, apparatus, and electronic device of this application. In response to an instruction to allocate memory to an application, after obtaining the memory requirement corresponding to the instruction, memory can be allocated to the application from the remaining memory in a pre-configured, address-contiguous first memory region if the memory requirement is met. Thus, by using the pre-configured address-contiguous first memory region, when memory needs to be allocated to an application, memory can be directly selected from this first memory region for allocation to the application. This leverages the address-contiguous nature of the memory in the first memory region to allocate the required memory to the application more quickly.
[0022] The embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0023] Please see Figure 1 This application provides a memory allocation method, the method comprising:
[0024] S110: In response to an instruction to allocate memory for an application, obtain the memory requirement corresponding to the instruction.
[0025] Applications require memory to complete their tasks during runtime. Electronic devices can allocate memory to applications at various stages. For example, an electronic device can allocate the necessary memory when an application starts up, or it can allocate memory during the application's runtime. In this case, when the electronic device detects the application's startup, it can trigger a memory allocation instruction and respond to that instruction to obtain the corresponding memory requirement. Similarly, the electronic device can also trigger a memory allocation instruction and respond to a request from the application during runtime to further increase the allocated memory.
[0026] The memory requirement corresponding to the instruction can be understood as the memory requirement of the application that needs the memory. For example, if an electronic device detects that application A needs to allocate 100MB of memory when it starts, then the memory requirement of that application is 100MB. Correspondingly, the memory requirement carried in the instruction generated by the electronic device to allocate memory for that application will also be 100MB.
[0027] S120: If the remaining memory in the pre-configured first memory region meets the memory requirements, obtain memory from the remaining memory and allocate it to the application; wherein the addresses of the memory included in the first memory region are contiguous.
[0028] It should be noted that, in this embodiment of the application, a first memory region can be pre-configured in the electronic device, and the memory addresses in this first memory region are contiguous. Here, contiguous addresses can be understood as memory addresses in the first memory region being arranged adjacently together across all memory regions. For example, as shown... Figure 2 As shown, in the memory of an electronic device, the addresses of the memory in the pre-established first memory region include memory address D1, memory address D2, memory address D3, memory address D4, memory address D5, and memory address D6. Figure 2 As shown, memory addresses D1, D2, D3, D4, D5, and D6 are arranged consecutively in memory.
[0029] Understandably, with a pre-configured first memory region, the memory of an electronic device can be divided into the first memory region and regions outside the first memory region. In this case, after obtaining the application's memory requirements, it can first check whether the remaining memory in the first memory region meets the memory requirements. If the remaining memory in the first memory region meets the memory requirements, then memory can be selected from the first memory region and allocated to the application.
[0030] The statement that the remaining memory in the first memory region meets the memory requirement includes the statement that the remaining memory in the first memory region is not less than the memory requirement. For example, if the memory requirement is 50MB and the remaining memory in the first memory region is 60MB, then the remaining memory in the first memory region meets the memory requirement. However, if the memory requirement is 50MB and the remaining memory in the first memory region is 40MB, then the remaining memory in the first memory region does not meet the memory requirement.
[0031] It should be noted that the pre-established first memory region is a region allocated from the memory of the electronic device. In this case, to ensure that this first memory region can be used more for allocating large blocks of memory (e.g., more than 64KB), one approach is to, before allocating memory from the remaining memory to the application if the remaining memory in the pre-configured first memory region meets the memory requirement, further include: detecting whether the memory requirement is greater than a requirement threshold; if the memory requirement is greater than the requirement threshold, then if the remaining memory in the pre-configured first memory region meets the memory requirement, then allocating memory from the remaining memory to the application; if the memory requirement is not greater than the requirement threshold, then allocating memory to the application from the free memory. Here, free memory can be understood as free memory in memory regions outside the first memory region. For example, the requirement threshold can be 64KB. In this case, if the memory requirement of the application currently allocating memory is detected to be 64KB, then it is determined that memory can be selected from the first memory region to allocate to the application. If the memory requirement of the application currently allocating memory is detected to be 4KB, then memory can be selected from memory regions outside the first memory region to allocate to the application. In this approach, a demand threshold is set so that memory is only allocated to the application from the first memory region when the memory demand exceeds the threshold. This allows the first memory region to be used more for applications with high memory requirements.
[0032] For example, some large applications require extensive data processing during startup, resulting in high memory demands. In such cases, pre-establishing a first memory region allows for the direct acquisition of a large, contiguous block of memory (enough to meet the memory requirements of large applications) from this region, eliminating the need for real-time memory defragmentation when allocating large, contiguous blocks of memory. This facilitates faster memory allocation for startup applications, improving the startup speed of applications with high memory requirements during the startup phase.
[0033] In this embodiment, the demand threshold can be dynamically updated. Optionally, the demand threshold can be manually configured by the user. In this case, after detecting a user's update operation on the demand threshold, the updated demand threshold input by the user can be obtained, and then the updated demand threshold can be configured as the threshold for detecting memory demand.
[0034] Optionally, the electronic device can determine the demand threshold based on the startup duration of the installed applications. It should be noted that, as mentioned earlier, memory is required during application startup to load and process data before the application enters its normal running state. In this case, if the average startup duration of the target application on the electronic device is detected to be greater than the duration threshold, the demand threshold can be configured as a first demand threshold. Conversely, if the average startup duration of the target application on the electronic device is detected to be no greater than the duration threshold, the demand threshold can be configured as a second demand threshold. The second demand threshold is greater than the first demand threshold. This approach lowers the demand threshold when the average startup duration is long, making it easier to trigger memory allocation from the first memory region, thereby improving application startup speed. Conversely, when the average startup duration is not too long, the first memory region can be used more to cache memory pages of a specified type removed from the inactive list, reducing the probability of the electronic device repeatedly reading the same data from the disk into memory.
[0035] This embodiment provides a memory allocation method that, in response to an instruction to allocate memory to an application, after obtaining the memory requirement corresponding to the instruction, can allocate memory to the application from the remaining memory in a pre-configured first memory region containing contiguous memory addresses, provided that the memory requirement is met. Thus, by using a pre-configured first memory region with contiguous addresses, when memory needs to be allocated to an application, memory can be directly selected from this first memory region for allocation. This leverages the contiguous address characteristics of the memory in the first memory region to allocate the required memory to the application more quickly.
[0036] Please see Figure 3 This application provides a memory allocation method, which includes:
[0037] S210: In response to an instruction to allocate memory for an application, obtain the memory requirement corresponding to the instruction.
[0038] S220: Detect whether the remaining memory in the pre-configured first memory region meets the memory requirements.
[0039] S221: If the remaining memory in the pre-configured first memory region meets the memory requirement, obtain memory from the remaining memory and allocate it to the application; wherein the addresses of the memory included in the first memory region are contiguous.
[0040] S222: If the remaining memory in the pre-configured first memory region is insufficient to meet the memory requirements, perform memory reclamation and allocate memory from the free memory to the application.
[0041] S230: In response to reclaiming a page of a specified type from the inactive list, the reclaimed page of the specified type is stored in the first memory area.
[0042] In this embodiment, the first memory region can be understood as the memory region configured based on the cleancache mechanism.
[0043] S240: If a page of the specified type is read while stored in the first memory region, move the page of the specified type to the active list.
[0044] One approach is to divide memory pages in electronic devices into frequently used and infrequently used memory pages based on their usage frequency. Frequently used memory pages can be stored in an active list, while infrequently used memory pages can be stored in an inactive list.
[0045] It's important to note that electronic devices reclaim memory based on demand. Since the size of memory in electronic devices is finite, memory reclamation is a common mechanism. There are two main reasons for this: electronic devices need to provide sufficient memory for any sudden memory requests to prevent cache and other related memory usage from leaving the device with very little free memory for extended periods. Cache is a high-speed storage device located between the processor and main memory. Secondly, memory reclamation is triggered when a request for more free memory arrives. The core idea of memory reclamation is that if some data can be saved to disk, it is written from memory to disk when memory is insufficient, thus freeing up the memory pages occupied by this data for the system. The entire memory reclamation process can be understood as the shrinking of a linked list storing memory pages.
[0046] For example, as described above, memory pages can be stored in a linked list of a specified format (e.g., an LRU linked list). When a process reclaims memory, if the length of the inactive page list has not reached a certain threshold, it will first scan from the tail to the head of the active page list (typically scanning 32 pages at a time). Then, all scanned memory pages will be moved to the inactive page list. After that, the electronic device can directly select memory pages from the inactive page list for reclamation.
[0047] For some types of memory pages, after being reclaimed from the inactive page list, the data they record may soon be used by the electronic device again, requiring the electronic device to read from the disk again, thus increasing the frequency of data reads by the electronic device. As an improvement, the electronic device can reclaim pages of a specified type from the inactive list and store the reclaimed pages of that type in a first memory area. In this case, if the specified type of page is read while stored in the first memory area, the specified type of page is moved to the active list, thus avoiding further reads from the disk. Optionally, the specified type of memory page can be a clean page. The data recorded in a clean page can include data read from the disk that has not been edited.
[0048] It should be noted that, as described above, the first memory region can be used to allocate the memory required by the application, or it can be used to cache memory pages of a specific type removed from the inactive list. In this case, if memory in the first memory region is currently being allocated to the application, and a memory page of the specified type is also synchronously written to the first memory region, it may cause the application to be unable to properly use the memory allocated from the first memory region. For example, as shown below... Figure 4 As shown, the memory addresses in the first memory region include memory addresses D1, D2, D3, D4, D5, and D6. Memory addresses D1, D2, D3, and D4 are currently being allocated to the application. In this situation, if a memory page of a specified type is simultaneously written to at least one of the regions at memory addresses D1, D2, D3, and D4, it may cause an application runtime error.
[0049] As an improvement, in response to reclaiming pages of a specified type from the inactive list, the reclaimed pages of the specified type are stored in a first memory area, including: in response to reclaiming pages of the specified type from the inactive list, detecting whether the caching function of the first memory area is disabled; if the caching function is not disabled, the reclaimed pages of the specified type are stored in the first memory area; if the caching function is disabled, the reclaimed pages of the specified type are discarded, wherein the caching function of the first memory area is disabled when memory is allocated to the application in the first memory area.
[0050] In the embodiments of this application, the duration of the caching function of the first memory region can be configured in various ways.
[0051] Optionally, the duration of the caching function in the first memory region can be determined based on the application. This determination includes: determining the duration of the caching function based on the application's historical startup duration, where the duration is proportional to the historical startup duration. It should be noted that different applications require different amounts of data to load and process during startup, and even for the same application, the amount of data to load and process will vary depending on the steps performed during startup. For example, if the application does not require updates, it only needs to load the data required for application operation at startup. If the application needs updates, it must first load the update data into memory, then perform the application update, and finally load the data required for application operation after the update is complete. Therefore, determining the duration of the caching function in the first memory region based on the application's historical startup duration allows for a more accurate determination of the caching duration. Furthermore, the duration of the caching function's shutdown can also be configured by the user or developer. For example, the shutdown duration can be configured to be 5 seconds.
[0052] This embodiment provides a memory allocation method that, in response to an instruction to allocate memory to an application, after obtaining the memory requirement corresponding to the instruction, can allocate memory to the application from the remaining memory in a pre-configured, address-contiguous first memory region if the memory requirement is met. This allows for faster allocation of memory to the application by directly selecting memory from the pre-configured address-contiguous first memory region when memory needs to be allocated to the application. Furthermore, in this embodiment, pages of a specific type removed from the inactive list can be cached in the first memory region. This allows the first memory region to be used both for memory allocation to the application and for caching pages of the specified type removed from the inactive list, thereby improving the utilization rate of the first memory region.
[0053] Please see Figure 5 This application provides a memory allocation method, which includes:
[0054] S310: In response to an instruction to allocate memory for an application, obtain the memory requirement corresponding to the instruction.
[0055] S320: Detects whether the available memory of an electronic device meets the memory requirements.
[0056] Here, free memory can be understood as the free memory outside the first memory region.
[0057] S321: If the available memory meets the memory requirements, allocate memory from the available memory to the application.
[0058] S322: If the available memory is insufficient to meet the memory requirements, check whether the remaining memory in the first memory region meets the memory requirements.
[0059] S330: If the remaining memory in the pre-configured first memory region meets the memory requirements, obtain memory from the remaining memory and allocate it to the application.
[0060] S340: If the remaining memory in the pre-configured first memory region does not meet the memory requirements, perform memory reclamation to increase the available memory.
[0061] S350: After performing memory reclamation, it allocates memory from free memory to the application.
[0062] This embodiment provides a memory allocation method that, in response to an instruction to allocate memory to an application, after obtaining the memory requirement corresponding to the instruction, can allocate memory to the application from the remaining memory in a pre-configured first memory region with contiguous memory addresses, provided that the memory requirement is met. This allows for faster allocation of memory to the application by directly selecting memory from this first memory region when memory needs to be allocated, leveraging the contiguous address characteristics of the memory in the first memory region. Furthermore, this embodiment provides three methods: allocating memory to the application from free memory, allocating memory to the application from the first memory region, and reclaiming memory before reallocating it to the application. This allows the electronic device to select a method for memory allocation based on the order of priority or current needs.
[0063] Next, we will proceed through... Figure 6 This embodiment describes a memory allocation method.
[0064] like Figure 6 As shown, the method includes:
[0065] S410: Triggers the buddy system to allocate memory.
[0066] The Buddy system is a subsystem of the Linux Kernel that manages physical memory pages. In the Buddy system, the basic unit of memory management is a block, and each block consists of several contiguous physical pages. The number of physical pages is 2^n, where n is called the order in the Buddy system. Blocks of the same order are mounted on a doubly linked list.
[0067] Optionally, the electronic device may trigger the buddy system to allocate memory in response to the launch of an application, or in response to a request from an application to increase memory allocation.
[0068] S420: Does the required memory allocation exceed the demand threshold? If yes, proceed to S430; otherwise, proceed to S480.
[0069] The demand threshold can be either 4k or 64k.
[0070] S430: First allocate memory from free memory, without triggering memory reclamation.
[0071] In this context, the free memory in S430 can be understood as the free memory in the memory area outside the first memory area.
[0072] S440: Check if the allocation was successful? If not, proceed to S450.
[0073] In the S440, it checks whether memory can be successfully allocated to the application from free memory. Optionally, it can first try to allocate memory with order=8 (1Mbytes). If the allocation is successful, it will be allocated. If the allocation fails, it will try memory with order=4 (64kbytes). If the allocation is successful, it will be allocated. If the allocation fails, it will try memory with order=0 (4kbytes).
[0074] S450: Check if there is memory available for reuse in the first memory area?
[0075] If free memory cannot be allocated, the system will further check whether memory in the first memory region can be reused. It's important to note that the first memory region (e.g., the cleancache region) can be used to cache memory pages of a specified type removed from the inactive list. In this case, reusing memory in the first memory region means that not all memory in the first memory region is used to cache memory pages; some memory remains available for allocation to the application.
[0076] S460: If so, reuse the memory in the first memory region.
[0077] S470: Check if the allocation was successful? If not, proceed to S480.
[0078] S480: Performs memory reclamation, allocating memory from free memory.
[0079] In the S480, memory reclamation can be performed first to increase the amount of free memory. Then, memory allocation can be performed after memory reclamation.
[0080] S490: Check if the allocation was successful? Otherwise, determine that the allocation failed.
[0081] Please see Figure 7 This application provides a memory allocation device 400, which includes:
[0082] The allocation requirement determination unit 410 is used to obtain the memory requirement corresponding to the instruction to allocate memory for the application.
[0083] The memory allocation unit 420 is used to allocate memory to the application from the remaining memory if the remaining memory in the pre-configured first memory region meets the memory requirements; wherein the addresses of the memory included in the first memory region are contiguous.
[0084] In one manner, the memory page cache unit 430 is used to store the reclaimed page of the specified type into a first memory region in response to the reclamation of a page of the specified type from the inactive list; if the page of the specified type is read while stored in the first memory region, the page of the specified type is moved to the active list.
[0085] Optionally, the memory page cache unit 430 is specifically used to detect whether the caching function of the first memory region is disabled in response to the reclamation of a specified type of page from the inactive list; if the caching function is not disabled, the specified type of page to be reclaimed is stored in the first memory region; if the caching function is disabled, the specified type of page to be reclaimed is discarded; wherein, the caching function of the first memory region is disabled when memory is allocated to the application in the first memory region.
[0086] Optionally, the memory page cache unit 430 is specifically used to determine the duration for which the caching function of the first memory region is disabled based on the application. Alternatively, the memory page cache unit 430 is specifically used to determine the duration for which the caching function of the first memory region is disabled based on the historical startup duration of the application, wherein the duration for which the caching function is disabled is proportional to the historical startup duration.
[0087] In one manner, the memory allocation unit 420 is also used to detect whether the free memory of the electronic device meets the memory requirements; if the free memory does not meet the memory requirements, if the remaining memory in the pre-configured first memory area meets the memory requirements, memory is allocated from the remaining memory to the application. If the remaining memory in the pre-configured first memory area does not meet the memory requirements, memory reclamation is performed to increase the free memory.
[0088] After performing memory reclamation, memory is allocated to the application from the free memory.
[0089] In one approach, the memory allocation unit 420 is specifically used to detect whether the memory demand is greater than the demand threshold; if the memory demand is greater than the demand threshold, if the remaining memory in the pre-configured first memory area meets the memory demand, memory is obtained from the remaining memory and allocated to the application; if the memory demand is not greater than the demand threshold, memory is allocated from the free memory to the application.
[0090] This application provides a memory allocation device that, in response to an instruction to allocate memory to an application, after obtaining the memory requirement corresponding to the instruction, can allocate memory to the application from the remaining memory in a pre-configured first memory region containing contiguous memory addresses, provided that the memory requirement is met. Thus, by utilizing the pre-configured first memory region with contiguous addresses, when memory needs to be allocated to an application, memory can be directly selected from this first memory region for allocation. This leverages the contiguous address characteristics of the memory in the first memory region to allocate the required memory to the application more quickly.
[0091] It should be noted that the device embodiments in this application correspond to the aforementioned method embodiments. The specific principles in the device embodiments can be found in the content of the aforementioned method embodiments, and will not be repeated here.
[0092] The following will combine Figure 8 This application describes an electronic device.
[0093] Please see Figure 8 Based on the aforementioned audio processing method and apparatus, this application embodiment also provides another electronic device 200 capable of executing the aforementioned audio processing method. The electronic device 200 includes one or more (only one shown in the figure) processors 202, a memory 204, a network module 206, a sensor module 208, and an audio acquisition device 210, all coupled to each other. The memory 204 stores programs capable of executing the contents of the aforementioned embodiments, and the processor 202 can execute the programs stored in the memory 204.
[0094] The processor 202 may include one or more processing cores. The processor 202 connects to various parts within the electronic device 200 using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 204, and by calling data stored in the memory 204. Optionally, the processor 202 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 202 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the displayed content; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 202 and may be implemented separately using a communication chip.
[0095] The memory 204 may include random access memory (RAM) or read-only memory (ROM). The memory 204 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 204 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), and instructions for implementing the various method embodiments described below.
[0096] The network module 206 is used to enable information interaction between the electronic device 200 and other devices, such as transmitting device control commands, manipulation request commands, and status information acquisition commands. However, the network module 206 may differ depending on the specific device being used.
[0097] Sensor module 208 may include at least one sensor. Specifically, sensor module 208 may include, but is not limited to, a light sensor, a motion sensor, a pressure sensor, an infrared thermal sensor, a distance sensor, an acceleration sensor, and other sensors.
[0098] The pressure sensor is a sensor that detects pressure generated by pressing on the electronic device 200. That is, the pressure sensor detects pressure generated by contact or pressing between the user and the electronic device, such as the pressure generated by contact or pressing between the user's ear and the mobile terminal. Therefore, the pressure sensor can be used to determine whether contact or pressing has occurred between the user and the electronic device 200, and the magnitude of the pressure.
[0099] The accelerometer can detect the magnitude of acceleration in various directions (generally three axes), and when stationary, it can detect the magnitude and direction of gravity. It can be used for applications such as identifying the posture of electronic devices 200 (e.g., screen orientation switching, related games, magnetometer posture calibration), and vibration recognition functions (e.g., pedometer, tapping). In addition, electronic devices 200 can also be equipped with other sensors such as gyroscopes, barometers, hygrometers, and thermometers, which will not be elaborated upon here.
[0100] Audio acquisition device 210 is used for acquiring audio signals. Optionally, audio acquisition device 210 includes multiple audio acquisition devices. These audio acquisition devices can be microphones. For example, in one embodiment, audio acquisition device 210 may include two microphones, in which one microphone may correspond to one analog-to-digital converter (ADC), and the other microphone may correspond to two ADCs with different analog gains. In another embodiment, audio acquisition device 210 may include three microphones. In this embodiment, two microphones (e.g., a main microphone and a secondary microphone) may each correspond to one ADC, and the other microphone (e.g., a camera microphone) may correspond to two ADCs with different analog gains.
[0101] In one approach, the network module of the electronic device 200 is a radio frequency (RF) module. This RF module is used to receive and transmit electromagnetic waves, converting electromagnetic waves into electrical signals, thereby enabling communication with a communication network or other devices. The RF module may include various existing circuit elements used to perform these functions, such as antennas, RF transceivers, digital signal processors, encryption / decryption chips, SIM cards, memory, etc. For example, the RF module can interact with external devices by transmitting or receiving electromagnetic waves, thereby receiving audio signals transmitted by external devices.
[0102] Furthermore, the electronic device 200 may also include an image acquisition device for image acquisition. For example, the image acquisition device can be used to capture video, still images, or moving images.
[0103] Please refer to Figure 9This diagram illustrates a structural block diagram of a computer-readable storage medium provided in an embodiment of this application. The computer-readable medium 800 stores program code that can be called by a processor to execute the methods described in the above method embodiments.
[0104] The computer-readable storage medium 800 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or ROM. Optionally, the computer-readable storage medium 800 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 800 has storage space for program code 810 that performs any of the method steps described above. This program code can be read from or written to one or more computer program products. The program code 810 may be compressed, for example, in a suitable form.
[0105] This application provides a memory allocation method, apparatus, and electronic device. In response to an instruction to allocate memory to an application, after obtaining the memory requirement corresponding to the instruction, if the remaining memory in a pre-configured first memory region containing contiguous memory addresses satisfies the memory requirement, memory can be allocated to the application from the remaining memory. Thus, by using a pre-configured first memory region with contiguous addresses, when memory needs to be allocated to an application, memory can be directly selected from this first memory region for allocation. This leverages the contiguous address characteristics of the memory in the first memory region to allocate the required memory to the application more quickly.
[0106] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A memory allocation method, characterized in that, The method includes: In response to an instruction to allocate memory to an application, the memory requirement corresponding to the instruction is obtained, wherein the instruction is triggered when the application starts; The system detects whether the memory requirement is greater than a requirement threshold. If the average startup time of the target application in the electronic device is detected to be greater than the duration threshold, the requirement threshold is configured as a first requirement threshold. If the average startup time is detected to be less than the duration threshold, the requirement threshold is configured as a second requirement threshold. The second requirement threshold is greater than the first requirement threshold. The target application includes the applications installed on the electronic device. If the memory requirement exceeds the requirement threshold, and the remaining memory in the pre-configured first memory region satisfies the memory requirement, memory is allocated from the remaining memory to the application; wherein the addresses of the memory included in the first memory region are contiguous. If the memory requirement is not greater than the requirement threshold, memory is allocated to the application from the free memory outside the first memory region. The first memory region is further used to cache memory pages of a specified type removed from the inactive list, the specified type of memory pages including clean pages. The method further includes: in response to reclaiming pages of a specified type from the inactive list, detecting whether the caching function of the first memory region is disabled; if the caching function is not disabled, storing the reclaimed pages of the specified type into the first memory region; if the caching function is disabled, discarding the reclaimed pages of the specified type, wherein the caching function of the first memory region is disabled when memory is allocated to the application in the first memory region; if the specified type of page is read while stored in the first memory region, moving the specified type of page to the active list.
2. The method according to claim 1, characterized in that, The method further includes: The duration for which the caching function of the first memory region is disabled is determined based on the application.
3. The method according to claim 2, characterized in that, The step of determining the duration for which the caching function of the first memory region is disabled based on the application includes: The duration for which the caching function of the first memory region is disabled is determined based on the historical startup duration of the application, wherein the duration for which the caching function is disabled is proportional to the historical startup duration.
4. The method according to claim 1, characterized in that, If the remaining memory in the pre-configured first memory region meets the memory requirement, the step of allocating memory from the remaining memory to the application further includes: Detect whether the available memory of the electronic device meets the memory requirement; If the available memory does not meet the memory requirement, then if the remaining memory in the pre-configured first memory region meets the memory requirement, then memory is allocated from the remaining memory to the application.
5. The method according to claim 4, characterized in that, The method further includes: If the remaining memory in the pre-configured first memory area does not meet the memory requirement, memory reclamation is performed to increase the free memory. After the memory reclamation is performed, memory is allocated from the free memory to the application.
6. A memory allocation device, characterized in that, The device includes: The allocation requirement determination unit is used to obtain the memory requirement corresponding to the instruction to allocate memory for the application in response to the instruction, wherein the instruction is triggered when the application starts. A memory allocation unit is used to detect whether the memory demand exceeds a demand threshold. If the average startup time of a target application in the electronic device is detected to be greater than a duration threshold, the demand threshold is configured as a first demand threshold. If the average startup time is detected to be less than the duration threshold, the demand threshold is configured as a second demand threshold, which is greater than the first demand threshold. The target application includes applications installed on the electronic device. If the memory demand exceeds the demand threshold, and if the remaining memory in a pre-configured first memory region satisfies the memory demand, memory is obtained from the remaining memory and allocated to the application. The first memory region includes contiguous memory addresses and is also used to cache memory pages of a specified type removed from an inactive list, including clean pages. The memory allocation unit is also configured to allocate memory to the application from free memory outside the first memory region if the memory requirement is not greater than the requirement threshold. A memory page caching unit is configured to, in response to the reclamation of a specified type of page from an inactive list, detect whether the caching function of the first memory region is disabled; if the caching function is not disabled, store the reclaimed specified type of page in the first memory region; if the caching function is disabled, discard the reclaimed specified type of page, wherein the caching function of the first memory region is disabled when memory is allocated to the application in the first memory region; if the specified type of page is read while stored in the first memory region, move the specified type of page to the active list.
7. An electronic device, characterized in that, It includes one or more processors and memory; one or more programs are stored in the memory and configured to be executed by the one or more processors according to any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program code, wherein the program code, when executed by a processor, performs the method according to any one of claims 1-5.
Citation Information
Patent Citations
Memory allocation method and device, storage medium and electronic equipment
CN113806238A