Memory management method and electronic device

By detecting the operating status of the application and automatically recycling non-critical business-related memory, the problem of low memory management of electronic devices is solved, the system fluency and application stability are improved, and more efficient memory resource utilization is achieved.

CN117519959BActive Publication Date: 2025-01-14HUAWEI TECH CO LTD

Patent Information

Application Number
CN202210912594.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-07-30
Publication Date
2025-01-14
Estimated Expiration
2042-07-30

AI Technical Summary

Technical Problem

The existing technology is difficult to effectively manage the memory resources of electronic devices, resulting in insufficient free memory in the system, affecting the smoothness of the application and user experience.

Method used

Memory management is carried out using granularity based on application services or functional modules, detect the operating status of the application and automatically recycle memory associated with non-critical services, avoid relying on applications to actively release memory, and optimize memory usage through memory compression and swap out mechanisms.

Benefits of technology

It improves the memory management performance of electronic devices, reduces the probability of applications being detected or abnormally exited, enhances system fluency and stability, and improves the operating performance of the entire machine.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117519959B_ABST
    Figure CN117519959B_ABST
Patent Text Reader

Abstract

A memory management method and electronic device, relating to the field of terminal technology, can perform memory management based on the granularity of the business or functional module of an application program, thereby improving the memory management performance of the electronic device. The method is applied to an electronic device, and the method includes: detecting the running state of a first application; detecting that the running state of the first application switches from the first running state to the second running state, and processing part of the memory used by the first application; the part of the memory is the memory associated with the target business of the first application; the target business is the non-critical business of the first application in the second running state.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of terminal technology, and in particular to a memory management method and an electronic device. Background Art

[0002] At present, with the popularity of electronic devices such as mobile phones, users have higher and higher requirements for the fluency of applications. In electronic devices, memory is one of the most important system resources. If the system has insufficient free memory, the fluency of the application will be greatly reduced, causing the application to freeze and affecting the user experience. Therefore, it is urgent to propose an effective memory management method to improve the performance of electronic devices. Summary of the invention

[0003] In order to solve the above technical problems, the embodiments of the present application provide a memory management method and an electronic device. The technical solution provided by the embodiments of the present application can perform memory management based on the granularity of the business or functional module of the application program, thereby improving the memory management performance of the electronic device.

[0004] In order to achieve the above technical objectives, the embodiments of the present application provide the following technical solutions:

[0005] In a first aspect, a memory management method is provided, which is applied to an electronic device or a component (such as a chip system) capable of realizing a function of an electronic device, and the method comprises:

[0006] Detecting the running state of the first application; detecting that the running state of the first application switches from the first running state to the second running state, and processing part of the memory used by the first application; the part of the memory is the memory associated with the target business of the first application; the target business is the non-critical business of the first application in the second running state.

[0007] For example, taking the first application as a navigation application, Figure 6 , detecting 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 non-critical business memory used by the navigation application (such as the memory used by the rendering thread and the user interface thread).

[0008] Among them, processing the memory includes recycling the memory. First of all, since the memory processing is performed based on the granularity of the functional modules or services of the application, the memory processing is more sophisticated. In some scenarios, processing some functional modules of the application (such as functional modules that will not be used for the time being) or memory data used by non-critical services will not affect the normal operation of the entire application, and can improve the degree of application survival and reduce the probability of the application being killed or abnormally exiting. 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 is released to a large extent, enhancing the fluency and stability of the electronic device, and improving the memory management efficiency of the electronic device.

[0009] 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 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 alleviate the memory pressure of the system. On the other hand, the electronic device reclaims 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 exiting will be greatly reduced.

[0010] In one possible design, it is detected that the running state of a first application switches from a first running state to a second running state, and part of the memory used by the first application is processed, including: it is detected that the running state of the first application switches from the first running state to the second running state, and the length of time that the first application is in the second running state reaches a first threshold, and part of the memory used by the first application is processed.

