Memory management method and electronic equipment

By detecting the operating status of the application and performing memory management based on functional modules or business granularity, the problem of insufficient memory of electronic devices is solved, the fluency and stability of the system are improved, and the risk of abnormal exit of the application is reduced.

CN120295752APending Publication Date: 2025-07-11HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510181158.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2022-07-30
Publication Date
2025-07-11

AI Technical Summary

Technical Problem

In the prior art, the memory management method of electronic devices cannot effectively improve the fluency of applications, resulting in insufficient free memory of the system and affecting the user experience.

Method used

By detecting the operating status of the application and performing memory management based on functional modules or business granularity, including memory recycling, compression and swapping, optimize the memory usage method and avoid relying on the application to actively release memory data.

Benefits of technology

It improves the memory management efficiency of electronic devices, reduces the probability of applications being detected or abnormally exited, improves the fluency and stability of the system, and meets the memory processing needs of different life cycle stages.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120295752A_ABST
    Figure CN120295752A_ABST
Patent Text Reader

Abstract

The invention discloses a memory management method and electronic equipment, relates to the technical field of terminals, and aims to perform memory management by taking a service or a functional module of an application program as granularity and improve the memory management performance of the electronic equipment. The method is applied to the electronic equipment and comprises the steps that the running state of a first application is detected; when it is detected that the running state of a first application is switched from a first running state to a second running state, processing a part of memory used by the first application; the partial memory is a memory associated with a target service of the first application; the target service is a non-key service of the first application in the second running state.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] This application is a divisional application. The application number of the original application is 202210912594.8, and the original application date is July 30, 2022. The entire content of the original application is incorporated herein by reference. Technical Field

[0002] This application relates to the field of terminal technologies, and particularly to a memory management method and an electronic device. Background Art

[0003] Currently, with the popularization of electronic devices such as mobile phones, users have an increasingly high demand for the fluency of applications. In an electronic device, memory is one of the very important system resources. If the free memory of the system is insufficient, the fluency of applications will be greatly reduced, resulting in application lags and affecting the user experience. Therefore, there is an urgent need to propose an effective memory management method to improve the performance of electronic devices. Summary of the Invention

[0004] To solve the above technical problems, embodiments of this application provide a memory management method and an electronic device. The technical solutions provided by the embodiments of this application can perform memory management at the granularity of the services or functional modules of an application program, and improve the memory management performance of an electronic device.

[0005] To achieve the above technical objectives, embodiments of this application provide the following technical solutions:

[0006] In a first aspect, a memory management method is provided. This method is applied to an electronic device or a component (such as a chip system) that can implement the functions of an electronic device. The method includes:

[0007] Detect the running state of a first application; when it is detected that the running state of the first application switches from a first running state to a second running state, process a part of the memory used by the first application; the part of the memory is the memory associated with the target service of the first application; the target service is a non-critical service of the first application in the second running state.

[0008] Exemplarily, taking the first application as a navigation application as an example, Figure 6 , when it is detected that the state of the navigation application switches from the foreground running state (an example of the first running state) to the background playing state (an example of the second running state), the mobile phone can process the memory of the non-critical services used by the navigation application (such as the memory used by the rendering thread and the user interface thread).

[0009] Among them, processing the memory includes recycling the memory. First, since the memory is processed based on the granularity of the application's functional modules or services, the fineness of the memory processing is higher. In some scenarios, processing the memory data used by some functional modules of the application (such as functional modules that will not be used temporarily) or non-critical services will not affect the normal operation of the entire application, which can improve the keep-alive degree of the application and reduce the probability of the application being killed or abnormally exited. For example, for some background applications, the electronic device can only recycle the memory used by some functional modules or non-critical services of the background application. In this way, while ensuring the survival of the background application as much as possible, the memory of the application can be released to a large extent, enhancing the fluency and stability of the electronic device and improving the memory management efficiency of the electronic device.

[0010] Secondly, compared with the related art, relying on the onTrimMemory mechanism and expecting the application to actively release the memory data, resulting in the fact that the application actually does not release the memory data and the memory pressure of the system is still relatively large. The technical solution of the embodiment of the present application does not rely on the application to actively release the memory data. Specifically, in the embodiment of the present application, when it is detected that the application is in the corresponding running state of the life cycle, the electronic device can automatically process the memory data of some functional modules or non-critical services of the application. In this way, on the one hand, it helps to relieve the memory pressure of the system. On the other hand, the electronic device recovers the corresponding memory data from some functional modules or non-critical services of the application, so that the total memory occupied by the application is reduced. Therefore, the probability of the application being killed or abnormally exited will be greatly reduced.

[0011] In a possible design, when it is detected that the running state of the first application switches from the first running state to the second running state, processing a part of the memory used by the first application includes: when it is detected that the running state of the first application switches from the first running state to the second running state, and the duration of the first application in the second running state reaches the first threshold, processing a part of the memory used by the first application.