[0011] For example, Fig.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 playing state (an example of the second running state), and the duration that the navigation application is in the background playing state (duration t1-t1') reaches a first threshold, and part of the memory used by the navigation application (memory used by the rendering thread and the user interface thread) is processed.

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

[0013] 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 portion of memory, and the memory associated with the second service includes a second portion of memory;

[0014] The first part of memory is: pages that are not used by the first application in a first time period;

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

[0016] For example, Figure 6 , taking the case where the application switches 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 during the time period t1-t2. When it is detected that the application switches from the foreground running state to the background playing state, the mobile phone can reclaim the memory used by non-critical services such as the rendering thread and the user interface thread.

[0017] Take the case where a navigation application switches from the background playback state to the background service state. In the background service state, the first part of the memory includes the memory used by the surface and media. The first part of the memory is not used by the navigation application during the t2-t3 period. When it is detected that the application switches from the background playback state to the background service state, the phone can reclaim the memory used by non-critical services such as the surface and media.

[0018] Take the case where the navigation application switches from the background service state to the background cache state as an example. In the background cache state, the first part of the memory includes the memory used by the 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 in the t3-t4 time period, and the second part of the memory is not used by the navigation application in the t1-t3 time period (the second time period). When it is detected that the application switches from the background service running state to the background cache state, the mobile phone can reclaim the memory used by non-critical businesses such as the service, rendering thread, and user interface thread.

[0019] Take the case where the navigation application switches 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 the service, media, and surface. Among them, the service memory is not used by the navigation application in the t3-t4 time period (the second time period), and the media and surface memories are not used by the navigation application in the t2-t4 time period (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 businesses such as service, media, and surface.

[0020] Take the case where the navigation application switches 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 in the t5-t6 time period (the first time period). When it is detected that the navigation application switches from the shallow freeze state to the deep freeze state, the mobile phone can reclaim the memory used by non-critical services such as object, class, and method.

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

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

[0023] In this way, different compression methods can be used to perform memory processing on memory data that is not used by applications in the short term and memory data that is not used by applications in the long term, thereby improving memory processing performance.

[0024] In one possible design, the second operating state includes a first background operating state;

[0025] Detecting that the running state of the first application is switched from the first running state to the second running state, and processing part of the memory used by the first application, including:

[0026] It is detected that the running state of the first application is switched from the first running state to the first background running state, and a first portion of memory associated with the first service is compressed. The first portion of memory may be memory that is not used by the application in a short period of time (eg, a first time period).

[0027] In this way, for memory data that is not used in the short term, this part of the memory data can be compressed and stored in the compressed space, so as to save some memory space.

[0028] In one possible design, the second operating state includes a first background operating state;

[0029] Detecting that the running state of the first application is switched from the first running state to the second running state, and processing part of the memory used by the first application, including:

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

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

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

[0033] In one possible design, the first running state is the second background running state.

[0034] In one possible design, the method further includes:

[0035] detecting that the running state of the first application is switched from the first background running state to the third background running state;

[0036] compressing a third portion of memory of the first application, where the third portion of memory is: memory that is not used by the first application during the first time period;

[0037] and / or,

[0038] Swap out a fourth portion of memory of the first application to disk space, where the fourth portion of memory is: memory not used by the first application during the second time period; and a length of the fourth time period is longer than a length of the third time period.

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

[0040] Acquire a virtual memory space VMA corresponding to the portion of memory and a linked list corresponding to the VMA; the linked list includes the portion of memory;

[0041] The portion of memory in the linked list is processed.

[0042] In one possible design, the method further includes:

[0043] Display the first interface;

[0044] An operation input by a user on the first interface is received, where the operation is used to enable a memory management function.

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

[0046] In the background playing state, the first application executes the first task in the background. Exemplarily, the background playing state means that the application switches to the background and no longer presents a graphical interface, but the function or task of the application is still running. For example, a music application runs a music playing task in the background (an example of the first task), and a navigation application runs a navigation task in the background.

[0047] In the background service state, the first application provides background services in the background, and the first application does not perform the first task in the background. For example, it refers to a background service application, which mainly implements background data collection, message push or resident interrupt waiting service, such as Bluetooth connection. For example, the application can push messages in the background, and for example, the application can collect some data in order to push some messages to the user.

[0048] In the background cache state, the first application does not execute the first task in the background and does not provide background services, and the duration of the first application being in the background running state reaches a first duration.

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

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

[0051] In a possible design, in the background playing state, the first service includes a service related to interface display. Figure 6 , the interface display related business includes the non-critical business 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 usage of the application.

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

[0053] In the background cache state, the first service includes background services, and the second service includes services related to interface display. Figure 6 In the background cache state, the application no longer executes background services (such as service) and has not switched to the foreground display interface for a long time. The first business includes the background service, and the second business includes the business executed by the rendering thread and the user interface thread. When the application is detected to enter the background cache state, the electronic device can compress the memory related to 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 have not been used by the application for a long time.

[0054] In the shallow freeze state, the second business includes: the business corresponding to the first task and the background service. Figure 6 In the shallow freeze state, the second service includes: media, surface (service corresponding to the first task), service (background service). When the application is detected to enter the shallow 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.

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

[0056] In a second aspect, a memory management device is provided, the device being applied to an electronic device or a component (such as a chip system) supporting a function of an electronic device, the device comprising:

[0057] A processing unit, used for detecting the running state of a first application; upon detecting that the running state of the first application switches from the first running state to the second running state, processing a portion of the memory used by the first application; the portion of the memory is the memory associated with the target business of the first application; the target business is a non-critical business of the first application in the second running state.

[0058] In one possible design, it is detected that the running state of a first application switches from a first running state to a second running state, and part of the memory used by the first application is processed, including: it is detected that the running state of the first application switches from the first running state to the second running state, and the length of time that the first application is in the second running state reaches a first threshold, and part of the memory used by the first application is processed.

[0059] 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 portion of memory, and the memory associated with the second service includes a second portion of memory;

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

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

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

[0063] In one possible design, the second operating state includes a first background operating state;

[0064] Detecting that the running state of the first application is switched from the first running state to the second running state, and processing part of the memory used by the first application, including:

[0065] It is detected that the running state of the first application is switched from the first running state to the first background running state, and a first portion of memory associated with the first service is compressed.

[0066] In one possible design, the second operating state includes a first background operating state;

[0067] Detecting that the running state of the first application is switched from the first running state to the second running state, and processing part of the memory used by the first application, including:

[0068] It is detected that the running state of the first application is switched from the first running state to the first background running state, and the second part of the memory associated with the second service is swapped out to the disk space.

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

[0070] In one possible design, the first running state is the second background running state.

[0071] In a possible design, the processing unit is further used for:

[0072] detecting that the running state of the first application is switched from the first background running state to the third background running state;

[0073] compressing a third portion of memory of the first application, where the third portion of memory is: memory that is not used by the first application during the first time period;

[0074] and / or,

[0075] Swap out a fourth portion of memory of the first application to disk space, where the fourth portion of memory is: memory not used by the first application during the second time period; and a length of the fourth time period is longer than a length of the third time period.

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

[0077] Acquire a virtual memory space VMA corresponding to the portion of memory and a linked list corresponding to the VMA; the linked list includes the portion of memory;

[0078] The portion of memory in the linked list is processed.

[0079] In one possible design, the device further includes:

[0080] A display unit, used to display a first interface;

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

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

[0083] Wherein, in the background playing state, the first application executes a first task in the background;

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

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

[0086] In the shallow freeze state, the first application does not execute the first task in the background and does not provide background services, and the first application is in the background running state for a second time period; the second time period is greater than the first time period;

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

[0088] In a possible design, in the background playback state, the first service includes an interface display-related service;

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

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

[0091] In the shallow frozen state, the second business includes: the business and background services corresponding to the first task;

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

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

[0094] 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 (also referred to as an instruction or code), and when the computer program is executed by an electronic device, the electronic device executes the method of the first aspect or any one of the implementations of the first aspect.

[0095] In a fifth aspect, an embodiment of the present application provides a computer program product. When the computer program product runs on an electronic device, the electronic device executes the method of the first aspect or any one of the implementations of the first aspect.

[0096] In a sixth aspect, an embodiment of the present application provides a circuit system, the circuit system includes a processing circuit, and the processing circuit is configured to execute the method of the first aspect or any one of the implementations of the first aspect.

[0097] In the seventh aspect, an embodiment of the present application provides a chip system, comprising at least one processor and at least one interface circuit, wherein the at least one interface circuit is used to perform transceiver functions and send instructions to the at least one processor, and when the at least one processor executes the instructions, the at least one processor executes the method of the first aspect or any one of the embodiments of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS

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

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

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

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

[0102] Figure 1E A schematic diagram of a memory compression and swapping mechanism provided in an embodiment of the present application;

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

[0104] Figure 1G is a schematic diagram of a memory management mechanism in the related art;

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

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

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

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

[0109] Figure 6 A schematic diagram of a memory management method according to an embodiment of the present invention;

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

[0111] Figure 8A schematic diagram of a memory management method provided in an embodiment of the present application;

[0112] Fig. 9 A schematic diagram of memory management operations in a background cache state provided by an embodiment of the present application;

[0113] Fig.10 A schematic diagram of memory management operations in a shallow freeze state provided by an embodiment of the present application;

[0114] Fig.11 A schematic diagram of a memory management method according to an embodiment of the present invention;

[0115] Fig.12 A schematic diagram of an interface provided in an embodiment of the present application;

[0116] Fig.13 A schematic diagram of the structure of a memory management device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0117] In the description of the embodiments of the present application, unless otherwise specified, “ / ” means or. For example, A / B can mean A or B. The “and / or” in this article is merely a way to describe the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone.

[0118] In the following, the terms "first" and "second" are used for descriptive purposes only and are not to be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of the embodiments of the present application, unless otherwise specified, "plurality" means two or more.

[0119] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.

[0120] First, some terms involved in the embodiments of the present application are introduced:

[0121] 1. Memory paging mechanism

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

[0123] It should be noted that, with the evolution of technology, the memory unit can also be implemented in other ways, not limited to the memory page. It should be understood that in the embodiment of the present application, the memory page is used as an example of the memory unit, but this does not constitute a limitation on the memory unit.

[0124] 2. Anonymous pages and file pages

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

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

[0127] As a possible implementation method, the program can read files from the 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 disk file content can be regarded as a file page.

[0128] Anonymous pages refer to memory pages that have no corresponding files in the external storage space. For example, the pages used by the process's heap and stack can be anonymous pages. Anonymous pages can be used to store temporary calculation results of the process during operation.

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

[0130] In addition to file pages and anonymous pages, memory can also include memory managed autonomously by the kernel or modules within the kernel. This memory can be used to store basic data structures and driver data to maintain normal system operation.

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

[0132] 3. Three memory areas

[0133] In some schemes, such as Figure 1A As shown, the memory of an electronic device can be divided into three parts:

[0134] The first part of memory: Java virtual machine's on-heap memory. The virtual machine uses garbage collector (GC) to manage on-heap memory based on its own memory management mechanism. As a possible implementation method, the virtual machine calls the mmap interface to allocate on-heap memory. The operating system will record the on-heap memory allocated by the virtual machine.

[0135] Optional, such as Figure 1A The heap memory of the virtual machine can be used to store one or more objects, classes, and methods.

[0136] The second part of memory: direct (native) memory. For example, direct memory can be off-heap memory. Compared with heap memory managed by the virtual machine, direct memory is managed by the operating system, which can reduce the impact of garbage collection on the application to a certain extent.

[0137] Optionally, the data in the direct memory mainly includes: data associated with the display function, data associated with the rendering function, and data associated with the system service. Figure 1A , data related to the operation of user interface (UI) threads, rendering (Render) threads, etc. are stored in direct memory. For another example, the runtime metadata of threads are stored in direct memory. For another example, data related to push services are stored in direct memory.

[0138] As a possible implementation, the operating system allocates direct memory through a C++ allocator. Optionally, the C++ allocator includes but is not limited to jemalloc, scudo, etc. Taking the C++ allocator as an example, the C++ allocator can identify the identifier of the thread currently applying for memory (such as the thread name), and determine the function of the thread based on the thread identifier. For example, if the C++ allocator identifies that the thread name of the thread currently applying for 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 interface.

[0139] The third part of memory: streaming media memory. Currently, hardware codecs are basically used in chip architectures to accelerate video and audio rendering. In order to allow data streams to flow between the CPU and the hardware codec, the industry usually uses (ION) or dma_buf allocators to identify the commonality of memory space. Through ION or dma_buf attribute identification, it can be clearly determined that this part of memory is used for playback.

[0140] Optionally, the streaming media memory can be used to store data related to display (surface), multimedia (media), and service (service).

[0141] 4. The operating system manages memory uniformly

[0142] Since memory resources are limited, when memory is insufficient, the operating system can reclaim infrequently used memory based on the frequency of memory use. In some solutions, memory reclaim can be performed based on a memory watermark.

[0143] As a possible implementation method, when receiving a memory allocation request, 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 amount of system memory and meet the memory allocation requirements.

[0144] For example, 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 waterline), the recycling thread can scan the used memory and recycle part of the used memory. Among them, the recycling thread can calculate the amount of memory to be reclaimed according to the parameter Swappiness and the proportion of reclaimed memory, and recycle the memory to be reclaimed according to the amount of memory to be reclaimed. In this memory recycling mechanism, when the recycling thread calculates the amount of memory to be reclaimed, it does not take into account the actual memory requirements of each application, which may lead to excessive memory recycling or insufficient memory recycling. For example, when the electronic device has little free memory, the electronic device excessively recycles a large amount of memory, causing the normal operating application to exit abnormally, affecting the performance of the electronic device.

[0145] 5. Linked list and memory management mechanism based on linked list

[0146] like Figure 1B ,The whole memory of an electronic device can include kernel memory, file pages, anonymous pages, free memory, and other memory. Different applications can use different memory to support the running of the applications. 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 which case the file pages can be called shared pages. Alternatively, 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.

[0147] In some schemes, the kernel can manage file pages and anonymous pages through the least recently used (LRU) linked list. According to the active state of the linked list, the linked list can be divided into two levels: active and inactive linked lists. Figure 1B As 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, and 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.

[0148] Multiple memory pages can be stored in a linked list. For example, multiple active file pages can be stored in an active file page linked list, and multiple inactive file pages can be stored in an inactive file page linked list. Multiple active anonymous pages can be stored in an active anonymous page linked list, and multiple inactive anonymous pages can be stored in an inactive anonymous page linked list. Active memory pages can be memory pages that are frequently used by a process, and inactive memory pages can be memory pages that are not frequently used by a process.

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

[0150] In the embodiment of the present application, using a memory page may also be referred to as accessing a memory page, calling a memory page, etc.

[0151] 5.1 Memory management mechanism based on linked lists when memory pages are used:

[0152] First, we introduce the mechanism of managing memory pages based on linked lists when memory pages are used. Figure 1C Based on the linked list (active linked list or inactive linked list) where the used memory page is located and the previous usage mark of the memory page, the electronic device can perform different operations:

[0153] like Figure 1C (a), if memory page A in the active linked list is used, the electronic device sets PG_referenced of the memory page A to 1, indicating that the memory page A has been used.

[0154] like Figure 1C (b) If the memory page A in the inactive linked list is used, and the previous identification bit PG_referenced of the memory page A is 0 (for example, it has not been used before), the electronic device sets the identification bit PG_referenced of the memory page A to 1, indicating that the memory page A has been used.

[0155] like Figure 1C(c), if memory page A in the inactive list is used, and the previous flag bit PG_referenced of 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 that memory page A is used is high. The electronic device can move memory page A from the inactive list to the active list, and can set the flag bit PG_referenced to 0, indicating that memory page A has not been used after being moved to the active list.

[0156] 5.2 Memory management mechanism based on linked lists when the system memory is insufficient:

[0157] The following describes the mechanism of managing memory pages based on linked lists when free memory is insufficient (for example, free memory is below the waterline). Figure 1D When the memory of the electronic device is insufficient, the memory page can be recycled from the inactive linked list first. As a possible implementation, the electronic device scans the memory page in the inactive linked list and determines whether to recycle the memory page according to the identification bit of the memory page.

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

[0159] exist Figure 1D In the example shown in (b), the flag PG_referenced of memory page A is 0, which means that memory page A has not been used for a certain period of time. Then, in order to increase the free memory of the entire device, the electronic device can recycle memory page A into the memory recycling area. As the memory pages at the end of the inactive linked list are recycled, the memory pages at the front of the inactive linked list move to the end.

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

[0161] As a possible implementation manner, the electronic device reclaims the memory page to the compression space, which can be implemented by: compressing the memory page and storing the compressed memory page in the compression space.

[0162] As a possible implementation manner, the electronic device reclaims the memory page to the disk space, which can be implemented by swapping the memory page to the disk space.

[0163] In some embodiments, the electronic device can recycle some pages stored in the memory through a page compression thread. The page compression thread generally recycles the memory by compression. For example, Figure 1E (a) compresses the anonymous page to obtain a compressed anonymous page, and can store the compressed anonymous page in the compressed space of the memory. The memory usage of the compressed anonymous page is less than the memory usage of the corresponding anonymous page. In some scenarios, the allocated anonymous page in the memory (such as infrequently used anonymous pages, etc.) can also be released. After the anonymous page is released, it no longer occupies memory. In this way, memory recycling is achieved and free memory is increased for application use.

[0164] Later, after detecting that the process needs to use the compressed page request, such as Figure 1E (a) can decompress the page from the compressed space and allocate the decompressed page to the process for use.

[0165] In some embodiments, the electronic device can recycle compressed pages in the memory through a page swap thread. The page swap thread recycles the memory by swapping out the compressed pages in the memory (such as the compressed space) to the disk. For example, Figure 1E (b) swaps out the compressed anonymous pages in the memory (such as the compression space) to the disk for storage, thereby reducing the memory usage and facilitating subsequent memory allocation. For example, the page swap 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. This reduces the memory usage in the DDR SDRAM and facilitates subsequent application programs to apply for memory in the DDR SDRAM.

[0166] Later, after detecting that the process needs to use the swapped out compressed page, such as Figure 1E (b) can swap compressed pages in the disk space (such as compressed anonymous pages) back to the memory (such as compressed space) and decompress them, and then allocate the decompressed pages to the process for use.

[0167] The swapping process can also be understood as storing the data in random access memory (RAM) to read-only memory (ROM). In this way, part of the ROM storage space can be used as RAM, thus expanding the RAM storage space. For devices with insufficient RAM space (such as mobile phones with 2G or 4G memory), it can significantly reduce system freezes.

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