[0012] Exemplarily, as Figure 11 , it is detected that at time t1, the running state of the navigation application (an example of the first application) switches from the foreground running state (an example of the first running state) to the background playback state (an example of the second running state), and the duration of the navigation application in the background playback state (duration t1 - t1') reaches the first threshold, and a part of the memory used by the navigation application (the memory used by the rendering thread and the user interface thread) is processed.

[0013] In this way, after the application switches the running state, the electronic device can delay the processing. In other words, the electronic device can perform memory recycling after a period of time after the application switches the running state to ensure the stable running state of the application.

[0014] In a possible design, the target service includes a first service and / or a second service; the memory associated with the first service includes a first part of the memory, and the memory associated with the second service includes a second part of the memory;

[0015] The first part of the memory is: the pages not used by the first application within the first time period;

[0016] The second part of the memory is: the compressed pages not used by the first application within the second time period.

[0017] Exemplarily, such as Figure 6 , taking the application switching from the foreground running state to the background playing state as an example. In the background playing state, the first part of the memory includes the memory used by the rendering thread and the user interface thread. The first part of the memory is not used by the first application within the time period from t1 to t2. When it is detected that the application switches from the foreground running state to the background playing state, the mobile phone can recycle the memory used by non-critical services such as the rendering thread and the user interface thread.

[0018] Taking the navigation application switching from the background playing state to the background service state as an example. In the background service state, the first part of the memory includes the memory used by surface and media. The first part of the memory is not used by the navigation application within the time period from t2 to t3. When it is detected that the application switches from the background playing running state to the background service state, the mobile phone can recycle the memory used by non-critical services such as surface and media.

[0019] Taking the navigation application switching from the background service state to the background caching state as an example. In the background caching state, the first part of the memory includes the memory used by service, and the second part of the memory includes the memory used by the rendering thread and the user interface thread. The first part of the memory is not used by the navigation application within the time period from t3 to t4, and the second part of the memory is not used by the navigation application within the time period from t1 to t3 (the second time period). When it is detected that the application switches from the background service running state to the background caching state, the mobile phone can recycle the memory used by non-critical services such as service, the rendering thread, and the user interface thread.

[0020] Taking the navigation application switching from the background cache state to the shallow freeze state as an example, in the shallow freeze state, the second part of the memory includes the memory used by service, media, and surface. Among them, the memory of service is not used by the navigation application during the time period t3 - t4 (the second time period), and the memory of media and surface is not used by the navigation application during the time period t2 - t4 (the second time period). When it is detected that the navigation application switches from the background cache running state to the shallow freeze state, the mobile phone can reclaim the memory used by non-critical services such as service, media, and surface.

[0021] Taking the navigation application switching from the shallow freeze state to the deep freeze state as an example, in the deep freeze state, the first part of the memory includes the memory used by object, class, and method. The memory of object, class, and method is not used by the navigation application during the time period t5 - t6 (the first time period). When it is detected that the navigation application switches from the shallow freeze running state to the deep freeze state, the mobile phone can reclaim the memory used by non-critical services such as object, class, and method.

[0022] In a possible design, the duration of the second time period is greater than the duration of the first time period.

[0023] In a possible design, the first compression method is adopted to process the memory of the first service, and the second compression method is adopted to process the memory of the second service.

[0024] In this way, for the memory data not used by the application program in the short term and the memory data not used by the application program in the long term, different compression methods can be used for memory processing to improve the memory processing performance.

[0025] In a possible design, the second running state includes the first background running state;

[0026] When it is detected that the running state of the first application switches from the first running state to the second running state, processing the part of the memory used by the first application includes:

[0027] When it is detected that the running state of the first application switches from the first running state to the first background running state, compressing the first part of the memory associated with the first service. The first part of the memory can be the memory not used by the application program in the short term (such as the first time period).

[0028] In this way, for the memory data that is not needed in the short term, this part of the memory data can be compressed and stored in the compression space to save a part of the memory space.

[0029] In a possible design, the second running state includes a first background running state;

[0030] When it is detected that the running state of the first application switches from the first running state to the second running state, processing is performed on a part of the memory used by the first application, including:

[0031] When it is detected that the running state of the first application switches from the first running state to the first background running state, a second part of the memory associated with the second service is swapped out to disk space. The second part of the memory can be memory that is not used by the application program for a long time (such as a second time period).

[0032] In this way, for compressed memory data that is not used for a long time (such as compressed memory pages), this part of the compressed memory data can be swapped out, for example, through a disk write mechanism, to disk space (such as on a Flash device), reducing memory occupancy.

[0033] In a possible design, the first running state is the foreground running state.

[0034] In a possible design, the first running state is a second background running state.

[0035] In a possible design, the method further includes:

[0036] When it is detected that the running state of the first application switches from the first background running state to a third background running state;

[0037] Compressing a third part of the memory of the first application, where the third part of the memory is: the memory not used by the first application within the first time period;

[0038] and / or,

[0039] Swapping out a fourth part of the memory of the first application to disk space, where the fourth part of the memory is: the memory not used by the first application within the second time period; the duration of the fourth time period is greater than the duration of the third time period.

[0040] In a possible design, processing a part of the memory used by the first application includes:

[0041] Obtaining the virtual memory space VMA corresponding to the part of the memory and the linked list corresponding to the VMA; the linked list includes the part of the memory;

[0042] Processing the part of the memory in the linked list.

[0043] In a possible design, the method further includes:

[0044] Display the first interface;

[0045] Receive an operation input by the user on the first interface, where the operation is used to enable the memory management function.

[0046] In a possible design, the first background running state includes the following states: background playback state, background service state, background cache state, shallow freeze state, and deep freeze state;

[0047] Among them, in the background playback state, the first application executes a first task in the background. Exemplarily, the background playback state means that the application switches to running in the background and no longer presents a graphical interface, but the functions or tasks of the application are still running. For example, the music application runs a music playback task (an example of the first task) in the background, and the navigation application runs a navigation task in the background.

[0048] In the background service state, the first application provides a background service in the background, and the first application does not execute the first task in the background. Exemplarily, it refers to background service type applications. Background service type applications mainly implement background data collection, message push, or permanent interrupt waiting services, such as Bluetooth connection. For another example, the application can push messages in the background. For another example, the application can collect some data to push some messages to the user.

[0049] In the background cache state, the first application does not execute the first task in the background, does not provide a background service, and the duration for which the first application is in the background running state reaches a first duration.

[0050] In the shallow freeze state, the first application does not execute the first task in the background, does not provide a background service, and the duration for which the first application is in the background running state reaches a second duration; the second duration is greater than the first duration.

[0051] In the deep freeze state, the first application does not execute the first task in the background, does not provide a background service, and the duration for which the first application is in the background running state reaches a third duration; the third duration is greater than the second duration.

[0052] In a possible design, in the background playback state, the first service includes services related to interface display. Exemplarily, Figure 6 , services related to interface display include non-critical services executed by the rendering thread and the user interface thread. In the background playback state, the electronic device can recycle the memory used by the rendering thread and the user interface thread of the application to reduce the memory occupancy of the application.

[0053] In the background service state, the first service includes the service corresponding to the first task (the task that the first application does not execute in the background). Exemplarily, Figure 6 , in the background service state, the first application usually does not execute the services corresponding to media and surface in the background. When it is detected that the application enters the background service state, the electronic device can compress the memory used by media and surface to obtain the compressed memory data.

[0054] In the background cache state, the first service includes background services, and the second service includes services related to interface display. Exemplarily, Figure 6 , in the background cache state, the application no longer executes background services (such as service), and the interface has not been switched to the foreground for a long time. The first service includes background services, and the second service includes the services executed by the rendering thread and the user interface thread. When it is detected that the application enters the background cache state, the electronic device can compress the relevant memory of the service that has not been used by the application in the short term to obtain the compressed memory data. In addition, the electronic device can swap out the compressed memory related to the rendering thread and the user interface thread that has not been used by the application for a long time.

[0055] In the light freeze state, the second service includes: the service corresponding to the first task, background services. Exemplarily, Figure 6 , in the light freeze state, the second service includes: media, surface (the service corresponding to the first task), service (background services). When it is detected that the application enters the light freeze state, the electronic device can swap out the compressed memory of media, surface, and service that has not been used by the application for a long time.

[0056] In the deep freeze state, the first service includes the services corresponding to objects, classes, and methods. Exemplarily, Figure 6 , when it is detected that the application enters the deep freeze state, the electronic device can compress the memory used by objects, classes, and methods.

[0057] In a second aspect, a memory management device is provided. The device is applied to an electronic device or a component (such as a chip system) that supports the functions of the electronic device. The device includes:

[0058] A processing unit, configured to detect the running state of a first application; when it is detected that the running state of the first application switches from a first running state to a second running state, process a part of the memory used by the first application; the part of the memory is the memory associated with the target service of the first application; the target service is a non-critical service of the first application in the second running state.

[0059] In a possible design, when it is detected that the running state of the first application switches from the first running state to the second running state, processing a part of the memory used by the first application includes: detecting that the running state of the first application switches from the first running state to the second running state, and when the duration of the first application in the second running state reaches a first threshold, processing a part of the memory used by the first application.

[0060] In a possible design, the target service includes the first service and / or the second service; the memory associated with the first service includes a first part of the memory, and the memory associated with the second service includes a second part of the memory;

[0061] The first part of the memory is: pages that have not been used by the first application within a first time period;

[0062] The second part of the memory is: compressed pages that have not been used by the first application within a second time period.

[0063] In a possible design, the duration of the second time period is greater than the duration of the first time period.

[0064] In a possible design, the second running state includes a first background running state;

[0065] Detecting that the running state of the first application switches from the first running state to the second running state, and processing a part of the memory used by the first application includes:

[0066] Detecting that the running state of the first application switches from the first running state to the first background running state, and compressing the first part of the memory associated with the first service.

[0067] In a possible design, the second running state includes a first background running state;

[0068] Detecting that the running state of the first application switches from the first running state to the second running state, and processing a part of the memory used by the first application includes:

[0069] Detecting that the running state of the first application switches from the first running state to the first background running state, and swapping out the second part of the memory associated with the second service to disk space.

[0070] In a possible design, the first running state is the foreground running state.

[0071] In a possible design, the first running state is the second background running state.

[0072] In a possible design, the processing unit is further configured to:

[0073] It is detected that the running state of the first application switches from the first background running state to the third background running state;

[0074] Compress the third part of the memory of the first application, where the third part of the memory is the memory not used by the first application during the first time period;

[0075] And / or,

[0076] Swap out the fourth part of the memory of the first application to disk space, where the fourth part of the memory is the memory not used by the first application during the second time period; the duration of the fourth time period is greater than the duration of the third time period.

[0077] In a possible design, processing the part of the memory used by the first application includes:

[0078] Obtain the virtual memory space VMA corresponding to the part of the memory and the linked list corresponding to the VMA; the linked list includes the part of the memory;

[0079] Process the part of the memory in the linked list.

[0080] In a possible design, the device further includes:

[0081] A display unit for displaying a first interface;

[0082] An input unit for receiving an operation input by a user on the first interface, where the operation is used to enable a memory management function.

[0083] In a possible design, the first background running state includes the following states: background playback state, background service state, background cache state, shallow freeze state, deep freeze state;

[0084] Among them, in the background playback state, the first application executes a first task in the background;

[0085] In the background service state, the first application provides a background service in the background, and the first application does not execute the first task in the background;

[0086] In the background cache state, the first application does not execute the first task in the background, does not provide a background service, and the duration of the first application in the background running state reaches a first duration;

[0087] In the shallow freeze state, the first application does not execute the first task in the background, does not provide a background service, and the duration of the first application in the background running state reaches a second duration; the second duration is greater than the first duration;

[0088] In the deep freeze state, the first application does not execute the first task in the background, does not provide background services, and the duration of the first application in the background running state reaches a third duration; the third duration is greater than the second duration.

[0089] In a possible design, in the background playback state, the first service includes services related to interface display;

[0090] In the background service state, the first service includes the services corresponding to the first task;

[0091] In the background cache state, the first service includes background services, and the second service includes services related to interface display;

[0092] In the light freeze state, the second service includes: the services corresponding to the first task, background services;

[0093] In the deep freeze state, the first service includes services corresponding to objects, classes, and methods.

[0094] In a third aspect, an embodiment of the present application provides an electronic device, and the electronic device has a function of implementing the method described in any of the above aspects and any possible implementation manner thereof. This function can be implemented by hardware or by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the above functions.

[0095] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium. The computer-readable storage medium stores a computer program (which can also be referred to as an instruction or code), and when the computer program is executed by an electronic device, the electronic device is caused to execute the method of the first aspect or any implementation manner in the first aspect.

[0096] In a fifth aspect, an embodiment of the present application provides a computer program product, and when the computer program product runs on an electronic device, the electronic device is caused to execute the method of the first aspect or any implementation manner in the first aspect.

[0097] In a sixth aspect, an embodiment of the present application provides a circuit system, and the circuit system includes a processing circuit configured to execute the method of the first aspect or any implementation manner in the first aspect.

[0098] In a seventh aspect, an embodiment of the present application provides a chip system, including at least one processor and at least one interface circuit. The at least one interface circuit is configured to perform transceiver functions and send instructions to the at least one processor. When the at least one processor executes the instructions, the at least one processor executes the method according to the first aspect or any one of the implementation manners of the first aspect. Description of the Drawings

[0099] Figure 1A A schematic diagram of memory partitioning provided by an embodiment of the present application;

[0100] Figure 1B A schematic diagram of memory management based on a linked list provided by an embodiment of the present application;

[0101] Figure 1C A schematic diagram of a linked list management mechanism when a memory page is in use provided by an embodiment of the present application;

[0102] Figure 1D A schematic diagram of the recycling process in an inactive linked list when memory is insufficient provided by an embodiment of the present application;

[0103] Figure 1E A schematic diagram of a memory compression and swap-out mechanism provided by an embodiment of the present application;

[0104] Figure 1F A schematic diagram of the decline process in an active linked list when memory is insufficient provided by an embodiment of the present application;

[0105] Figure 1G A schematic diagram of a memory management mechanism in related technologies;

[0106] Figure 2 A schematic diagram of memory management based on a framework provided by an embodiment of the present application;

[0107] Figure 3 A schematic diagram of the structure of an electronic device provided by an embodiment of the present application;

[0108] Figure 4 A schematic diagram of the software structure of an electronic device provided by an embodiment of the present application;

[0109] Figure 5 A schematic diagram of the structure of another electronic device provided by an embodiment of the present application;

[0110] Figure 6 A schematic diagram of the scenario of the memory management method provided by an embodiment of the present application;

[0111] Figure 7 A schematic diagram of the virtual memory space and the linked list provided by an embodiment of the present application;

[0112] Figure 8Schematic diagram of the memory management method provided by the embodiments of the present application;

[0113] Figure 9 Schematic diagram of the memory management operation in the background cache state provided by the embodiments of the present application;

[0114] Figure 10 Schematic diagram of the memory management operation in the shallow freeze state provided by the embodiments of the present application;

[0115] Figure 11 Scenario schematic diagram of the memory management method provided by the embodiments of the present application;

[0116] Figure 12 Schematic diagram of the interface provided by the embodiments of the present application;

[0117] Figure 13 Schematic diagram of the structure of the memory management device provided by the embodiments of the present application. Detailed implementation manners

[0118] In the description of the embodiments of the present application, unless otherwise specified, " / " means "or". For example, A / B may represent A or B; herein, "and / or" is merely a description of the associated relationship of the associated objects, indicating that there can be three relationships. For example, A and / or B may represent: A exists alone, A and B exist simultaneously, and B exists alone. These three situations.

[0119] Hereinafter, the terms "first" and "second" are only used for descriptive purposes and cannot be construed as indicating or implying relative importance or implicitly indicating the quantity of the indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include one or more of such features. In the description of the embodiments of the present application, unless otherwise specified, the meaning of "a plurality" is two or more.

[0120] In the embodiments of the present application, words such as "exemplary" or "for example" are used to represent examples, illustrations or explanations. Any embodiment or design solution described as "exemplary" or "for example" in the embodiments of the present application should not be construed as being more preferred or having more advantages than other embodiments or design solutions. Exactly speaking, the use of words such as "exemplary" or "for example" is intended to present relevant concepts in a specific manner.

[0121] First, some terms related to the embodiments of the present application are introduced:

[0122] 1. Memory paging mechanism

[0123] Currently, memory management can be implemented based on memory units. Memory units include, but are not limited to, memory pages. In some solutions, memory can be divided into memory pages according to a certain size (such as 4K). Through the memory paging mechanism, the efficiency of accessing memory can be improved.

[0124] It should be noted that with the evolution of technology, the memory unit can also be implemented in other ways, not limited to the way of memory pages. It should be understood that in the embodiments of the present application, memory pages are used as an example of memory units, but this does not constitute a limitation on the memory unit.

[0125] 2. Anonymous Pages and File Pages

[0126] User-mode memory includes anonymous pages and file-backed pages.

[0127] Among them, a file-backed page refers to a memory page (which can be simply referred to as a memory page or a page) that has a source backup page in an external storage space (such as a disk). The file-backed page has a mapping relationship with the source backup page in the external storage space. In some embodiments, file-backed pages can be used to cache file data. Exemplarily, file-backed pages include core library code, application code, or icon resources, etc.

[0128] As a possible implementation, a program can read a file from a disk through basic operations such as read / mmap, and the system can apply for pages to store the content read from the disk. These pages used to store the content of the disk file can be regarded as a type of file-backed page.

[0129] An anonymous page refers to a memory page that has no corresponding file in the external storage space. For example, the pages used by the heap and stack of a process can be anonymous pages. Anonymous pages can be used to store the temporary calculation results of a process during operation.

[0130] Among them, the external storage space refers to the storage space other than memory.

[0131] In addition to file-backed pages and anonymous pages, memory can also include memory independently managed by the kernel or modules in the kernel. This part of the memory can be used to store the basic data structures and driver-class data that maintain the normal operation of the system.

[0132] When performing memory recycling, for different types of memory, the operating system can recycle them in different ways and proportions.

[0133] 3. Three Memory Regions

[0134] In some solutions, as Figure 1A shown, the memory of an electronic device can be divided into three parts:

[0135] The first part of memory: the on-heap memory of the Java virtual machine. The virtual machine manages the on-heap memory using a garbage collector (GC) based on its own memory management mechanism. As a possible implementation, the virtual machine calls the mmap interface to allocate the on-heap memory. The operating system records this part of the on-heap memory allocated by the virtual machine.

[0136] Optionally, Figure 1A the on-heap memory of the virtual machine can be used to store one or more of objects, classes, and methods.

[0137] The second part of memory: native memory. Exemplarily, native memory can be off-heap memory. Compared with the on-heap memory managed by the virtual machine, native memory is managed by the operating system, so that the impact of garbage collection on the application can be reduced to a certain extent.

[0138] Optionally, the data in native memory mainly includes: data associated with the display function, data associated with the rendering function, and data associated with the system service. For example, Figure 1A the native memory stores data related to the operation of threads such as the user interface (UI) thread and the Render thread. For another example, the native memory stores the runtime metadata of the thread. For another example, the native memory stores data related to the push service.

[0139] As a possible implementation, the operating system allocates native memory through a C++ allocator such as jemalloc or scudo. Optionally, the C++ allocator can identify the identity of the thread that currently requests memory (such as the thread name) and determine the function of the thread based on the identity of the thread. For example, if the C++ allocator identifies that the thread name of the thread that currently requests memory is RenderThread, it can be determined that the thread is a rendering thread, and the function corresponding to the thread name is to render the application's interface.

[0140] The third part of memory: streaming media memory. Currently, hardware encoding and decoding are basically used in the chip architecture to accelerate the rendering of video and audio. In order to enable the data stream to flow between the CPU and the hardware codec, the industry usually uses the ION or dma_buf allocator to identify the commonality of the memory space. Through the identification of the ION or dma_buf attribute, it can be determined that this part of the memory is used for playback.

[0141] Optionally, the streaming media memory can be used to store data related to display classes (surfaces), multimedia classes (media), and service classes (services).

[0142] 4. The operating system uniformly manages the memory

[0143] Due to limited memory resources, when there is insufficient memory, the operating system can recycle infrequently used memory based on the usage frequency of the memory. In some solutions, memory recycling can be based on memory watermarks.

[0144] As a possible implementation, when a memory allocation request is received, the system detects the amount of remaining memory. If the remaining memory is lower than the set low watermark threshold, the recycling thread (kswapd) is awakened to implement asynchronous memory recycling to maintain the remaining system memory and meet the memory allocation requirements.

[0145] Exemplarily, taking the operating system (such as ) as an example, the recycling thread can maintain the free memory at 100 - 200M. When the amount of free memory is lower than 100 (i.e., the watermark), the recycling thread can scan the used memory and recycle some of the used memory. Among them, the recycling thread can calculate the number of memory to be recycled based on the parameter Swappiness and the proportion of the recycled memory, and recycle the memory to be recycled according to the number of memory to be recycled. In this memory recycling mechanism, when the recycling thread calculates the number of memory to be recycled, it does not consider the actual memory requirements of each application, which may lead to excessive memory recycling or insufficient memory recycling. For example, when the free memory of the electronic device is very small, the electronic device over-recycles a large amount of memory, resulting in abnormal termination of the normally running applications and affecting the performance of the electronic device.

[0146] 5. Linked lists and memory management mechanisms based on linked lists

[0147] Such as Figure 1B , the overall memory of the electronic device can include kernel memory, file pages, anonymous pages, free memory, and other memory. Different applications can use different memory to support the operation of the applications. For example, such as Figure 1B , Application 1 uses file pages and anonymous pages, and Application 2 uses file pages and anonymous pages. It should be noted that the file pages used by Application 1 and Application 2 can be the same file pages. In this case, this file page can be called a shared page. Or, the file pages used by Application 1 and Application 2 can be different file pages. Similarly, the anonymous pages used by Application 1 and Application 2 can be the same or different anonymous pages.

[0148] In some solutions, the kernel can manage file pages and anonymous pages through a least recently used (LRU) linked list. According to the active state of the linked list, the linked list can be divided into two levels: an active linked list and an inactive linked list. Among them, as Figure 1B shown, the active linked list includes: an active anonymous page linked list for managing anonymous pages, and an active file page linked list for managing file pages. The inactive linked list includes: an inactive anonymous page linked list for managing anonymous pages, and an inactive file page linked list for managing file pages.

[0149] Multiple memory pages can be stored in the linked list. For example, multiple active file pages can be stored in the active file page linked list, and multiple inactive file pages can be stored in the inactive file page linked list. Multiple active anonymous pages can be stored in the active anonymous page linked list, and multiple inactive anonymous pages can be stored in the inactive anonymous page linked list. Active memory pages can be memory pages frequently used by a process, and inactive memory pages can be memory pages infrequently used by a process.

[0150] Optionally, each memory page corresponds to a usage identifier (such as the flag bit PG_referenced), and this identifier can be used to indicate whether the memory page has been used (referenced).

[0151] In the embodiments of this application, using a memory page can also be referred to as accessing a memory page, calling a memory page, etc.

[0152] 5.1 Memory management mechanism based on a linked list when a memory page is used:

[0153] First, introduce the mechanism of managing memory pages based on a linked list when a memory page is used. As Figure 1C shown, based on the linked list (active linked list or inactive linked list) where the used memory page is located and the previous usage identifier of this memory page, the electronic device can perform different operations:

[0154] As Figure 1C shown in (a) of

[0155] If memory page A in the active linked list is used, the electronic device sets the PG_referenced of this memory page A to 1, indicating that this memory page A has been used. Figure 1C shown in (b) of

[0156] If memory page A in the inactive linked list is used, and the previous flag bit PG_referenced of this memory page A is 0 (such as not being used previously), the electronic device sets the flag bit PG_referenced of this memory page A to 1, indicating that this memory page A has been used. Figure 1CFor (c), if memory page A in the inactive linked list is used and the previous identification bit PG_referenced of this memory page A is 1 (for example, it has been used before). Then, considering that memory page A has been used before and is used again this time, the probability of memory page A being used is relatively high. The electronic device can move memory page A from the inactive linked list to the active linked list, and can set the identification bit PG_referenced to 0, indicating that memory page A has not been used after being moved to the active linked list.

[0157] 5.2 Memory management mechanism based on linked list when system memory is insufficient:

[0158] As follows, introduce the mechanism of managing memory pages based on linked list when free memory is insufficient (for example, free memory is lower than the waterline). Figure 1D When the memory of the electronic device is insufficient, memory page recycling can be preferentially performed from the inactive linked list. As a possible implementation, the electronic device scans the memory pages in the inactive linked list and determines whether to recycle the memory page according to the identification bit of the memory page.

[0159] Taking memory page A at the tail of the inactive linked list as an example, in Figure 1D the example of (a), the identification bit PG_referenced of memory page A is 1, which means that memory page A has been used. In this case, it can be considered that the probability of memory page A being used again in the short term is relatively high. To avoid being unable to quickly find memory page A when memory page A is used again in the short term, the electronic device skips this memory page A, does not recycle this memory page, and sets the identification bit PG_referenced of this memory page A to 0.

[0160] In Figure 1D the example shown in (b), the identification bit PG_referenced of memory page A is 0, which means that memory page A has not been used within a certain period of time. Then, in order to increase the free memory of the whole machine, the electronic device can recycle memory page A to the memory recycling area. As the memory pages at the tail of the inactive linked list are recycled, the memory pages closer to the head in the inactive linked list move towards the tail.

[0161] Optionally, the memory recycling area includes but is not limited to compression space (such as zram), disk space (such as hard disk, etc.).

[0162] As a possible implementation, when the electronic device recycles a memory page to the compression space, it can be implemented as: compressing the memory page and storing the compressed memory page in the compression space.

[0163] As a possible implementation, when the electronic device recycles a memory page to the disk space, it can be implemented as: swapping out the memory page to the disk space.

[0164] In some embodiments, the electronic device may reclaim some pages stored in memory through a page compression thread. The general way for the page compression thread to reclaim memory is compression. Exemplarily, as Figure 1E shown in (a) of, the anonymous pages are compressed to obtain compressed anonymous pages, and the compressed anonymous pages can be stored in the compressed space of the memory. The memory occupancy of the compressed anonymous pages is less than that of the corresponding anonymous pages. In some scenarios, the allocated anonymous pages in the memory (such as infrequently used anonymous pages, etc.) can also be released. After the anonymous pages are released, they no longer occupy memory. Thus, memory reclaim is achieved, and more free memory is added for application programs to use.

[0165] Subsequently, after detecting a request from a process to use a compressed page, such as Figure 1E shown in (a) of, the page can be decompressed from the compressed space and the decompressed page can be allocated to the process for use.

[0166] In some embodiments, the electronic device may reclaim the compressed pages in memory through a page swapping thread. The way for the page swapping thread to reclaim memory is to swap out the compressed pages in the memory (such as the compressed space) to the disk. Exemplarily, as Figure 1E shown in (b) of, the compressed anonymous pages in the memory (such as the compressed space) are swapped out to the disk for storage, thereby reducing the memory occupancy and facilitating subsequent memory allocation. For example, the page swapping thread sends the compressed anonymous pages stored in the double data rate (DDR) synchronous dynamic random access memory (SDRAM) to the disk for storage through the input / output (I / O) interface. Thus, the memory occupancy in the DDR SDRAM is reduced, facilitating subsequent applications to apply for memory in the DDR SDRAM.

[0167] Subsequently, after detecting a request from a process to use a swapped-out compressed page, such as Figure 1E shown in (b) of, the compressed page (such as the compressed anonymous page) in the disk space can be swapped back to the memory (such as the compressed space) and decompressed, and the decompressed page can be allocated to the process for use.

[0168] The above-mentioned page-out process can also be understood as storing the data in the random access memory (RAM) into the read-only memory (ROM). In this way, a part of the storage space of the ROM can be used as the RAM, achieving the expansion of the storage space of the RAM. For devices with insufficient RAM space itself (such as mobile phones with 2G or 4G of memory), it can significantly reduce the system lag.

[0169] As a possible implementation, when the memory is insufficient, along with the recycling of the memory pages in the inactive list, the memory pages in the active list also have a decay process. For example, Figure 1F taking the memory page A at the tail of the active list as an example, the decay process is as follows:

[0170] 1. In Figure 1F the situation shown in (a) of

[0171] Figure 1F

[0172]

[0173] To sum up, the memory recycling process can be simplified into the following steps: The memory pages in the active list can move towards the tail of the active list. In some cases, the memory pages that meet the conditions at the tail of the active list will migrate to the head of the inactive list, realizing the migration of memory pages between the lists. The memory pages in the inactive list can migrate towards the tail of the inactive list, and the electronic device can recycle the memory pages that meet the conditions from the tail of the inactive list.

[0173] Generally, an application can run in the software system of an electronic device in the form of one or more processes. In some cases, some processes of an application can run in the foreground. The processes running in the foreground usually have a visible interface. Since the foreground processes are usually related to the user-visible interface, the smoothness of the foreground processes usually has a greater impact on the smoothness of the electronic device. Similarly, when an application runs in the foreground, it usually has a visible interface, and the foreground application has a greater impact on the smoothness of the electronic device.

[0174] In some cases, some processes of an application can run in the background. Although background processes usually do not have a visual interface, the running of some background processes also has a significant impact on the fluency of the electronic device. For example, although the background download task is not in the foreground, it directly affects the response latency of the electronic device and the fluency of the electronic device. For another example, after clicking the camera to take a photo, although the background processing process of the photo is invisible, its processing speed affects the user's photo-taking experience and the fluency of the electronic device. Similarly, for some applications running in the background, although they do not have a visual interface, their performance has a great impact on the fluency of the electronic device.

[0175] Currently, the memory of an application can be uniformly managed by the operating system. The operating system can preferentially recycle the memory pages of a target application according to the usage frequency of the memory pages by the application, where the target application is the application with a lower usage frequency of the memory pages. In this memory management scheme, the activity of some background applications may be relatively high, and correspondingly, the usage frequency of the memory pages by the background applications increases. Then, when performing memory recycling, since the usage frequency of the memory pages by the background applications is higher than that by the foreground applications, the memory pages used by the foreground applications are recycled, affecting the performance of the foreground applications and further affecting the fluency of the operation of the electronic device. In particular, when the memory specification of the entire electronic device (such as 3G, 4G) is relatively low, the probability of the memory pages of the foreground applications being recycled increases significantly, and the user experience drops sharply. Exemplarily, Figure 1G for example, there are applications A - D running on the electronic device. Since the background applications B, C, and D have a relatively high usage frequency of the memory pages and the foreground application A has a relatively low usage frequency of the memory pages, it is very likely that the memory pages of the foreground application A will be recycled, affecting the running performance of the foreground application A and further affecting the fluency of the electronic device.

[0176] In addition, in this memory management scheme, it may also cause the memory of some important background applications to be recycled, which may also affect the fluency of the electronic device.

[0177] 6. Memory management mechanism based on a framework

[0178] In some solutions, when the system memory is insufficient, the system can determine the applications that need to be killed according to the memory occupancy of the applications. Optionally, when the free memory is insufficient, the system will preferentially kill the applications with a high memory occupancy. For example, if application A occupies 1G of memory, application B occupies 300M of memory, and application C occupies 200M of memory, then the system can preferentially kill application A, which occupies the most memory.

[0179] In addition to the kernel of the operating system providing a globally unified memory management mechanism, the operating system also provides a memory management method based on a framework or an interface. For example, in the mechanism or Purgeable mechanism of the Android open source project (AOSP), when an application switches to running in the background, the memory that the application temporarily does not need to use can be released. In this way, for the target application that has released part of its memory, since it has released part of its memory, the total memory it occupies is relatively reduced. When the memory of the electronic device is insufficient, since the target application has released part of its memory based on the Purgeable Memory mechanism and occupies less memory, therefore, the probability that the target application is killed by the system is usually reduced, and the keep-alive degree of the target application can be improved. That is, the target application can survive longer in the system by actively releasing part of its memory, so that the user experience of using the target application can be improved.

[0180] Optionally, the memory released by the electronic device includes the memory occupied by any one or more of the following data: files, pictures, dynamically generated view controls, etc.

[0181] Taking the onTrimMemory mechanism as an example, the operating system can detect the running state of the application program. When the memory of the electronic device is insufficient and it is detected that there is an application program in a specific running state, the operating system sends a communication message to the application program in the specific running state. The application program responds to the notification message and calls the onTrimMemory interface to execute the memory release method in the onTrimMemory interface to release part of the memory.

[0182] Among them, the onTrimMemory interface is provided by the system, and the developer of the application program (referred to as the application developer) can implement the memory release method based on the onTrimMemory interface. For example, the application developer can override the onTrimMemory interface and define the memory release method of the application program in the onTrimMemory interface. Subsequently, in different situations, the application can call the onTrimMemory interface to release its own memory to avoid the application being directly killed by the system and improve the user experience of using the application.

[0183] Exemplarily, the onTrimMemory interface can be implemented in the following format:

[0184] onTrimMemory()

[0185] {

[0186] …

[0187] Memory release method ...

[0189] }

[0190] Among them, the running state of the application can be as shown in Table 1 below.

[0191] Table 1

[0192]

[0193] Exemplarily, such as Figure 2 , initially, the application runs in the foreground, and the system service in the operating system detects the running state of the application. Subsequently, when the application switches to running in the background, and the system service monitors that the application has switched from running in the foreground to the TRIM_MEMORY_BACKGROUND (the application has switched to running in the background) running state, the system service sends a notification message to the application, triggering the application to call the onTrimMemory interface and execute the memory release method in the onTrimMemory interface to release some memory to reduce the memory pressure of the system.

[0194] Although the above solution can release a certain number of memories, since the memory release method in the onTrimMemory interface needs to be implemented by the application developer themselves, that is, the system hands over the memory recycling to the application itself. Therefore, the effect of memory release depends to a large extent on the development level and ability of the application developer. Also, because many application developers do not know which memories can be released or for other reasons, most applications do not release memory when they go to the background, or the effect of memory release is not good, or memory release errors cause the system to crash.

[0195] For example, in order to ensure the survival of the application developed by themselves and the user experience, the application developer does not implement the memory release method for background applications in the onTrimMemory interface. In this way, when the overall memory of the system is insufficient, the system will directly kill the high-occupancy background application.

[0196] In addition, the running state of each application changes at all times. It is very likely that during the execution of the onTrimMemory method of the application, the running state of the application suddenly deteriorates, resulting in the application being killed due to high memory occupancy before it has time to release the memory of the application, thus affecting the user experience of using the application.

[0197] In summary, it can be seen that the current memory release method has a poor effect and cannot guarantee the running performance of the electronic device.

[0198] To solve the above technical problems, an embodiment of the present application provides a memory management method. In this method, an electronic device can detect the life cycle of an application and process the memory data used by the function modules corresponding to the application according to the stage of the life cycle in which the application is located. Considering that the performance requirements of an application may be different in different life cycle stages, when the application is in different life cycle stages, the processing methods of the memory data of each function module of the application can be different.

[0199] For example, when the application is in the background playback state, the memory data used by the function module of the application for interface display (such as the memory data used by the rendering thread and the UI thread) is compressed. When the application is in the background caching state, the memory data used by the function module of the application for interface display is swapped out.

[0200] In this way, it can meet the memory processing requirements of the application in different life cycle stages. In other words, on the basis of meeting the performance requirements of the application in the corresponding life cycle stage, it can process the memory from the function module of the application as efficiently as possible, so as to increase the free memory of the electronic device and further improve the overall operating performance of the electronic device.

[0201] The memory management method of the embodiment of the present application can be applied in an electronic device. For example, it can be applied in an electronic device with an AOSP system or a similar system. Optionally, the electronic device can allocate memory in ways such as mmap, standard allocator, kernel allocator, etc. Exemplarily, the electronic device can be, for example, a mobile phone, a tablet computer, a personal computer (PC), a netbook, or other devices that need memory optimization. The specific form of the electronic device is not particularly limited in the present application.

[0202] Taking the electronic device as a mobile phone as an example, Figure 3 FIG. shows the schematic hardware structure of the electronic device 100a. The structures of other electronic devices can refer to the structure of the electronic device 100a.

[0203] The electronic device 100a may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, a barometric pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.

[0204] It can be understood that the structure schematically shown in the embodiments of the present invention does not constitute a specific limitation on the electronic device 100a. In other embodiments of the present application, the electronic device 100a may include more or fewer components than those shown in the figure, or combine certain components, or split certain components, or have different component arrangements. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.

[0205] The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Among them, different processing units may be independent devices or integrated in one or more processors.

[0206] The controller may generate an operation control signal according to the instruction operation code and the timing signal to complete the control of fetching and executing instructions.

[0207] A memory may also be provided in the processor 110 for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory can hold the instructions or data that the processor 110 has just used or recycled. If the processor 110 needs to use the instruction or data again, it can directly call it from the said memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.

[0208] In some embodiments, the processor 110 may include one or more interfaces. The interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.

[0209] Among them, the USB interface 130 is an interface that conforms to the USB standard specification, and specifically may be a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface 130 can be used to connect a charger to charge the electronic device 100a, and can also be used for data transmission between the electronic device 100a and peripheral devices. It can also be used to connect headphones to play audio through the headphones. This interface can also be used to connect other electronic devices, such as AR devices, etc.

[0210] It can be understood that the interface connection relationships between the modules illustrated in the embodiments of the present invention are only illustrative and do not constitute a structural limitation on the electronic device 100a. In other embodiments of the present application, the electronic device 100a may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.

[0211] The charging management module 140 is configured to receive a charging input from a charger. The charger can be a wireless charger or a wired charger. In some embodiments of wired charging, the charging management module 140 can receive the charging input from a wired charger through the USB interface 130. In some embodiments of wireless charging, the charging management module 140 can receive the wireless charging input through the wireless charging coil of the electronic device 100a. While charging the battery 142, the charging management module 140 can also supply power to the terminal through the power management module 141.

[0212] The power management module 141 is used to connect the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives inputs from the battery 142 and / or the charging management module 140 and supplies power to the processor 110, the internal memory 121, the display screen 194, the camera 193, the wireless communication module 160, etc. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle count, and battery health status (leakage, impedance). In some other embodiments, the power management module 141 can also be disposed in the processor 110. In some other embodiments, the power management module 141 and the charging management module 140 can also be disposed in the same device.

[0213] The wireless communication function of the electronic device 100a can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modulation and demodulation processor, and the baseband processor, etc.

[0214] The antenna 1 and the antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in the electronic device 100a can be used to cover a single or multiple communication frequency bands. Different antennas can also be multiplexed to improve the utilization rate of the antennas. For example, the antenna 1 can be multiplexed as the diversity antenna of the wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.

[0215] The mobile communication module 150 may provide a solution for wireless communication including 2G / 3G / 4G / 5G, etc., applied to the electronic device 100a. The mobile communication module 150 may include at least one filter, switch, power amplifier, low noise amplifier (LNA), etc. The mobile communication module 150 may receive electromagnetic waves through the antenna 1, filter, amplify, and perform other processing on the received electromagnetic waves, and then transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 may also amplify the signal modulated by the modulation and demodulation processor and convert it into electromagnetic waves through the antenna 1 for radiation. In some embodiments, at least some functional modules of the mobile communication module 150 may be provided in the processor 110. In some embodiments, at least some functional modules of the mobile communication module 150 and at least some modules of the processor 110 may be provided in the same device.

[0216] The modulation and demodulation processor may include a modulator and a demodulator. Among them, the modulator is used to modulate the low-frequency baseband signal to be transmitted into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. Subsequently, the demodulator transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After being processed by the baseband processor, the low-frequency baseband signal is transmitted to the application processor. The application processor outputs a sound signal through an audio device (not limited to the speaker 170A, receiver 170B, etc.), or displays an image or video through the display screen 194. In some embodiments, the modulation and demodulation processor may be an independent device. In other embodiments, the modulation and demodulation processor may be independent of the processor 110 and be provided in the same device as the mobile communication module 150 or other functional modules.

[0217] The wireless communication module 160 may provide solutions for wireless communications applied to the electronic device 100a, including wireless local area networks (WLANs) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite systems (GNSSs), frequency modulation (FM), near field communication (NFC), infrared (IR), etc. The wireless communication module 160 may be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, performs frequency modulation and filtering processing on the electromagnetic wave signals, and sends the processed signals to the processor 110. The wireless communication module 160 may also receive signals to be sent from the processor 110, perform frequency modulation and amplification on them, and convert them into electromagnetic waves through the antenna 2 for radiation.

[0218] In some embodiments of the present application, the electronic device 100a may establish a wireless connection with other terminals or servers through the wireless communication module 160 (such as a WLAN module) and the antenna 2 to implement communication between the electronic device 100a and other terminals or servers.

[0219] In some embodiments, antenna 1 of electronic device 100a is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, such that electronic device 100a can communicate with a network and other devices via wireless communication technologies. The wireless communication technologies may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technologies, etc. The GNSS may include global positioning system (GPS), global navigation satellite system (GLONASS), beidou navigation satellite system (BDS), quasi-zenith satellite system (QZSS), and / or satellite based augmentation systems (SBAS).

[0220] Electronic device 100a implements a display function via a GPU, display screen 194, and an application processor, etc. The GPU is a microprocessor for image processing, and is connected to display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. Processor 110 may include one or more GPUs, which execute program instructions to generate or change display information.

[0221] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), a Miniled, a MicroLed, a Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100a may include one or N display screens 194, where N is a positive integer greater than 1.