[0169] 1. In Figure 1F In the situation shown in (a), the identification bit PG_referenced of memory page A is 1, then the electronic device sets the identification bit PG_referenced of memory page A to 0.

[0170] 2. In Figure 1F In the case shown in (b), the flag PG_referenced of memory page A is 0, which means that memory page A has not been used for a certain period of time. Then, the electronic device can move memory page A to the inactive list. As the memory page at the end of the active list is moved out of the active list, the memory page at the front of the active list moves to the end.

[0171] In summary, the memory recycling process can be simplified into the following steps: the memory pages in the active linked list can be moved to the end of the active linked list. In some cases, the memory pages that meet the conditions at the end of the active linked list will be migrated to the head of the inactive linked list to achieve the migration of memory pages between linked lists. The memory pages in the inactive linked list can be migrated to the end of the inactive linked list, and the electronic device can reclaim the memory pages that meet the conditions from the end of the inactive linked list.

[0172] 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, and the processes running in the foreground can usually have a visual interface. Since the foreground process is usually related to the user's visible interface, the smoothness of the foreground process usually has a greater impact on the smoothness of the electronic device. Similarly, when an application runs in the foreground, it usually has a visual interface, and the foreground application has a greater impact on the smoothness of the electronic device.

[0173] 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 great impact on the fluency of electronic devices. For example, although the background download task is not in the foreground, it directly affects the response delay of the electronic device and affects 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 not visible, its processing speed affects the user's photo-taking experience and the fluency of the electronic device. Similarly, although some applications running in the background do not have a visual interface, their performance has a great impact on the fluency of electronic devices.

[0174] At present, the memory of applications can be managed uniformly by the operating system. The operating system can give priority to reclaiming the memory pages of target applications based on the frequency of use of memory pages by applications. The target application is an application that uses memory pages less frequently. In this memory management scheme, the activity of some background applications may be relatively high, and accordingly, the frequency of use of memory pages by background applications increases. Then, when memory is recycled, since the frequency of memory page use by background applications is higher than that of memory pages used by foreground applications, the memory pages used by foreground applications are recycled, affecting the performance of foreground applications and further affecting the smoothness of operation of electronic devices. In particular, when the memory specifications of the entire electronic device (such as 3G, 4G) are low, the probability of the memory pages of foreground applications being recycled increases significantly, and the user experience drops sharply. For example, such as Figure 1G , there are applications A to D running on the electronic device. Since the background applications B, C, and D use memory pages more frequently, and the foreground application A uses memory pages less frequently, 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.

[0175] In addition, this memory management solution may also cause the memory of some important background applications to be recycled, which may also affect the smoothness of electronic devices.

[0176] 6. Framework-based memory management mechanism

[0177] In some solutions, when the system memory is insufficient, the system can determine the application to be killed based on the memory usage of the application. Optionally, when the free memory is insufficient, the system will give priority to killing applications with high memory usage. For example, if application A occupies 1G memory, application B occupies 300M memory, and application C occupies 200M memory, the system can give priority to killing application A which occupies the most memory.

[0178] In addition to the global unified memory management mechanism provided by the operating system kernel, the operating system also provides a framework-based or interface-based memory management method. For example, in the Android open source project (AOSP), Mechanism or Purgeable In the mechanism, when an application switches to the background, the memory that the application does not need to use temporarily can be released. In this way, for the target application that has released part of the memory, since it has released part of the 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 the memory based on the Purgeable Memory mechanism, it occupies less memory. Therefore, the probability of the target application being killed by the system is usually reduced, which can improve the survival degree of the target application. That is, by actively releasing part of the memory, the target application can survive longer in the system, thus improving the user experience of using the target application.

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

[0180] Taking the onTrimMemory mechanism as an example, the operating system can detect the running status of an application. When the electronic device is short of memory and detects that there is an application in a specific running status, the operating system sends a communication message to the application in the specific running status. The application 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.

[0181] Among them, the onTrimMemory interface is provided by the system, and the application developer (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 application's memory release method 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.

[0182] For example, the onTrimMemory interface can be implemented in the following format:

[0183] onTrimMemory()

[0184] {

[0185] …

[0186] Memory release method ...

[0188] }

[0189] The running status of the application may be as shown in Table 1 below.

[0190] Table 1

[0191]

[0192] For example, Figure 2 Initially, the application runs in the foreground, and the system service in the operating system detects the running status of the application. Later, the application switches to the background, and the system service monitors that the application switches from the foreground to the TRIM_MEMORY_BACKGROUND (the application has switched to the background). 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 system's memory pressure.

[0193] Although the above solution can release a certain amount of memory, the memory release method in the onTrimMemory interface needs to be implemented by the application developer, that is, the system leaves the memory recovery 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. In addition, because many application developers do not know which memory can be released or for other reasons, most applications do not release memory when they return to the background, or the memory release effect is not good, or the memory release error causes the system to crash.

[0194] For example, in order to ensure the survival of the application and user experience, the application developer does not implement the memory release method of the background application in the onTrimMemory interface. In this way, when the system as a whole is short of memory, the system will directly kill the high-occupancy background application.

[0195] In addition, the running status of each application changes all the time. It is very likely that during the execution of the onTrimMemory method of the application, the running status of the application suddenly deteriorates, resulting in the application being killed due to high memory usage before the memory of the application is released, thus affecting the user experience of using the application.

[0196] From the above, it can be seen that the current memory release method has a poor effect and cannot guarantee the operating performance of the electronic device.

[0197] In order to solve the above technical problems, an embodiment of the present application provides a memory management method, in which an electronic device can detect the life cycle of an application and process the memory data used by the functional modules corresponding to the application according to the stage of the life cycle that the application is in. Considering that the performance requirements of the application at different life cycle stages may be different, the memory data of each functional module of the application may be processed differently when the application is in different life cycle stages.

[0198] For example, when the application is in the background playing state, the memory data used by the functional modules 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 functional modules of the application for interface display is swapped out.

[0199] In this way, it is possible to meet the memory processing requirements of applications at different life cycle stages. In other words, on the basis of meeting the performance requirements of applications at the corresponding life cycle stages, it is possible to process memory from the functional modules of the application as efficiently as possible, so as to increase the free memory of the electronic device and thereby improve the overall operating performance of the electronic device.

[0200] The memory management method of the embodiment of the present application can be applied in electronic devices. For example, it can be applied in electronic devices of AOSP system or similar system. Optionally, the electronic device can allocate memory by mmap, standard allocator, kernel allocator, etc. Exemplarily, the electronic device can be a mobile phone, tablet computer, personal computer (PC), netbook, etc., which need to perform memory optimization. The present application does not impose any special restrictions on the specific form of the electronic device.

[0201] Taking a mobile phone as an example, Figure 3 The hardware structure diagram of the electronic device 100a is shown. The structures of other electronic devices can refer to the structure of the electronic device 100a.

[0202] 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, an earphone 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, an air 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.

[0203] It is to be understood that the structure illustrated in the embodiment 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 shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.

[0204] 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 processor (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. Different processing units may be independent devices or integrated into one or more processors.

[0205] The controller can generate operation control signals according to the instruction operation code and timing signal to complete the control of instruction fetching and execution.

[0206] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.

[0207] In some embodiments, the processor 110 may include one or more interfaces. The interface 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.

[0208] The USB interface 130 is an interface that complies with the USB standard specification, and specifically can be a Mini USB interface, a MicroUSB 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 to transmit data between the electronic device 100a and a peripheral device. It can also be used to connect headphones to play audio through the headphones. The interface can also be used to connect other electronic devices, such as AR devices, etc.

[0209] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present invention is only a schematic illustration and does 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.

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

[0211] 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 input 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, and the wireless communication module 160. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle number, battery health status (leakage, impedance), etc. In some other embodiments, the power management module 141 can also be set in the processor 110. In other embodiments, the power management module 141 and the charging management module 140 can also be set in the same device.

[0212] 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 modem processor and the baseband processor.

[0213] Antenna 1 and 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 reused to improve the utilization of the antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.

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

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

[0216] The wireless communication module 160 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the electronic device 100a. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, modulate the frequency of it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.

[0217] In some embodiments of the present application, the electronic device 100a can 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 achieve communication between the electronic device 100a and other terminals or servers.

[0218] In some embodiments, the antenna 1 of the electronic device 100a is coupled to the mobile communication module 150, and the antenna 2 is coupled to the wireless communication module 160, so that the electronic device 100a can communicate with the network and other devices through wireless communication technology. The wireless communication technology 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 technology. The GNSS may include a global positioning system (GPS), a global navigation satellite system (GLONASS), a Beidou navigation satellite system (BDS), a quasi-zenith satellite system (QZSS) and / or a satellite based augmentation system (SBAS).

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

[0220] 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 or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100a may include 1 or N display screens 194, where N is a positive integer greater than 1.

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

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

[0223] The camera 193 is used to capture still 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 light signal into an electrical signal, and then passes the electrical signal to the ISP to be converted 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 a standard RGB, YUV or other format. In some embodiments, the electronic device 100a may include 1 or N cameras 193, where N is a positive integer greater than 1.

[0224] The digital signal processor is used to process digital signals, and can process not only digital image signals but also other digital signals. For example, when the electronic device 100a is selecting a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy.

[0225] Video codecs are used to compress or decompress digital videos. The electronic device 100a may support one or more video codecs. Thus, the electronic device 100a may play or record videos in a variety of coding formats, such as moving picture experts group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.

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

[0227] 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 a data storage function. For example, files such as music and videos can be stored in the external memory card.

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

[0229] The electronic device 100a can implement audio functions such as music playing and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.

[0230] The audio module 170 is used to convert digital audio information into analog audio signal output, and is also used to convert analog audio input into digital audio signals. The audio module 170 can also be used to encode and decode audio signals. In some embodiments, the audio module 170 can be arranged in the processor 110, or some functional modules of the audio module 170 can be arranged in the processor 110.

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

[0232] The receiver 170B, also called a "earpiece", is used to convert audio electrical signals into sound signals. When the electronic device 100a receives a call or voice message, the voice can be received by placing the receiver 170B close to the human ear.

[0233] Microphone 170C, also called "microphone" or "microphone", is used to convert sound signals into electrical signals. When making a call or sending a voice message, the user can speak by putting their mouth close to microphone 170C to input the sound signal into microphone 170C. The electronic device 100a can be provided with at least one microphone 170C. In other embodiments, the electronic device 100a can be provided with two microphones 170C, which can not only collect sound signals but also realize noise reduction function. In other embodiments, the electronic device 100a can also be provided with three, four or more microphones 170C to collect sound signals, reduce noise, identify the sound source, realize directional recording function, etc.

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

[0235] The key 190 includes a power key, a volume key, etc. The key 190 may be a mechanical key or a touch key. The electronic device 100a may receive key input and generate key signal input related to user settings and function control of the electronic device 100a.

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

[0237] Indicator 192 may be an indicator light, which may be used to indicate charging status, power changes, messages, missed calls, notifications, etc.

[0238] The SIM card interface 195 is used to connect a SIM card. The SIM card can be connected to and separated from the electronic device 100a by inserting it into the SIM card interface 195 or pulling it out from the SIM card interface 195. 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, and the like. 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 implement functions such as calls and data communications. In some embodiments, the electronic device 100a uses an eSIM, i.e., an embedded SIM card. The eSIM card can be embedded in the electronic device 100a and cannot be separated from the electronic device 100a.

[0239] It should be noted that the structure of electronic equipment can also refer to Figure 5 The electronic device may have a structure as shown. Figure 5 The structures shown may have more or fewer components, or combine some components, or separate some components, or arrange the components differently. The components shown may be implemented in hardware, software, or a combination of software and hardware.

[0240] Optionally, the software system of the electronic device 100a may adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. Taking the system as an example, the software structure of the electronic device 100a is exemplified.

[0241] Figure 4 It is a software structure block diagram of the electronic device 100a according to an embodiment of the present invention.

[0242] The layered architecture divides the software into several layers, each with clear roles and division 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 library, and the kernel layer.

[0243] The application layer can include a series of application packages.

[0244] like Figure 4 As shown, the application package may include camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message and other applications.

[0245] An electronic device may run one or more applications, and each application has at least one corresponding process. A process has at least one thread executing a task. In other words, multiple threads are running in the electronic device. Threads include Java threads and C / C++ threads.

[0246] In order to ensure the normal operation of the thread, the electronic device can allocate a processing unit (such as a CPU core) to the thread according to a certain strategy. After the thread is allocated a processing unit, the corresponding task can be executed through the processing unit.

[0247] The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer includes some predefined functions.

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

[0249] The first service is used to detect the running state of the application. When it is detected that the running state of the application is switched, a notification message is sent to the memory management module of 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.

[0250] That is, in the embodiment of the present application, the memory of each functional module can be managed based on the granularity of the functional module or business type 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.

[0251] Optionally, the functional modules of the application include, but are not limited to, one or more of the following: component, thread, method, class, object, surface (which may correspond to a memory area storing information of an image to be displayed on the screen), and media.

[0252] Optionally, the components include, but are not limited to, any one or more of the following: activity, fragment, service, content provider, broadcast receiver.

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

[0254] In some examples, different functional modules may correspond to different business types. For example, the rendering thread and UI thread may correspond to the business type of interface display.

[0255] Optionally, the running state of the application includes, but is not limited to, any one or more of the following: foreground running state, background playing state, background service state, background cache state, shallow freeze state, and deep freeze state.

[0256] The foreground running state refers to the application 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.

[0257] Background playback state: refers to the application switching to the background and no longer presenting the graphical interface, but the application's functions or tasks 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.

[0258] Background service status: refers to background service applications. Background service applications mainly implement background data collection, message push or resident interrupt waiting services, such as Bluetooth connection. For example, the application can push messages in the background, and the application can collect some data in order to push some messages to the user.

[0259] Background cache state: refers to the state where the application is switched to the background and usually no longer runs tasks, and the user has not operated the application for a short period of time (such as the first period of time). 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 history previously running in the foreground can be retained, so that the user can continue to operate the application and browse the corresponding content.

[0260] Shallow freeze state: refers to the application being retreated to the background and no longer running 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 actions when certain system events occur, such as responding to the system executing resolution conversion, etc. The second duration is greater than the first duration.

[0261] Deep freeze state: refers to the application being retreated to the background and no longer running 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 behavior and the application is completely non-running. Among them, the third duration is greater than the second duration.

[0262] 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. The difference between the three states lies in the length of time the application resides in the background. When the application resides in the background and does not run tasks for a short period of time (such as the first period of time), the running state of the application is in the background cache state. When the application resides in the background and does not run tasks for a long period of time (such as the second period of time), the running state of the application is in the shallow freeze state. When the application resides in the background and does not run tasks for a long period of time (such as the third period of time), the running state of the application is in the deep freeze state. If an application in the background cache state is still not operated by the user (does not run tasks) after reaching the second period of time, it can be switched to the shallow freeze state. If an application in the shallow freeze state is still not operated by the user (does not run tasks) after reaching the third period of time, it can be switched to the deep freeze state.

[0263] In an embodiment of the present application, the electronic device can detect the running state of the application, and according to the running state of the application, manage the memory used by the functional modules 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 of 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 operation in a short period of time, the memory management module can compress the memory page of the functional module used for display to obtain a compressed memory page, so that when the application switches back to the foreground, the memory page can be quickly decompressed and used to implement the interface display function.

[0264] 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).

[0265] 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.

[0266] 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.

[0267] 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.

[0268] 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.

[0269] 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.

[0270] 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.

[0271] 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.

[0272] It should be noted that Figure 4The software architecture shown is only 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 embodiment of the present application does not limit the software architecture of the electronic device.

[0273] like Figure 5 Another possible structure of an electronic device is shown. The electronic device may include a processor 401 (optionally, a processor 408), a memory 403, a transceiver 404, etc.

[0274] Among them, each of the above components may include a path for transmitting information between the above components.

[0275] The transceiver 404 is used for communicating with other devices or communication networks via protocols such as Ethernet and WLAN.

[0276] For details on processors and memory, please refer to Figure 2 The description of the relevant structure in the electronic device will not be repeated here.

[0277] The technical solution provided by the embodiment of the present application is described in detail below. Optionally, the physical memory in the electronic device can be divided into multiple memory units. Optionally, the memory unit includes but is not limited to a memory page. The following embodiment is mainly described by taking a memory page as a memory unit as an example, but the technical solution of the embodiment of the present application is not limited thereto.

[0278] In the embodiment 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 memory data that is not used in the short term, this part of the memory data can be compressed and stored in the compressed space to save part of the memory space. For compressed memory data that is not 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 drop mechanism, swapped out to disk space (such as a Flash device).

[0279] Optionally, the life cycle of an application includes one or more of the following running stages: foreground running, background playing, background service, background cache, shallow freeze, and deep freeze. The stage when the application is in the foreground running stage can also be referred to as the application's running state being the foreground running state. Similarly, the stage when the application is in the background playing stage can also be referred to as the application's running state being the background playing state. The stage when the application is in the background service stage can also be referred to as the application's running state being the background service state. The stage when the application is in the background cache stage can also be referred to as the application's running state being the background cache state. The stage when the application is in the shallow freeze stage can also be referred to as the application's running state being the shallow freeze state. The stage when the application is in the deep freeze stage can also be referred to as the application's running state being the deep freeze state.

[0280] As follows, the life cycle of an application and the technical solutions of the embodiments of the present application are introduced in conjunction with the accompanying drawings.

[0281] For example, take the navigation application in a mobile phone as an example. 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.

[0282] At time t1, the mobile phone detects that the user switches the navigation application to the background. Assuming that the navigation application is still running navigation functions 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 playing state. Considering that when the navigation application is running in the background, the graphical interface is no longer displayed (the display function is no longer executed), the memory related to the interface display can be processed.

[0283] Considering that the navigation application may be switched back to the foreground in a short period of time, as a possible implementation method, when the application is in the background playing state (at this time, although the application is running in the background, it may still execute some core functions), the memory related to the interface display can be processed by compressing the memory data related to the interface display of the application, obtaining the compressed memory data related to the interface display and storing it in the compressed space, and not swapping out the compressed memory data related to the interface display to the disk space (such as Flash device). In this way, it can avoid the long time caused by re-reading data from the disk, and the application is delayed in switching back to the foreground.

[0284] For example, Figure 6 During the time period t1-t2, the mobile phone can process memory data related to the interface display, and the memory data related to the interface display includes but is not limited to: memory data related to the user interface thread and memory data related to the rendering thread.

[0285] Afterwards, at time t2, the mobile phone detects that the navigation application is running in the background, and it 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 the navigation application no longer runs navigation tasks / functions when it is in the background service state, the mobile phone can process the memory data related to core functions such as navigation on the basis of the previous memory processing (i.e., processing the memory data related to the interface display). Navigation functions include but are not limited to determining the navigation route and playing navigation audio.

[0286] Optionally, processing memory data related to core functions such as navigation may be implemented as follows: compressing memory data related to core functions such as navigation, and storing the compressed memory data related to core functions such as navigation in a compressed space.

[0287] For example, Figure 6 During the t2-t3 period, in addition to processing memory data related to the interface display (such as memory data related to the rendering thread and memory data related to the user interface thread), 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.

[0288] As the time for the navigation application to run in the background becomes longer, for example, at time t3, the time for the navigation application to run in the background reaches the first time, the navigation application enters the background cache state. Considering that when the application is in the background cache state, the probability of the user switching the navigation application back to the foreground is low, therefore, in order to save more memory space, as a possible implementation method, the mobile phone can swap out the memory data of the navigation application used for interface display to the disk space (such as on the Flash device).

[0289] For example, Figure 6 ,During the period of t3-t4, the mobile phone swaps out the memory data related to the interface display (such as the memory data related to the rendering thread and the user interface thread) to the disk space.

[0290] Optionally, considering that during the t3-t4 period (the navigation application is in the background cache state), the probability of the user operating the navigation application in the short term is low, or the user may no longer need the background service provided by the navigation application in the short term, the mobile phone can compress the memory data used by the service. Figure 6 As shown, during the time period t3-t4, the mobile phone can also compress the relevant memory data used by media and surface.

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

[0292] For example, Figure 6 During the t4-t5 period, the phone swaps out the memory data related to the user interface thread, rendering thread, surface, media, and service to the disk space. In this way, as much memory as possible can be processed from inactive background applications to increase the amount of free memory of the entire device, thereby improving the performance of the entire device.

[0293] The navigation application continues to run in the background. At time t5, the duration of the navigation application running in the background reaches the third duration, and the navigation application enters a deep freeze state. The navigation application is in a deep freeze state, which means that the navigation application has not been used by the user again for a long period of time. Based on this, the mobile phone can predict that the navigation application will not be used by the user again in the future for a long period of time. In order 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 a deep freeze state and store it in a compressed space. In this way, the memory usage of the heap memory data can be reduced, and because the heap memory data is compressed to the compressed space (not swapped out to the Flash device), when the subsequent process wants to use the heap memory data, it only needs to decompress the compressed heap memory data, and there is no need 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 sense of jamming when the user switches the navigation application back to the foreground.

[0294] For example, Figure 6 During the t5-t6 period, the mobile phone can compress the heap memory data of the Java virtual machine (such as method, class, object related memory data) 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, and store the compression results in the compression space.

[0295] In the above solution, the electronic device can detect the life cycle of the application, and process the memory data used by some functional modules corresponding to the application according to the stage of the life cycle of the application.

[0296] First of all, since memory processing is performed based on the granularity of the application's functional modules, the memory processing is more sophisticated. In some scenarios, processing the memory data used by some functional modules of the application (such as functional modules that will not be used for the time being) will not affect the normal operation of the entire application, and can improve the degree of application survival and reduce the probability of the application being killed or abnormally exiting. For example, for some background applications, the electronic device can only process the memory used by some functional modules of the background application. In this way, while ensuring the survival of the background application as much as possible, the memory of the application is released to a large extent, enhancing the fluency and stability of the electronic device (avoiding memory bumps), and improving the memory management efficiency of the electronic device.

[0297] 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.

[0298] 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.

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

[0300] 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.

[0301] 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.

[0302] Optionally, the electronic device may also mark the memory pages in the allocated physical memory. For example, different marks are set for the memory pages used by different functional modules. For example, the mark of the memory page used by the UI thread is set to mark 1, and the mark of 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.

[0303] After the VMA is allocated to the thread, the thread can apply to use the memory page mapped to the VMA. As a possible implementation method, the virtual address A' of the memory page to be used can be carried in the use 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', and then address the physical address A of the memory page, so that the thread can use the memory page in the physical address A.

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

[0305] In some scenarios, if a change in the running state of an application is detected, the electronic device may manage memory data used by one or more functional modules of the application according to the running state of the application.

[0306] Figure 8 FIG. 2 shows an exemplary process of the memory management method of an embodiment of the present application. Figure 8 , the method comprises the following steps:

[0307] S101: A first service detects that a running state of an application has changed.

[0308] Optionally, the first service is located at the framework layer.

[0309] S102: The first service sends a message A to the memory management module.

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

[0311] It should be noted that, in the embodiments of the present application, mentioning that a certain message is used for a certain function means that the message can be used for the function, rather than that the message is dedicated to the function.

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

[0313] S103: The memory management module sends a message B to the processing thread.

[0314] The message B is used to instruct the processing thread to perform memory processing.

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

[0316] As a possible implementation, the processing thread processes the memory pages used by the application according to the functional modules (or business types) associated with the memory pages. Optionally, for a certain operating 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 functional modules (or business types) associated with the memory pages in a specific operating state.

[0317] For example, for the foreground application, all memory pages used by the application can be regarded as critical memory pages. For applications in the background playing state, since the application no longer displays the UI in the foreground, the business related to the interface display is regarded as non-critical business, or in other words, the application function modules related to the interface display are regarded as non-critical function modules. Accordingly, the memory pages related to the interface display (such as the memory pages used by the UI thread, rendering thread, and layer synthesis thread) can be regarded as non-critical memory pages. Accordingly, for applications in the background playing state, electronic devices 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.

[0318] For another example, for applications in the background cache state, memory pages related to the interface display and the 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, non-critical memory pages can include two types, one is a memory page that is not used in a short time, and the other is a memory page that is not used for a long time. For non-critical memory pages that are not used in a short time (such as memory pages used by surface, media, and service in the background cache state), the electronic device can compress the part of non-critical memory pages and store them in the compressed space. For non-critical memory pages that are not used for a long time (such as memory pages related to the interface display in the background cache state), the electronic device can swap out the part of non-critical memory pages and store them in the disk space.

[0319] Compared with the related art that triggers memory processing by setting a waterline, which will cause the amount of memory processing to not match the actual memory usage requirements of the electronic device with a high probability, the memory management method provided in the embodiment of the present application can determine the key business (or key function module) and non-key business (or non-key function module) corresponding to the application in different operating states, and according to the key business and non-key business, the electronic device does not process the memory (key memory) actually needed for the key business of the application, and processes the memory (i.e. non-key memory) that is actually not needed for the key business (or key function module) of the application. In this way, the actual memory usage requirements of the application can be matched and the appropriate amount of memory can be processed.

[0320] The memory processing process of the processing thread is described below using 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). Fig. 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 method, the processing thread can scan the linked list 1 to which the VMA is mapped according to the VMA of the UI thread of the navigation application; scan the linked list 2 to which the VMA is mapped according to the VMA of the rendering thread of the navigation application; scan the linked list 3 to which the VMA is mapped according to the VMA of the surface of the navigation application; scan the linked list 4 to which the VMA is mapped according to the VMA of the media of the navigation application; and scan the linked list 5 to which the VMA is mapped according to the VMA of the service of the navigation application.