[0222] The electronic device 100a can implement the shooting function through the ISP, the camera 193, the video codec, the GPU, the display screen 194, and the application processor, etc.

[0223] The ISP is used to process the data fed back by the camera 193. For example, when taking a photo, the shutter is opened, and the light passes through the lens and is transmitted to the camera photosensitive element. The optical signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to the ISP for processing and converts it into an image visible to the naked eye. The ISP can also optimize the noise, brightness, and skin color of the image through algorithms. The ISP can also optimize parameters such as the exposure and color temperature of the shooting scene. In some embodiments, the ISP can be set in the camera 193.

[0224] The camera 193 is used to capture static images or videos. The object generates an optical image through the lens and projects it onto the photosensitive element. The photosensitive element can be a charge-coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the optical signal into an electrical signal, and then transmits the electrical signal to the ISP to convert it into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in standard RGB, YUV, etc. formats. In some embodiments, the electronic device 100a may include one or N cameras 193, where N is a positive integer greater than 1.

[0225] The digital signal processor is used to process digital signals. In addition to processing digital image signals, it can also process other digital signals. For example, when the electronic device 100a selects a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy, etc.

[0226] The video codec is used to compress or decompress digital videos. The electronic device 100a can support one or more video codecs. In this way, the electronic device 100a can play or record videos in multiple coding formats, such as: Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.