[0321] Among them, for the memory pages that meet the processing conditions in linked list 1 (i.e., the memory pages related to the UI thread), since the navigation application has been running in the background for a long time, the probability of the navigation application switching back to the foreground in a short time is very small, so 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 that meet the processing conditions in linked list 2 (i.e., the memory pages related to the rendering thread), the processing thread can swap out these memory pages (such as memory page C) to the disk space.

[0322] For the memory pages that meet the processing conditions in linked list 3 (memory pages related to surface), considering that the probability of users operating navigation applications in the short term is low, the processing thread can compress these memory pages (such as memory page D) into the compressed space. Similarly, the processing thread can compress the processable memory pages in linked list 4 (memory pages related to media) into the compressed space, and compress the processable memory pages in linked list 5 (memory pages related to service) into the compressed space.

[0323] It should be noted that Fig. 9 Taking the example that the VMA of a functional module corresponds to a linked list, in other embodiments, the VMA of a functional module may correspond to multiple linked lists, or the VMAs of multiple functional modules may correspond to one linked list, or the VMAs of multiple functional modules may cross-correspond to multiple linked lists, for example, the VMAs of the UI thread and rendering thread of the 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 embodiment of the present application does not limit the mapping relationship between the VMA of the functional module and the physical memory.

[0324] For example, Fig.10 If it is detected that the running state of the navigation application switches to a shallow 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 the linked list 1 mapped to the VMA of the UI thread of the navigation application according to the VMA of the navigation application, and swap out the memory pages in linked list 1 that meet the processing conditions (that is, the memory pages used by the UI thread) 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).

[0325] The above mainly uses the example that after the application switches the running state, the electronic device can process part of the application's memory according to the running state of the application. In 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 that the running state of the application is stable. For example, Fig.11 , the application enters the background playing state at time t1. In order to prevent the user from switching the application back to the foreground in a short period of time, the electronic device can delay for a period of time. For example, the delay time is t1-t1'. During the period t1-t1, if the user does not switch the application back to the foreground, 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. In this case, the electronic device can process the memory related to the interface display (such as the memory used by the rendering thread and the UI thread) at time t1'.

[0326] In some embodiments, the electronic device may provide a memory management setting portal, and the user may set the memory management related functions through the setting portal. Fig.12, the electronic device displays a setting interface 110 (an example of the first interface), and the interface 110 includes a setting option 1101, which is used to turn on or off the memory management function. When the memory management function is turned on, the electronic device can manage the memory used by one or more functional modules of the application based on the running state of the application and the granularity of the functional module. Alternatively, in other embodiments, the electronic device can always turn on the memory management function. Alternatively, the electronic device can automatically turn on the memory management function when certain conditions are met.

[0327] Table 2 below shows the performance indicators of the electronic device when the memory management function is enabled and when the memory management function is not enabled.

[0328] Table 2

[0329]

[0330] It should be noted that the above only gives a few examples of the application running status. In other embodiments, the application running status or life cycle stages can be divided separately, and the above technical solutions can be used to process part of the application's memory (non-business-critical memory in the corresponding running state) according to the application's running status (or the life cycle stage of the application).

[0331] In some schemes, multiple embodiments of the present application can be combined, and the combined scheme can be implemented. Optionally, some operations in the process of each method embodiment are optionally combined, and / or the order of some operations is optionally changed. In addition, the execution order between the steps of each process is only exemplary and does not constitute a restriction on the execution order between the steps. There can also be other execution orders between the steps. It is not intended to indicate that the execution order is the only order in which these operations can be performed. A person of ordinary skill in the art will think of a variety of ways to reorder the operations described herein. In addition, it should be noted that the process details involved in a certain embodiment of this article are also applicable to other embodiments in a similar manner, or different embodiments can be used in combination.

[0332] In addition, some steps in the method embodiment may be equivalently replaced by other possible steps. Alternatively, some steps in the method embodiment may be optional and may be deleted in certain usage scenarios. Alternatively, other possible steps may be added to the method embodiment.

[0333] Furthermore, the various method embodiments may be implemented separately or in combination.