[0227] The NPU is a neural-network (NN) computing processor. By drawing on the structure of the biological neural network, such as the transmission mode between human brain neurons, it can quickly process the input information and can also continuously self-learn. Through the NPU, applications such as intelligent cognition of the electronic device 100a can be realized, such as: image recognition, face recognition, speech recognition, text understanding, etc.

[0228] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100a. The external memory card communicates with the processor 110 through the external memory interface 120 to implement the data storage function. For example, files such as music and videos are saved in the external memory card.

[0229] The internal memory 121 can be used to store computer-executable program code, and the executable program code includes instructions. The internal memory 121 can include a program storage area and a data storage area. Among them, the program storage area can store the operating system, application programs required for at least one function (such as the sound playback function, image playback function, etc.). The data storage area can store the data created during the use of the electronic device 100a (such as audio data, phone book, etc.). In addition, the internal memory 121 can include high-speed random access memory and can also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc. The processor 110 executes various functional applications and data processing of the electronic device 100a by running the instructions stored in the internal memory 121 and / or the instructions stored in the memory provided in the processor.

[0230] The electronic device 100a can implement audio functions through the audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone jack 170D, and the application processor, etc. For example, music playback, recording, etc.

[0231] The audio module 170 is used to convert digital audio information into an analog audio signal for output, and is also used to convert an analog audio input into a digital audio signal. The audio module 170 can also be used for encoding and decoding audio signals. In some embodiments, the audio module 170 can be disposed in the processor 110, or some functional modules of the audio module 170 can be disposed in the processor 110.

[0232] The speaker 170A, also known as the "loudspeaker", is used to convert an audio electrical signal into a sound signal. The electronic device 100a can listen to music or hands-free calls through the speaker 170A.

[0233] The receiver 170B, also known as the "earpiece", is used to convert an audio electrical signal into a sound signal. When the electronic device 100a answers a call or a voice message, the voice can be listened to by holding the receiver 170B close to the ear.

[0234] The microphone 170C, also known as the "microphone" or "transmitter", is used to convert a sound signal into an electrical signal. When making a call or sending a voice message, the user can speak by bringing the mouth close to the microphone 170C to input the sound signal into the microphone 170C. The electronic device 100a can be provided with at least one microphone 170C. In some other embodiments, the electronic device 100a can be provided with two microphones 170C, which can not only collect sound signals but also implement a noise reduction function. In some other embodiments, the electronic device 100a can also be provided with three, four or more microphones 170C, which can collect sound signals, reduce noise, identify the sound source, and implement functions such as directional recording.

[0235] The headphone jack 170D is used to connect a wired headphone. The headphone jack 170D can be a USB interface 130, or a 3.5mm open mobile terminal platform (OMTP) standard interface, or a cellular telecommunications industry association of the USA (CTIA) standard interface.

[0236] The keys 190 include a power-on key, volume keys, etc. The keys 190 can be mechanical keys or touch keys. The electronic device 100a can receive key inputs to generate key signal inputs related to the user settings and function controls of the electronic device 100a.