[0334] It is understandable that the electronic device in the embodiment of the present application includes a hardware structure and / or software module corresponding to each function in order to realize the above functions. In combination with the units and algorithm steps of each example described in the embodiment disclosed in the present application, the embodiment of the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the technical solution of the embodiment of the present application.

[0335] The embodiment of the present application can divide the electronic device into functional units according to the above method example. For example, each functional unit can be divided according 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 software functional units. It should be noted that the division of units in the embodiment of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation.

[0336] Fig.13 A schematic block diagram of a memory management device provided in an embodiment of the present application is shown, and the device may be the first electronic device mentioned above or a component with corresponding functions. The device 1700 may exist in the form of software, or may be a chip that can be used for a device. The device 1700 includes: a processing unit 1702. The processing unit 1702 may be used to support Figure 8 S101, S104, etc. shown, and / or other processes for the scheme described herein.

[0337] Optionally, the apparatus 1700 may further include a communication unit 1703. Optionally, the communication unit 1703 may also be divided into a sending unit (not shown in FIG. Fig.13 ) and the receiving unit (not shown in Fig.13 ). The sending unit is used to support the apparatus 1700 to send information to other electronic devices. The receiving unit is used to support the apparatus 1700 to receive information from other electronic devices.

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

[0339] In one possible embodiment, the processing unit 1702 may be a controller or Figure 5The processor 401 and / or 408 shown may be, for example, 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 may implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of this application. The processor may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and the like.

[0340] In one possible manner, the communication unit 1703 may include Figure 5 The transceiver 404 shown may also include a transceiver circuit, a radio frequency device, and the like.

[0341] In one possible embodiment, the storage unit 1701 may be Figure 5 The memory 403 is shown.

[0342] The embodiment of the present application 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 codes, and the computer program codes include computer instructions. When the one or more processors execute the computer instructions, the electronic device executes the above-mentioned related method steps to implement the method in the above-mentioned embodiment.

[0343] An embodiment of the present application also provides a chip system, including: a processor, the processor is coupled to a memory, the memory is used to store programs or instructions, when the program or instructions are executed by the processor, the chip system implements the method in any of the above method embodiments.

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

[0345] Optionally, the memory in the chip system may be one or more. The memory may be integrated with the processor or may be separately provided with the processor, which is not limited in the present application. Exemplarily, the memory may be a non-transient processor, such as a read-only memory ROM, which may be integrated with the processor on the same chip or may be provided on different chips. The present application does not specifically limit the type of memory and the arrangement of the memory and the processor.

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

[0347] It should be understood that each step in the above method embodiment can be completed by an integrated logic circuit of hardware in a processor or by instructions in the form of software. The method steps disclosed in the embodiments of the present application can be directly embodied as being executed by a hardware processor, or by a combination of hardware and software modules in a processor.

[0348] An embodiment of the present application also provides a computer-readable storage medium, in which computer instructions are stored. When the computer instructions are executed on an electronic device, the electronic device executes the above-mentioned related method steps to implement the method in the above-mentioned embodiment.

[0349] The embodiment of the present application also provides a computer program product. When the computer program product is run on a computer, the computer is enabled to execute the above-mentioned related steps to implement the method in the above-mentioned embodiment.

[0350] In addition, an embodiment of the present application also provides a device, which may specifically be a component or a module, and the device may include a connected processor and a memory; wherein the memory is used to store computer-executable instructions, and when the device is running, the processor may execute the computer-executable instructions stored in the memory so that the device executes the methods in the above-mentioned method embodiments.

[0351] 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 repeated here.

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

[0353] In this embodiment, the electronic device can be divided into functional modules according to the above method example. For example, each functional module can be divided according 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 schematic and is only a logical function division. There may be other division methods in actual implementation.

[0354] Through the description of the above implementation methods, technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, 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 process of the system, device and unit described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0355] In the several embodiments provided in the present application, it should be understood that the disclosed method can be implemented in other ways. For example, the terminal device embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as 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 mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of modules or units, which can be electrical, mechanical or other forms.

[0356] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0357] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0358] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially 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, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: flash memory, mobile hard disk, read-only memory, random access memory, disk or optical disk and other media that can store program instructions.

[0359] The above is only a specific implementation 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 included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on 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 comprises: Detecting the running status of the first application; It is detected that the running state of the first application switches from the first running state to the second running state, and part of the memory used by the first application is processed; the part of the memory is the memory associated with the target business of the first application; the target business is the non-critical business of the first application in the second running state; the target business includes the first business and the second business; the memory associated with the first business includes the first part of the memory, and the memory associated with the second business includes the second part of the memory; wherein, the first part of the memory is processed in a first manner, and the second part of the memory is processed in a second manner; the first manner is different from the second manner, and the first business is different from the second business.

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 part of the memory used by the first application, including: detecting that the running state of the first application switches from the first running state to the second running state, and the length of time that the first application is in the second running state reaches a first threshold, and processing part of the memory used by the first application.

3. The method according to claim 1 or 2, characterized in that: The first part of memory is: pages that are not used by the first application in a first time period; The second part of memory is: compressed pages that are not used by the first application within a second time period.

4. The method according to claim 3, characterized in that 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 to 4, characterized in that The first mode includes compression, and the second operating state includes a first background operating state; Detecting that the running state of the first application is switched from the first running state to the second running state, and processing part of the memory used by the first application, including: It is detected that the running state of the first application is switched from the first running state to the first background running state, and a first portion of memory associated with the first service is compressed.

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

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

8. The method according to any one of claims 1 to 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 comprises: detecting that the running state of the first application is switched from the first background running state to the third background running state; compressing a third portion of memory of the first application, where the third portion of memory is: memory that is not used by the first application within a third time period; and / or, Swap out a fourth portion of memory of the first application to disk space, where the fourth portion of memory is: memory that is not used by the first application in a fourth time period; and the duration of the fourth time period is longer than the duration of the third time period.

10. The method according to any one of claims 1 to 9, characterized in that Processing a portion of the memory used by the first application includes: Acquire a virtual memory space VMA corresponding to the portion of memory and a linked list corresponding to the VMA; the linked list includes the portion of memory; The portion of memory in the linked list is processed.

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

12. The method according to claim 5 or 6, characterized in that: The first background operation state includes the following states: background play state, background service state, background cache state, shallow freeze state, and deep freeze state; Wherein, in the background playing state, the first application executes a first task in the background; In the background service state, the first application provides background services 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 and does not provide background services, and the duration of the first application being 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 and does not provide background services, and the first application is in the background running state for a second time period; the second time period is greater than the first time period; In the deep freeze state, the first application does not execute the first task in the background and does not provide background services, and the time duration for which the first application is in the background running state reaches a third time duration; the third time duration is greater than the second time duration.

13. The method according to claim 12, characterized in that In the background playback state, the first service includes a service related to interface display; In the background service state, the first service includes the service 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 frozen state, the second business includes: the business and background services corresponding to the first task; In the deep freeze state, the first service includes services corresponding to objects, classes, and methods.

14. An electronic device, characterized in that: include: A processor and a memory, wherein 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 as described in any one of claims 1-13.

15. A computer-readable storage medium, characterized in that: The method comprises computer instructions, which, when executed on an electronic device, cause the electronic device to execute the method as claimed in any one of claims 1 to 13.

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

Citation Information

Patent Citations

  • Virtual memory management method and electronic equipment

    CN113722087A

  • Application switching method and device

    WO2019028912A1

Cited By

  • Memory management method and electronic device

    EP4513334A1

  • Memory management method and electronic device

    WO2024027544A1