[0237] The motor 191 can generate vibration prompts. The motor 191 can be used for incoming call vibration prompts and also for touch vibration feedback. For example, touch operations for different applications (such as taking pictures, playing audio, etc.) can correspond to different vibration feedback effects. For touch operations on different areas of the display screen 194, the motor 191 can also correspond to different vibration feedback effects. Different application scenarios (such as time reminders, receiving messages, alarms, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also support customization.

[0238] The indicator 192 can be an indicator light and can be used to indicate the charging status, power change, and can also be used to indicate messages, missed calls, notifications, etc.

[0239] The SIM card interface 195 is used to connect the SIM card. The SIM card can be inserted into or pulled out from the SIM card interface 195 to achieve contact and separation from the electronic device 100a. The electronic device 100a can support 1 or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, etc. Multiple cards can be inserted into the same SIM card interface 195 at the same time. The types of the multiple cards can be the same or different. The SIM card interface 195 can also be compatible with different types of SIM cards. The SIM card interface 195 can also be compatible with external memory cards. The electronic device 100a interacts with the network through the SIM card to achieve functions such as calls and data communication. In some embodiments, the electronic device 100a uses an eSIM, that is, an embedded SIM card. The eSIM card can be embedded in the electronic device 100a and cannot be separated from the electronic device 100a.

[0240] It should be noted that the structure of the electronic device can also refer to Figure 5 the structure shown. The electronic device can have more or fewer components than Figure 5 the structure shown, or combine certain components, or split certain components, or have different component arrangements. The illustrated components can be implemented in hardware, software, or a combination of software and hardware.

[0241] Optionally, the software system of the electronic device 100a can adopt a layered architecture, an event-driven architecture, a microkernel architecture, a microservices architecture, or a cloud architecture. In the embodiments of the present invention, taking the system of the layered architecture as an example, the software structure of the electronic device 100a is exemplarily described.

[0242] Figure 4 is the software structure block diagram of the electronic device 100a in the embodiments of the present invention.

[0243] The layered architecture divides software into several layers, and each layer has clear roles and divisions of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom: the application layer, the application framework layer, the Android runtime and system libraries, and the kernel layer.

[0244] The application layer may include a series of application packages.

[0245] As Figure 4 shown, the application packages may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message, etc.

[0246] One or more applications may be running on the electronic device, and each application has at least one corresponding process. A process has at least one thread executing tasks. That is, multiple threads are running on the electronic device. Threads include Java threads and C / C++ threads.

[0247] To ensure the normal operation of the threads, the electronic device may allocate processing units (such as CPU cores) to the threads according to a certain strategy. After the threads are allocated processing units, they can execute corresponding tasks through these processing units.

[0248] The application framework layer provides application programming interfaces (APIs) and programming frameworks for the applications in the application layer. The application framework layer includes some predefined functions.

[0249] As Figure 4 shown, the application framework layer may include a first service, a virtual machine, a direct memory allocator, and an mmap interface.

[0250] Among them, the first service is used to detect the running state of the application. When it detects that the running state of the application changes, it sends a notification message to the memory management module in the kernel layer. Optionally, the notification message is used to indicate the running state of the application. After receiving the notification message, the memory management module manages the memory of one or more functional modules of the application according to the running state of the application.

[0251] That is, in the embodiments of the present application, the memory of each functional module can be managed based on the granularity of the functional modules or business types in the application. For example, for the first component of the application, the memory of the first component is managed in a first manner; for the second component of the application, the memory of the second component is managed in a second manner.

[0252] Optionally, the functional modules of the application include, but are not limited to, one or more of the following: components, threads, methods, classes, objects, surfaces (which can correspond to a memory area storing information about the images to be displayed on the screen), and media.

[0253] Optionally, the components include, but are not limited to, any one or more of the following: activities, fragments, services, content providers, and broadcast receivers.

[0254] Optionally, the threads of the application include, but are not limited to, one or more of the following: Render threads and user interface (UI) threads.

[0255] In some examples, different functional modules can correspond to different business types. For example, the above-mentioned Render threads and UI threads can correspond to the business type of interface display.

[0256] Optionally, the running states of the application include, but are not limited to, any one or more of the following: foreground running state, background playback state, background service state, background cache state, light freeze state, and deep freeze state.

[0257] Among them, the foreground running state means that the application is running in the foreground. In this running state, the application can usually present a graphical interface (such as a UI), and the user can interact with the application through the UI.

[0258] The background playback state means that the application switches to the background and no longer presents a graphical interface, but the functions or tasks of the application are still running. For example, a music application runs a music playback task in the background, and a navigation application runs a navigation task in the background.

[0259] The background service state refers to background service applications, which mainly implement background data collection, message push, or resident interrupt waiting services. For example, Bluetooth connection, or the application can push messages in the background, or the application can collect some data to push some messages to the user.

[0260] The background cache state means that after the application switches to the background, it usually no longer runs tasks, and the user has not operated the application for a short period of time (such as the first duration). This part of the application resides in the system background mainly to ensure that when switching back to the foreground from the background, the browsing records previously run in the foreground can be retained so that the user can continue to operate the application and browse the corresponding content.

[0261] Shallow freeze state: It means that after the application is sent to the background, it no longer runs tasks, and the user has not operated the application for a certain period of time (such as the second duration). In this running state, the background application only responds to system behaviors when some specific system events occur, such as responding to the system to perform resolution conversion, etc. Among them, the second duration is greater than the first duration.

[0262] Deep freeze state: It means that after the application is sent to the background, it no longer runs tasks, and the user has not used the application again for a long period of time (such as the third duration). In this running state, the background application no longer responds to system behaviors, and the application completely enters a non-running state. Among them, the third duration is greater than the second duration.

[0263] It should be noted that in the three running states of background cache state, shallow freeze state, and deep freeze state, the application does not run tasks, and the difference between the three states lies in the duration of the application staying in the background. When the application stays in the background and the duration of not running tasks is short (such as the first duration), the running state of the application is in the background cache state. When the application stays in the background and the duration of not running tasks is long (such as the second duration), the running state of the application is in the shallow freeze state. When the application stays in the background and the duration of not running tasks is very long (such as the third duration), the running state of the application is in the deep freeze state. For an application in the background cache state, if it has not been operated by the user (not running tasks) after reaching the second duration, it can be switched to the shallow freeze state. For an application in the shallow freeze state, if it has not been operated by the user (not running tasks) after reaching the third duration, it can be switched to the deep freeze state.

[0264] In the embodiments of the present application, the electronic device can detect the running state of the application and manage the memory used by the functional modules of the application according to the running state of the application. Exemplarily, when the first service detects that the music application switches from the background service state to the background cache state, the first service can send a notification message to the memory management module in the kernel layer to indicate the running state of the music application. After receiving the notification message, the memory management module manages the memory of one or more functional modules of the music application according to the running state of the music application. For example, when it is detected that the running state of the music application switches to the background playback state, considering that the music application may be switched back to the foreground for running in the short term, the memory management module can compress the memory pages of the functional module for display to obtain compressed memory pages, so that when the application is switched back to the foreground, it can be quickly decompressed and used to implement the interface display function.

[0265] For example, the running state of a music application switches to the background cache state, which means that the probability of the user switching the application back to the foreground is low. In order to save memory space, the memory management module can swap out the compressed memory pages of the functional modules used for display, such as swapping them out (or dropping them to disk) to a storage device (such as Flash).

[0266] Optionally, when the application is in the background cache state, the memory management module can also compress the memory of some functional modules of other functional modules of the application (functional modules other than the functional modules used for display). In this way, according to the characteristics and operation requirements of different functional modules of the same application, different methods are used to process the memory of different functional modules, so as to ensure the performance of the application in the corresponding operation state as much as possible, and at the same time improve the free memory of the electronic device.

[0267] For example, when an application is in the background playing state, since the application no longer displays the graphical interface, the electronic device can compress the memory data used by the functional modules of the application for interface display (such as rendering threads), while not processing the memory data used by some functional modules supporting background operation (such as services). In this way, the normal operation of the application in the background playing state can be guaranteed as much as possible, while increasing the free memory of the electronic device.

[0268] The virtual machine is used to manage heap memory. As a possible implementation method, the virtual machine calls the mmap interface to allocate heap memory. The operating system records the heap memory allocated by the virtual machine.

[0269] The direct memory allocator is used to allocate direct memory. As a possible implementation method, the virtual machine calls the mmap interface to allocate direct memory.

[0270] The hardware abstraction layer (HAL) includes a streaming media memory allocator. The streaming media memory allocator is used to allocate streaming media memory. Optionally, the streaming media memory allocator includes a dma_buf memory allocator or an ION memory allocator.

[0271] The kernel layer is the layer between hardware and software. The kernel layer may include at least display driver, camera driver, audio driver, and sensor driver.

[0272] In some embodiments of the present application, the kernel layer may include a memory management module for managing the memory of the application. For example, after receiving a notification message (for indicating the running state of the application) from the first service, the memory of one or more functional modules of the application is managed according to the running state of the application.

[0273] It should be noted that Figure 4The software architecture shown is just an example. The software architecture of the electronic device can also be in other forms. For example, the first service is set in other layers. The embodiments of the present application do not limit the software architecture of the electronic device.

[0274] As Figure 5 Fig. shows another possible structure of the electronic device. The electronic device may include a processor 401 (optionally including a processor 408), a memory 403, a transceiver 404, etc.

[0275] Among them, there may be a path between the above components for transmitting information between the above components.

[0276] The transceiver 404 is used to communicate with other devices or communication networks using protocols such as Ethernet, WLAN, etc.

[0277] For the detailed content of the processor and memory, please refer to Figure 2 the description of the relevant structure in the electronic device, which will not be elaborated here.

[0278] The technical solutions provided by the embodiments of the present application will be described in detail below. Optionally, the physical memory in the electronic device can be divided into multiple memory units. Optionally, the memory units include but are not limited to memory pages. The following embodiments mainly take memory pages as memory units for illustration, but the technical solutions of the embodiments of the present application are not limited thereto.

[0279] In the embodiments of the present application, the life cycle of the application can be detected, and the memory can be managed according to the life cycle of the application. For example, for the memory data that will not be used in the short term, this part of the memory data can be compressed and stored in the compression space to save a part of the memory space. For the compressed memory data that will not be used in the long term (such as compressed memory pages), this part of the compressed memory data can be swapped out, such as through a disk-offloading mechanism, and swapped out to the disk space (such as on a Flash device).

[0280] Optionally, the life cycle of the application includes one or more of the following running stages: foreground running, background playback, background service, background caching, light freeze, deep freeze. When the application is in the foreground running stage, it can also be said that the running state of the application is the foreground running state. Similarly, when the application is in the background playback stage, it can also be said that the running state of the application is the background playback state. When the application is in the background service stage, it can also be said that the running state of the application is the background service state. When the application is in the background caching stage, it can also be said that the running state of the application is the background caching state. When the application is in the light freeze stage, it can also be said that the running state of the application is the light freeze state. When the application is in the deep freeze stage, it can also be said that the running state of the application is the deep freeze state.

[0281] As follows, the application lifecycle and the technical solutions of the embodiments of the present application will be introduced in conjunction with the accompanying drawings.

[0282] Exemplarily, taking the navigation application in a mobile phone as an example, such as Figure 6 , assuming that the navigation application is initially running in the foreground, in order to ensure the normal operation of the navigation application in the foreground, the mobile phone may not process the memory of the navigation application.

[0283] At time t1, the mobile phone detects that the user switches the navigation application to run in the background. Assuming that the navigation application is still running the navigation function in the background, such as determining the navigation route and playing the navigation route voice, it is determined that the navigation application enters the background playback state. Considering that when the navigation application runs in the background, the graphical interface is no longer displayed (the display function is no longer executed), therefore, the memory related to the interface display can be processed.

[0284] Also considering that the navigation application may be switched back to the foreground for operation in the short term, as a possible implementation, when the application is in the background playback state (at this time, although the application runs in the background, it may still be executing some core functions), processing the memory related to the interface display can be implemented as: compressing the memory data related to the interface display of the application to obtain the compressed memory data related to the interface display and storing it in the compression space, rather than swapping out the compressed memory data related to the interface display to the disk space (such as a Flash device). In this way, it is possible to avoid the long time caused by reading data from the disk again, and the application is delayed in switching back to the foreground for operation.

[0285] Exemplarily, such as Figure 6 , during the time period from t1 to t2, the mobile phone can process the memory data related to the interface display. The memory data related to the interface display includes but is not limited to: the memory data related to the user interface thread and the memory data related to the rendering thread.

[0286] After that, at time t2, the mobile phone detects that the navigation application running in the background no longer runs core functions such as navigation. For example, the current navigation task has ended, and the user has not added a new navigation task. Then, the mobile phone can determine that the navigation application has entered the background service state. Considering that when the navigation application is in the background service state, it no longer runs navigation tasks / functions, therefore, the mobile phone can, on the basis of the previous memory processing (that is, processing the memory data related to the interface display), process the memory data related to core functions such as navigation. The navigation function includes but is not limited to determining the navigation route and playing the navigation audio.

[0287] Optionally, processing the memory data related to core functions such as navigation can be implemented as: compressing the memory data related to core functions such as navigation and storing the compressed memory data related to this part of core functions such as navigation in the compression space.

[0288] Exemplarily, such as Figure 6 , during the time period from t2 to t3, on the basis of displaying relevant memory data (such as memory data related to the rendering thread and the user interface thread) on the processed interface, the mobile phone can also process memory data related to the core functions of the application (such as navigation). The memory data related to the navigation function includes but is not limited to: memory data related to the surface thread and memory data related to the media thread.

[0289] As the running time of the navigation application in the background becomes longer, for example, at time t3, when the running duration of the navigation application in the background reaches the first duration, the navigation application enters the background cache state. Considering that when the application is in the background cache state, the probability that the user switches the navigation application back to the foreground is relatively low. Therefore, as a possible implementation method, the mobile phone can swap out the memory data for interface display of the navigation application to the disk space (such as on a Flash device).

[0290] Exemplarily, such as Figure 6 , during the time period from t3 to t4, the mobile phone swaps out the memory data related to interface display (such as memory data related to the rendering thread and the user interface thread) to the disk space.

[0291] Optionally, considering that during the time period from t3 to t4 (the navigation application is in the background cache state), the probability that the user operates the navigation application in the short term is relatively low, or the user may no longer need the navigation application to provide background services in the short term, the mobile phone can compress the memory data used by the service. Similarly, as Figure 6 shown, during the time period from t3 to t4, the mobile phone can also compress the relevant memory data used by media and surface.

[0292] The navigation application continues to run in the background. At time t4, when the running duration of the navigation application in the background reaches the second duration, the navigation application enters the light freeze state. Considering that in this state, the possibility that the navigation application is active in the background is greatly reduced. Therefore, as a possible implementation method, the mobile phone can swap out all the memory data other than the heap memory data of the Java virtual machine to the disk space (such as a Flash device).

[0293] Exemplarily, such as Figure 6 , during the time period from t4 to t5, the mobile phone swaps out the memory data related to the user interface thread, the rendering thread, surface, media, and service to the disk space. In this way, it is possible to process as much memory as possible from inactive background applications to increase the free memory quantity of the whole machine, thereby improving the running performance of the whole machine.

[0294] The navigation application continues to run in the background. At time t5, when the duration of the navigation application running in the background reaches the third duration, the navigation application enters the deep freeze state. The navigation application being in the deep freeze state indicates that the navigation application has not been used by the user again for a relatively long period of time. Based on this, the mobile phone can predict that the navigation application will still not be used by the user again in the next relatively long period. To reduce the memory occupied by inactive background applications, the mobile phone can compress the heap memory data of the Java virtual machine of the navigation application in the deep freeze state and store it in the compressed space. In this way, not only can the memory occupancy of the heap memory data be reduced, but also since the heap memory data is compressed into the compressed space (not swapped out to the Flash device), when a subsequent process wants to use the heap memory data, it only needs to decompress the compressed heap memory data, without having to swap the heap memory data back from the disk space, which can avoid the delay caused by swapping back the heap memory data, and thus can reduce the lag when the user switches the navigation application back to the foreground.

[0295] Exemplarily, such as Figure 6 , during the time period from t5 to t6, on the basis of swapping out the memory data related to the user interface thread, rendering thread, surface, media, and service to the disk space, the mobile phone can compress the heap memory data of the Java virtual machine (such as the memory data related to method, class, and object) and store the compression result in the compressed space.

[0296] In the above solution, the electronic device can detect the life cycle of the application and process the memory data used by some function modules corresponding to the application according to the stage of the life cycle in which the application is located.

[0297] First of all, since the memory processing is based on the granularity of the function modules of the application, the fineness of the memory processing is higher. In some scenarios, processing the memory data used by some function modules of the application (such as the function modules that will not be used temporarily) will not affect the normal operation of the entire application, which can improve the keep-alive degree of the application and reduce the probability of the application being killed or abnormally exited. For example, for some background applications, the electronic device can only process the memory used by some function modules of the background application. In this way, while ensuring the survival of the background application as much as possible, a large amount of memory of the application can be released, enhancing the fluency and stability of the electronic device (avoiding memory thrashing), and improving the memory management efficiency of the electronic device.

[0298] Secondly, compared to the related art, which relies on the onTrimMemory mechanism and expects the application to actively release memory data, resulting in the application not actually releasing memory data, and the system's memory pressure is still relatively large, the technical solution of the embodiment of the present application does not rely on the application to actively release memory data. Specifically, in the embodiment of the present application, when it is detected that the application is in the corresponding stage of the life cycle, the electronic device can automatically process memory data from some functional modules of the application. In this way, on the one hand, it helps to alleviate the memory pressure of the system. On the other hand, the electronic device processes the corresponding memory data from some functional modules of the application, so that the total memory occupied by the application is reduced. Therefore, the probability of the application being killed or abnormally exiting will be greatly reduced.

[0299] In addition, when the application is in different life cycle stages, the memory data of each functional module of the application can be processed differently. In this way, the memory processing requirements of the application at different life cycle stages can be met. In other words, on the basis of meeting the performance requirements of the application at the corresponding life cycle stage, as much memory as possible can be processed from the functional modules of the application, so as to increase the free memory of the electronic device, thereby improving the overall operation performance of the electronic device.

[0300] The technical details involved in the embodiments of the present application are introduced as follows.

[0301] Electronic devices can implement memory management based on virtual memory technology. A thread initiates a memory allocation request. After receiving the memory allocation request, the electronic device allocates virtual memory areas (VMA) and physical memory to the thread through a memory allocator. There is a mapping relationship (or association relationship) between the virtual address of the thread's VMA and the physical address of the physical memory. Optionally, the physical memory may include one or more linked lists, each of which includes one or more memory pages.

[0302] Optionally, the electronic device may mark the assigned VMA. For example, Figure 7 , shows the VMAs allocated by the memory allocator of the electronic device to the user interface (UI) thread and the rendering thread, wherein the VMA corresponding to the UI thread and the VMA corresponding to the rendering thread may have different identifiers to distinguish the threads associated with the VMAs. In some examples, after the memory allocator marks the VMA, the identification information of the VMA may be passed to the memory management module, and the memory management module may distinguish the threads associated with different VMAs according to the identification information of the VMA.

[0303] Optionally, the electronic device can also mark memory pages in the allocated physical memory. For example, different marks can be set for memory pages used by different functional modules. For example, the mark for the memory page used by the UI thread is set to mark 1, and the mark for the memory page used by the rendering thread is set to mark 2. In this way, the functional modules (such as threads) or business types associated with different memory pages can be distinguished.

[0304] After allocating a VMA for a thread, the thread can apply to use the memory pages mapped by the VMA. As a possible implementation, the virtual address A' of the memory page to be used can be carried in the usage request, and the virtual address A' includes an offset. The electronic device can obtain the base address of the physical address A corresponding to the virtual address A' by querying the page table, and calculate the physical address A based on the base address and the offset in the virtual address A', so as to address the physical address A of the memory page, enabling the thread to use the memory page in the physical address A.

[0305] Similarly, the process of the electronic device allocating a VMA and physical memory for other functional modules of the application can refer to the relevant description of the electronic device allocating a VMA and physical memory for the thread of the application.

[0306] In some scenarios, if it is detected that the running state of the application has changed, the electronic device can manage the memory data used by one or more functional modules of the application according to the running state of the application.

[0307] Figure 8 Illustrates an exemplary process of the memory management method according to an embodiment of the present application. As Figure 8 , the method includes the following steps:

[0308] S101. The first service detects that the running state of the application has changed.

[0309] Optionally, the first service is located in the framework layer.

[0310] S102. The first service sends message A to the memory management module.

[0311] Among them, message A is used to indicate that the running state has changed.

[0312] It should be noted that in the embodiments of the present application, when it is mentioned that a certain message is used for a certain purpose, it means that the message can be used for that purpose, rather than the message being dedicated to that purpose.

[0313] Optionally, the memory management module is located in the kernel layer.

[0314] S103. The memory management module sends message B to the processing thread.

[0315] Among them, message B is used to instruct the processing thread to perform memory processing.

[0316] S104. In response to message B, the processing thread reclaims the memory used by the function modules of the application.

[0317] As a possible implementation, the processing thread processes the memory pages used by the application according to the function modules (or business types) associated with the memory pages. Optionally, for a certain running state, the processing thread processes as many non-critical memory pages (or non-important memory pages) used by the application as possible, and processes as few critical memory pages as possible. The division method of critical memory pages and non-critical memory pages is related to the function modules (or business types) associated with the memory pages in a specific running state.

[0318] For example, for a foreground application, all the memory pages used by the application can be regarded as critical memory pages. For an application in the background playback state, since the application is no longer displaying the UI in the foreground, therefore, the services related to the interface display are regarded as non-critical services, or rather, the application function modules related to the interface display are regarded as non-critical function modules. Correspondingly, the memory pages related to the interface display (such as the memory pages used by the UI thread, rendering thread, and layer composition thread) can be regarded as non-critical memory pages. Correspondingly, for an application in the background playback state, the electronic device can process as many memory pages related to the interface display as possible to reduce the memory pressure of the electronic device and improve the performance of the electronic device.

[0319] For another example, for an application in the background cache state, the memory pages related to the interface display and surface, media, and service can be regarded as non-critical memory pages. The electronic device can process this part of non-critical memory pages. Optionally, the non-critical memory pages can include two types, one is the memory pages that will not be used in a short time, and the other is the memory pages that will not be used in a long time. For the non-critical memory pages that will not be used in a short time (such as the memory pages used by surface, media, and service in the background cache state), the electronic device can compress this part of non-critical memory pages and store them in the compression space. For the non-critical memory pages that will not be used in a long time (such as the memory pages related to the interface display in the background cache state), the electronic device can swap out this part of non-critical memory pages and store them in the disk space.

[0320] Compared with the related art in which setting a waterline to trigger memory processing will highly probably result in the mismatch between the quantity of memory processing and the actual memory usage requirements of an electronic device, the memory management method provided by the embodiments of the present application can determine the critical services (or critical functional modules) and non-critical services (or non-critical functional modules) corresponding to an application in different running states, and based on the critical services and non-critical services, for the memory actually required by the critical services of the application (critical memory), the electronic device does not perform processing, and for the memory actually not required by the critical services (or critical functional modules) of the application (i.e., non-critical memory), the electronic device performs processing. In this way, the actual memory usage requirements of the application can be matched, and an appropriate amount of memory can be processed.

[0321] The memory processing process of the processing thread is described below by taking a navigation application as an example. In one example, if it is detected that the running state of the navigation application switches from the background service state to the background cache state, the electronic device can wake up the processing thread (such as kswapd). Exemplarily, Figure 9 , the processing thread can process the memory used by one or more functional modules of the application according to the current running state of the navigation application. As a possible implementation, the processing thread can scan the linked list 1 mapped by the VMA of the UI thread of the navigation application; scan the linked list 2 mapped by the VMA of the rendering thread of the navigation application; scan the linked list 3 mapped by the VMA of the surface of the navigation application; scan the linked list 4 mapped by the VMA of the media of the navigation application; scan the linked list 5 mapped by the VMA of the service of the navigation application.

[0322] Among them, for the memory pages in the linked list 1 that meet the processing conditions (i.e., the memory pages related to the operation of the UI thread), since the navigation application has been running in the background for a long time and the probability of the navigation application switching back to the foreground in a short time is very small, the processing thread can swap out the compressed memory pages related to the UI thread (such as memory pages A and B) to the disk space. Similarly, for the memory pages in the linked list 2 that meet the processing conditions (i.e., the memory pages related to the operation of the rendering thread), the processing thread can swap out this part of the memory pages (such as memory page C) to the disk space.

[0323] For the memory pages in the linked list 3 that meet the processing conditions (memory pages related to the surface), considering that the probability of the user operating the navigation application in the short term is relatively low, then, the processing thread can compress this part of the memory pages (such as memory page D) into the compression space. Similarly, the processing thread can compress the processable memory pages in the linked list 4 (memory pages related to the media) into the compression space, and compress the processable memory pages in the linked list 5 (memory pages related to the service) into the compression space.

[0324] It should be noted that Figure 9 Taking the case where the VMA of a functional module corresponds to a linked list as an example for illustration, in some other embodiments, it may also be that the VMA of a functional module corresponds to multiple linked lists, or the VMAs of multiple functional modules correspond to one linked list, or the VMAs of multiple functional modules cross-correspond to multiple linked lists. For example, the VMAs of the UI thread and rendering thread of an application are mapped to linked list 1, and the VMAs of the remaining functional modules of the application are mapped to linked list 2. The embodiments of the present application do not limit the mapping relationship between the VMA of the functional module and the physical memory.

[0325] Exemplarily, such as Figure 10 , if it is detected that the running state of the navigation application switches to the light freeze state, the electronic device can wake up the processing thread, and the processing thread can process the memory used by one or more functional modules of the application according to the current running state of the navigation application. For example, the processing thread can scan linked list 1 mapped by the VMA of the UI thread of the navigation application, and swap out the memory pages (i.e., the memory pages used by the UI thread) in linked list 1 that meet the processing conditions to the disk space. Similarly, the processing thread can swap out the memory pages used by other functional modules of the navigation application (such as the memory pages used by the rendering thread, surface, media, and service).

[0326] The above mainly takes the case where after the application switches the running state, the electronic device can process some memory of the application according to the running state of the application as an example for illustration. In some other embodiments, after the application switches the running state, the electronic device can delay the processing. In other words, the electronic device can perform memory processing after a period of time after the application switches the running state to ensure the stable running state of the application. Exemplarily, such as Figure 11 , when the application enters the background playback state at time t1, in order to prevent the user from switching the application back to the foreground operation in the short term, the electronic device can delay for a period of time. For example, the delay duration is t1 - t1'. If the user does not switch the application back to the foreground operation during the period from t1 - t1 to t1, then the electronic device predicts that the application will not be switched back to the foreground in a short period of time in the future, and the electronic device can process the memory related to the interface display (such as the memory used by the rendering thread and UI thread) at time t1'.

[0327] In some embodiments, the electronic device can provide a setting entry for memory management, and the user can set the related functions of memory management through the setting entry. Exemplarily, such as Figure 12, the display settings interface 110 of the electronic device (an example of the first interface), the interface 110 includes a setting option 1101, and this setting option 1101 is used to turn on or off the memory management function. After the memory management function is turned on, the electronic device can manage the memory used by one or more functional modules of the application according to the running state of the application, with the functional module as the granularity. Or, in some other embodiments, the electronic device can always turn on the memory management function. Or, the electronic device can automatically turn on the memory management function when certain conditions are met.

[0328] The following Table 2 shows the performance indicators of the electronic device with the memory management function turned on and off.

[0329] Table 2

[0330]

[0331]

[0332] It should be noted that the above only gives several examples of the application running state. In some other embodiments, the running state or the stages of the life cycle of the application can be divided separately, and, in all cases, the above technical solutions can be used to process a part of the memory of the application (the memory of non-critical services in the corresponding running state) according to the running state of the application (or the stage of the life cycle where the application is located).

[0333] In some solutions, multiple embodiments of the present application can be combined and the combined solution can be implemented. Optionally, some operations in the processes of the method embodiments are optionally combined, and / or the order of some operations is optionally changed. And, the execution order between the steps of each process is only exemplary and does not constitute a limitation on the execution order between the steps. There can also be other execution orders between the steps. It is not intended to indicate that the execution order is the only order in which these operations can be executed. Those of ordinary skill in the art will think of various ways to reorder the operations described herein. Additionally, it should be noted that the process details involved in a certain embodiment herein are also applicable to other embodiments in a similar manner, or different embodiments can be combined and used.

[0334] In addition, some steps in the method embodiments can be equivalently replaced with other possible steps. Or, some steps in the method embodiments can be optional and can be deleted in some usage scenarios. Or, other possible steps can be added to the method embodiments.

[0335] And, each method embodiment can be implemented alone or in combination.

[0336] It can be understood that, in order to implement the above functions, the electronic device in the embodiments of the present application includes the corresponding hardware structures and / or software modules for executing each function. Combining the units and algorithm steps of each example described in the embodiments disclosed in the present application, the embodiments of the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a certain function is executed in the way of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the technical solution of the embodiments of the present application.

[0337] The embodiments of the present application can divide the functional units of the electronic device according to the above method examples. For example, each functional unit can be divided corresponding to each function, or two or more functions can be integrated into one processing unit. The above integrated unit can be implemented in the form of hardware or in the form of a software functional unit. It should be noted that the division of units in the embodiments of the present application is illustrative, only a logical function division, and there may be other division methods in actual implementation.

[0338] Figure 13 FIG. shows a schematic block diagram of a memory management device provided in the embodiments of the present application. The device can be the above-mentioned first electronic device or a component with corresponding functions. The device 1700 can exist in the form of software and can also be a chip available for the device. The device 1700 includes: a processing unit 1702. The processing unit 1702 can be used to support Figure 8 the S101, S104, etc. shown, and / or other processes of the solutions described herein.

[0339] Optionally, the device 1700 may further include a communication unit 1703. Optionally, the communication unit 1703 can also be divided into a sending unit (not shown in Figure 13 and a receiving unit (not shown in Figure 13 . Among them, the sending unit is used to support the device 1700 to send information to other electronic devices. The receiving unit is used to support the device 1700 to receive information from other electronic devices.

[0340] Optionally, the device 1700 may further include a storage unit 1701 for storing the program code and data of the device 1700. The data may include, but is not limited to, original data or intermediate data, etc.

[0341] In a possible way, the processing unit 1702 can be a controller or Figure 5The processor 401 and / or 408 shown, for example, can be a Central Processing Unit (CPU), a general-purpose processor, a Digital Signal Processing (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It can implement or execute various exemplary logical blocks, modules, and circuits described in connection with the disclosure of this application. The processor can also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and so on.

[0342] In one possible way, the communication unit 1703 can include Figure 5 the transceiver 404 shown, and can also include a transceiver circuit, radio frequency devices, etc.

[0343] In one possible way, the storage unit 1701 can be Figure 5 the memory 403 shown.

[0344] This application embodiment also provides an electronic device, including one or more processors and one or more memories. The one or more memories are coupled to the one or more processors, and the one or more memories are used to store computer program code. The computer program code includes computer instructions. When the one or more processors execute the computer instructions, the electronic device is caused to execute the above-related method steps to implement the method in the above embodiment.

[0345] This application embodiment also provides a chip system, including: a processor, the processor is coupled to a memory, and the memory is used to store programs or instructions. When the programs or instructions are executed by the processor, the chip system is caused to implement the method in any of the above method embodiments.

[0346] Optionally, the processor in the chip system can be one or more. The processor can be implemented by hardware or by software. When implemented by hardware, the processor can be a logic circuit, an integrated circuit, etc. When implemented by software, the processor can be a general-purpose processor and is implemented by reading software code stored in the memory.

[0347] Optionally, there may also be one or more memories in the chip system. The memory may be integrated with the processor or may be separately provided from the processor, which is not limited in this application. Exemplarily, the memory may be a non-transitory processor, such as a read-only memory (ROM), which may be integrated with the processor on the same chip or may be separately provided on different chips. This application does not specifically limit the type of the memory and the setting manner of the memory and the processor.

[0348] Exemplarily, the chip system may be a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), a system on chip (SoC), a central processing unit (CPU), a network processor (NP), a digital signal processing circuit (DSP), a micro controller unit (MCU), a programmable logic device (PLD), or other integrated chips.

[0349] It should be understood that the steps in the above method embodiments may be completed by the integrated logic circuit in the hardware of the processor or the instructions in the form of software. The method steps disclosed in combination with the embodiments of this application may be directly embodied as being executed and completed by the hardware processor, or executed and completed by the combination of the hardware and software modules in the processor.

[0350] The embodiments of this application further provide a computer-readable storage medium, in which computer instructions are stored. When the computer instructions run on an electronic device, the electronic device is enabled to execute the above related method steps to implement the method in the above embodiments.

[0351] The embodiments of this application further provide a computer program product. When the computer program product runs on a computer, the computer is enabled to execute the above related steps to implement the method in the above embodiments.

[0352] In addition, the embodiments of this application further provide a device, which may specifically be a component or a module. The device may include a processor and a memory connected to each other. Among them, the memory is used to store computer execution instructions. When the device runs, the processor may execute the computer execution instructions stored in the memory so that the device executes the methods in the above method embodiments.

[0353] Among them, the electronic device, computer-readable storage medium, computer program product or chip provided in the embodiments of the present application are all used to execute the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods provided above, and will not be elaborated here.

[0354] It can be understood that, in order to implement the above functions, the electronic device includes corresponding hardware and / or software modules for executing each function. Combining the algorithm steps of each example described in the embodiments disclosed in this article, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a certain function is executed in the way of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to exceed the scope of the present application.

[0355] In this embodiment, the electronic device can be divided into functional modules according to the above method examples. For example, each functional module can be divided corresponding to each function, or two or more functions can be integrated into one processing module. The above integrated module can be implemented in the form of hardware. It should be noted that the division of modules in this embodiment is illustrative, only a logical function division, and there may be other division methods in actual implementation.

[0356] Through the description of the above embodiments, those skilled in the art can clearly understand that, for the convenience and conciseness of description, only the above division of each functional module is used as an example. In actual applications, the above functions can be allocated to different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. The specific working processes of the systems, devices and units described above can refer to the corresponding processes in the foregoing method embodiments, and will not be elaborated here.

[0357] In several embodiments provided in the present application, it should be understood that the disclosed methods can be implemented in other ways. For example, the terminal device embodiments described above are only illustrative. For example, the division of the modules or units is only a logical function division, and there may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection to each other can be through some interfaces, and the indirect coupling or communication connection of the modules or units can be in an electrical, mechanical or other form.

[0358] The unit described as a separation component may or may not be physically separated. The component shown as a unit may or may not be a physical unit, that is, it may be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0359] In addition, each functional unit in various embodiments of the present application can be integrated into a processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of a software functional unit.

[0360] If the above-mentioned 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 computer-readable storage medium. Based on such an understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to execute all or part of the steps of the methods described in various embodiments of the present application. The aforementioned storage medium includes: flash memory, mobile hard disk, read-only memory, random access memory, magnetic disk, or optical disk and other various media that can store program instructions.

[0361] The above is only the specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application should be covered by the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.

Claims

1. A memory management method, characterized in that, The method is applied to an electronic device, and the method includes: Detecting that the running state of a first application switches from a first running state to a second running state, and processing a part of the memory used by the first application; the part of the memory is the memory associated with the target service of the first application; the target service is the non-critical service of the first application in the second running state; the memory associated with the target service of the first application includes a first part of the memory and a second part of the memory, and the first part of the memory is different from the second part of the memory; Wherein, the processing of the part of the memory of the first application specifically includes: processing the first part of the memory in a first manner and processing the second part of the memory in a second manner, and the first manner is different from the second manner.

2. The method according to claim 1, characterized in that, Detecting that the running state of a first application switches from a first running state to a second running state, and processing a part of the memory used by the first application, includes: detecting that the running state of the first application switches from the first running state to the second running state, and the duration of the first application in the second running state reaches a first threshold, and processing a part of the memory used by the first application.

3. The method according to claim 1 or 2, characterized in that, The target service includes a first service and / or a second service; the memory associated with the first service includes the first part of the memory, and the memory associated with the second service includes the second part of the memory; The first part of the memory is: a page that has not been used by the first application within a first time period; The second part of the memory is: a compressed page that has not been used by the first application within a second time period.

4. The method according to claim 3, wherein The duration of the second time period is greater than the duration of the first time period.

5. The method according to any one of claims 1-4, characterized in that, The second running state includes a first background running state; Detecting that the running state of a first application switches from a first running state to a second running state, and processing a part of the memory used by the first application, includes: Detecting that the running state of the first application switches from the first running state to the first background running state, and compressing the first part of the memory associated with the first service.

6. The method according to claim 3 or 4, characterized in that The second running state includes a first background running state; Detecting that the running state of a first application switches from a first running state to a second running state, and processing a part of the memory used by the first application, includes: Detecting that the running state of the first application switches from the first running state to the first background running state, and swapping out the second part of the memory associated with the second service to disk space.

7. The method according to any one of claims 1-6, characterized in that, The first running state is a foreground running state.

8. The method according to any one of claims 1-6, characterized in that, The first running state is a second background running state.

9. The method according to claim 3 or 4, characterized in that, The method further includes: Detecting that the running state of the first application switches from the first background running state to a third background running state; Compressing a third part of the memory of the first application, where the third part of the memory is: the memory that has not been used by the first application within the first time period; And / or, Swap out the fourth part of the memory of the first application to disk space, where the fourth part of the memory is the memory not used by the first application during the second time period; the duration of the fourth time period is greater than the duration of the third time period.

10. The method according to any one of claims 1-9, characterized in that, Process the part of the memory used by the first application, including: Obtain the virtual memory space VMA corresponding to the part of the memory and the linked list corresponding to the VMA; the linked list includes the part of the memory. Process the part of the memory in the linked list.

11. The method according to claim 5 or 6, characterized in that, The method further includes: Display a first interface. Receive an operation input by the user on the first interface, where the operation is used to enable the memory management function.

12. The method according to claim 5 or 6, characterized in that, The first background running state includes the following states: background playback state, background service state, background cache state, shallow freeze state, deep freeze state; Among them, in the background playback state, the first application executes a first task in the background. In the background service state, the first application provides a background service in the background, and the first application does not execute the first task in the background. In the background cache state, the first application does not execute the first task in the background, does not provide a background service, and the duration for which the first application is in the background running state reaches a first duration. In the shallow freeze state, the first application does not execute the first task in the background, does not provide a background service, and the duration for which the first application is in the background running state reaches a second duration; the second duration is greater than the first duration. In the deep freeze state, the first application does not execute the first task in the background, does not provide a background service, and the duration for which the first application is in the background running state reaches a third duration; the third duration is greater than the second duration.

13. The method according to claim 12, wherein In the background playback state, the first service includes services related to interface display. In the background service state, the first service includes services corresponding to the first task. In the background cache state, the first service includes background services, and the second service includes services related to interface display. In the shallow freeze state, the second service includes: services corresponding to the first task, background services. In the deep freeze state, the first service includes services corresponding to objects, classes, and methods.

14. An electronic device, characterized in that, including: A processor and a memory, the memory is coupled to the processor, the memory is used to store computer program code, the computer program code includes computer instructions, and the processor reads the computer instructions from the memory so that the electronic device executes the method according to any one of claims 1-13.

15. A computer-readable storage medium, characterized in that, including computer instructions, when the computer instructions run on an electronic device, causing the electronic device to execute the method according to any one of claims 1-13.

16. A computer program product, characterized in that, When the computer program product runs on an electronic device, causing the electronic device to execute the method according to any one of claims 1-13.