A memory management method, an electronic device, and a computer-readable storage medium

By centrally storing the accessed objects of the backend application in an electronic device and releasing unvisible pages, the foreground lag caused by the memory occupancy of background applications is solved, and more efficient memory management and user experience improvement is achieved.

CN119576583BActive Publication Date: 2025-07-11HONOR DEVICE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510118476.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-24
Publication Date
2025-07-11
Estimated Expiration
2045-01-24

AI Technical Summary

Technical Problem

In electronic devices, because the applications running in the background occupy a large amount of memory, the front-end application has lag, the user experience is poor, and the memory resources are seriously wasted.

Method used

After the application switches from the foreground to the background to run, it determines the accessed object and stores it centrally on the target page, and then copies the unvisible object to the storage area other than memory, frees up the page that originally stored the accessed object, and recycles the memory space occupied by the unvisible object.

Benefits of technology

It improves the memory usage efficiency of front-end applications, reduces the waste of memory resources, improves the user experience, reduces the number of page missing interrupts, and improves the device operation speed.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119576583B_ABST
    Figure CN119576583B_ABST
Patent Text Reader

Abstract

The present application provides a memory management method, an electronic device, and a computer-readable storage medium, relating to the field of computer technologies. The electronic device can, in response to an application switching from the foreground to the background, determine a first page from among a plurality of pages allocated for the application in the memory of the electronic device; centrally store objects accessed by the application that are dispersedly stored in different first pages in one or more target pages, and copy all the objects stored in the plurality of first pages to a storage area other than the memory, and release the plurality of first pages, so that the memory space occupied by the objects not accessed by the application stored in these first pages can be recycled. In this way, the electronic device can obtain more available memory, and to a certain extent, enable the application running in the foreground to have sufficient memory for use, enabling it to run more smoothly and improving the user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present application relate to the field of computer technologies, and in particular, to a memory management method, an electronic device, and a computer-readable storage medium. Background Art

[0002] With the development of technology, there are more and more types of applications. In order to improve the user experience, an electronic device can support applications to run in the background. For example, a chat application running in the background can receive messages and notify the user in a timely manner, improving communication efficiency. Another example is that a music application can play music in the background, allowing the user to enjoy music while performing other tasks.

[0003] The internal memory can be abbreviated as memory, which is a storage device directly addressable by the central processing unit (CPU) in an electronic device. In the prior art, during the running of an application in the foreground or background, whether it is a third-party application or a system application, the running data of the application needs to be stored in the storage area allocated for the application in the memory of the electronic device, so that the electronic device can access the running data of the application relatively quickly, and thus run the application at a relatively smooth speed. However, due to hardware limitations, the capacity of the memory in an electronic device is limited.

[0004] As the user uses the electronic device for a longer time, the number of applications running in the background may increase, and the electronic device generally runs these applications in the background for a relatively long time until the applications running in the background are closed, such as when the user manually operates to close the background applications or the operating system closes the background applications when cleaning data. It can be understood that the more applications running in the background, the greater the memory occupancy, resulting in a smaller available capacity of the remaining memory. In this way, when there are many applications running in the background in the electronic device, the application running in the foreground may not be able to run normally due to insufficient memory, resulting in a lag phenomenon and a poor user experience. Summary of the Invention

[0005] The present application provides a memory management method, an electronic device, and a computer-readable storage medium, which can, to a certain extent, enable the application running in the foreground to have sufficient memory for use, run more smoothly, and improve the user experience.

[0006] To achieve the above object, the present application adopts the following technical solutions:

[0007] In a first aspect, the present application provides a memory management method applied to an electronic device. The method includes: in response to an application switching from the foreground to the background, determining a first page from among a plurality of pages allocated for the application in the memory of the electronic device; wherein the pages allocated for the application are used to store objects corresponding to the implementation of the functions of the application; in the case where the number of the first pages is multiple, copying the first objects stored in the multiple first pages to a target page in the memory, and copying all the objects stored in the multiple first pages to a storage area other than the memory, and releasing the multiple first pages; wherein the first page is a page among the multiple pages that meets the conditions, and the conditions include: the page includes a first object and a second object; the first object is an object accessed by the application within a first time period after the application switches from the foreground to the background, and the second object is an object not accessed by the application within the first time period.

[0008] In the above solution, by centrally storing the objects accessed by the application, which are scattered in different pages, in one or more pages and releasing the pages that originally stored the accessed objects, the memory space occupied by the objects not accessed by the application stored in these pages can be recycled, and the storage space that can be reused in the memory becomes larger, that is, the remaining available capacity of the memory becomes larger. In this way, the electronic device can obtain more available memory, which can, to a certain extent, enable the applications running in the foreground to have sufficient memory for use and run more smoothly, improving the user experience.

[0009] In a possible implementation manner of the first aspect, the conditions in the above solution may further include: the ratio of the size of the first object included in the page to the size of the page is less than a threshold. That is, if the ratio of the size of the first object included in the page to the size of the page is less than the threshold, then the page is determined as the first page. If the ratio of the size of the first object included in the page to the size of the page is greater than or equal to the threshold, then no processing is performed on the page.

[0010] In a possible implementation manner of the first aspect, before obtaining the first page from among the multiple pages allocated for the application in the memory of the electronic device, the method further includes: determining the first object. That is, determining the object accessed by the application within a first time period after the application switches from the foreground to the background.

[0011] In a possible implementation of the first aspect, determining the first object includes: marking the read / write permissions of multiple pages as non-readable / writable; within a first time period, detecting whether there is an access violation event; in the case of detecting an access violation event, determining the object corresponding to the physical address carried in the access violation event as the first object. Wherein, an access violation event is an event generated by accessing an object in a page when the read / write permission of the page is non-readable / writable; and, the generated event will carry the physical address of the accessed object, so that in the case of detecting an access violation event, the object corresponding to the physical address carried in the access violation event can be determined as the first object.

[0012] In a possible implementation of the first aspect, within the first time period, detecting whether there is an access violation event includes: within the first time period, periodically detecting whether there is an access violation event; the method further includes: in each detection, in the case of detecting an access violation event, marking the read / write permission of the page where the object corresponding to the physical address carried in the access event is located as readable / writable, and before starting the next detection, re-marking the read / write permission of the page where the object corresponding to the physical address carried in the access event is located as non-readable / writable.

[0013] In the above solution, marking the read / write permission of the page where the object corresponding to the physical address carried in the access event is located as readable / writable, so that the application can continue to run normally. By periodically detecting whether there is an access violation event, it is possible to collect as many objects accessed by the application as possible within the first time period, improving the acquisition rate of the objects accessed by the application.

[0014] In a possible implementation of the first aspect, the method further includes: after the first time period, marking the read / write permissions of multiple pages as readable / writable. In this way, the application can continue to run normally.

[0015] In a possible implementation of the first aspect, the method further includes: storing the physical address of the first object in a preset queue; copying the first objects stored in multiple first pages to a target page in the memory, including: according to the physical addresses of the first objects in multiple first pages stored in the preset queue, copying the first objects stored in multiple first pages to the target page in the memory.

[0016] In the above solution, storing the physical address of the first object in a preset queue, so that the electronic device can obtain the first object according to the physical addresses of the first objects in multiple first pages stored in the preset queue, and implement copying the first objects stored in multiple first pages to the target page in the memory.

[0017] In a possible implementation of the first aspect, the first time period is a time period within a preset duration after the application switches from the foreground to the background, and the preset duration is set in advance; or, the first time period is a time period from when the application switches from the foreground to the background to a target time, and the target time is determined based on the growth rate of the first object determined in two adjacent detections; or, the first time period is determined based on the target time and the preset duration.

[0018] In a possible implementation of the first aspect, the target time is determined based on the growth rate of the first object determined in two adjacent detections, including: the electronic device can compare the growth rate of the object accessed by the application obtained each time with a preset threshold 1. When the growth rate of the object accessed by the application recognized by the electronic device is less than or equal to the preset threshold 1, it can be considered that most of the objects accessed by the application in the background have been collected. The electronic device can terminate the confirmation of the accessed object to reduce the power consumption of the electronic device. That is to say, the target time is the time when the growth rate of the accessed object recognized by the electronic device is less than or equal to the preset threshold 1. Correspondingly, the first time period is the time period between the time when the application switches from the foreground to the background and the time when the growth rate of the accessed object recognized by the electronic device is less than or equal to the preset threshold 1.

[0019] In a possible implementation of the first aspect, the first time period is determined based on the target time and the preset duration, including:

[0020] If the target time is within the time period after the preset duration starts from the time when the application switches from the foreground to the background, the first time period is the time period from when the application switches from the foreground to the background to the target time; if the target time exceeds the time period after the preset duration starts from the time when the application switches from the foreground to the background, the first time period is the time period within the preset duration after the application switches from the foreground to the background.

[0021] In a possible implementation of the first aspect, before copying the first objects stored in multiple first pages to the target page in the memory, the method further includes: allocating a target page for the application based on the sizes of all the first objects stored in the multiple first pages. The target page may include one or more pages, and the size of the target page satisfies the condition: the target page may include one or more pages, and the size of the target page satisfies the condition: the difference between the size of the target page and the sizes of all the first objects is less than the size of one page.

[0022] In a possible implementation of the first aspect, the calculation formula for the growth rate of the object accessed by the application recognized is:

[0023] (The number of accessed objects recognized in the (i + 1)-th application - the number of accessed objects recognized in the i-th application) / the time interval between the moment when the detection of the existence of accessed objects starts in the (i + 1)-th application and the moment when the detection of the existence of accessed objects starts in the i-th application, where the (i + 1)-th detection and the i-th detection are two adjacent detections.

[0024] In a possible implementation manner of the first aspect, copying all the objects stored in the multiple first pages to a storage area other than the memory includes: obtaining the target physical address of the target page; obtaining a first object based on the physical address of the first object in the preset queue; and storing the first object to the target page based on the target physical address.

[0025] In a possible implementation manner of the first aspect, the storage area other than the memory includes any one of the hard disk of the electronic device, the storage device connected to the interface of the electronic device, and the remote storage device.

[0026] In a possible implementation manner of the first aspect, the period can be once every 4 seconds, once every 5 seconds, or once every 6 seconds.

[0027] In a possible implementation manner of the first aspect, the preset time period can be any one of 4 minutes, 5 minutes, and the time period from 4 minutes to 5 minutes.

[0028] In a second aspect, the present application provides an electronic device, which at least includes: a memory and one or more processors. The memory is used to store computer instructions, and when the one or more processors execute the computer instructions, the electronic device executes the method according to any one of the above first aspects.

[0029] In a third aspect, the present application provides a computer-readable storage medium, which includes computer instructions, and when the computer instructions run on an electronic device, the electronic device executes the method according to any one of the above first aspects.

[0030] In a fourth aspect, the present application provides a computer program product, and when the computer program product runs on an electronic device, the electronic device executes the method according to any one of the above first aspects.

[0031] Among them, the technical effects brought by any one of the design manners in the second aspect to the fourth aspect can refer to the technical effects brought by different design manners in the first aspect, which will not be elaborated here. Description of the Drawings

[0032] Figure 1 Shows a schematic diagram of the memory space allocated by an electronic device for an application;

[0033] Figure 2 Shows a schematic diagram of an electronic device executing the memory management method of this application for the memory space allocated to Application 1;

[0034] Figure 3 Shows a schematic diagram of the interface change when an electronic device switches an application from the foreground to the background in response to a user's operation;

[0035] Figure 4 Shows a schematic diagram of the structure of mobile phone 100;

[0036] Figure 5 Shows a schematic diagram of the process of a memory management method;

[0037] Figure 6 Shows a schematic diagram of three types of pages;

[0038] Figure 7 Shows a schematic diagram of the page change before and after the execution of a memory management method;

[0039] Figure 8 Shows another schematic diagram of the page change before and after the execution of a memory management method;

[0040] Figure 9 Shows a schematic diagram of the virtualization architecture of mobile phone 100;

[0041] Figure 10 Shows a schematic diagram of the process of a method for confirming a first object. Detailed implementation manners

[0042] Next, in combination with the accompanying drawings in the embodiments of this application, the technical solutions in the embodiments of this application will be described. Among them, in the description of the embodiments of this application, the terms used in the following embodiments are only for the purpose of describing specific embodiments, and are not intended to limit this application. As used in the specification and appended claims of this application, the singular forms "a", "the", "above-mentioned", "this" and "this one" are also intended to include expressions such as "one or more", unless there is a clear opposite indication in the context. It should also be understood that in the following embodiments of this application, "at least one", "one or more" means one or more than two (including two). The term "and / or" is used to describe the association relationship of associated objects, indicating that three relationships can exist; for example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone, where A and B can be singular or plural. The character " / " generally represents an "or" relationship between the associated objects before and after.

[0043] References to "one embodiment" or "some embodiments" or the like described in this specification mean that a particular feature, structure, or characteristic described in connection with the embodiment is included in one or more embodiments of the present application. Thus, statements such as "in one embodiment", "in some embodiments", "in other some embodiments", "in still other embodiments", etc. that appear in different places in this specification do not necessarily all refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized. The terms "comprising", "including", "having" and their variants all mean "including but not limited to", unless otherwise specifically emphasized. The term "connection" includes direct connection and indirect connection, unless otherwise stated. "First", "second" are only for descriptive purposes and should not be construed as indicating or implying relative importance or implicitly indicating the quantity of the indicated technical features.

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

[0045] For the sake of clear and concise description of the following embodiments, the relevant technical terms or technologies involved in the present application are briefly introduced first:

[0046] (1) Page, also known as a page, in the operating system of an electronic device, the memory is divided into storage areas (or called small blocks) of equal size, and the physical addresses of each storage area are continuous. Each of these storage areas is a page. That is, each page is a fixed-size continuous storage area in the memory. The size of the page is generally fixed, such as 4KB or larger (such as 8KB, 16KB, 32KB, etc.). "K" represents kilo, and "B" represents byte. The Chinese meaning of 4KB is "4 kilobytes", and 4KB = 4 × 1024 bytes = 4096 bytes. The size of the page depends on the operating system and hardware architecture of the electronic device.

[0047] A page is the basic unit for an operating system to manage memory. That is, when an electronic device manages memory, it usually does so in units of pages. For example, taking the allocation of memory space for an application as an example, the application can send a memory allocation request to the operating system to request the operating system to allocate memory space for the application. After receiving the memory allocation request sent by the application, the operating system will check whether the remaining available capacity in the memory meets the size of the memory space requested by the application. If the remaining available capacity in the memory meets the size of the memory space requested by the application, the operating system will allocate one or more pages to the application. After the application obtains the allocated pages, it can store data in these pages in the memory. For example, Figure 1 shows a schematic diagram of the memory space allocated by an electronic device for an application. As Figure 1 shown, the electronic device has allocated n pages for Application 1, m pages for Application 2, l pages for Application 3, and o pages for Application 4. Among them, n, m, l, and o are integers greater than or equal to 1.

[0048] (2) Object. The object in the embodiments of this application is the data used to support an application to implement functions. One object is used to support an application to implement one function, or multiple objects are used to support an application to implement one function. An application can implement the functions of the application by accessing the object. For example, a chat application may include: a "message" object, and this "message" object contains the data required for the chat application to send and receive messages. The electronic device can implement the functions of sending and receiving messages of the chat application based on this "message" object.

[0049] The objects of an application are generally stored in the pages allocated for the application by the operating system in the memory. For example, continuing with the above example, as Figure 1 shown, 4 objects of Application 1 are stored in Page 3 allocated by the operating system for Application 1. These 4 objects can be used to support Application 1 to implement one function. Or these 4 objects are used to support Application 1 to implement multiple functions. For example, 1 of these objects is used to support Application 1 to implement Function 1, and the remaining 3 objects are used to support Application 1 to implement Function 2.

[0050] It can be understood that an application can implement the functions of the application (such as the functions of sending and receiving messages in a chat application) by accessing the objects stored in the page. An application's access to the objects stored in the page includes the application's read operation and / or write operation on the data of the object.

[0051] (3) Release the page. After the release, no data is stored in the page, and it is in a state where it can be reallocated. In some embodiments, when an application no longer uses a certain page allocated by the electronic device for the application, the application can send a page release request for the page to the operating system. After receiving the page release request sent by the application, the operating system deletes all the data stored in the page and marks the page as available. In this way, the page is in a state where it can be reallocated and can be used by other applications or processes.

[0052] As described above, the electronic device manages memory in units of pages. Then, when releasing a page, the electronic device, such as the operating system, does not release a partial storage area in the page alone, but releases the entire page. Specifically, the electronic device can delete all the data stored in the page and mark the page as available. For example, if a page stores multiple objects, the operating system does not release the storage area occupied by a certain one or several of the multiple objects alone, but deletes all the objects stored in the page and marks the page as available.

[0053] The following details the memory management method provided by the embodiments of the present application in combination with specific scenarios and drawings.

[0054] As described in the background art above, when there are many background-running applications in the electronic device, due to the large amount of memory occupied by the background applications, the applications running in the foreground may not be able to run properly due to insufficient memory and may experience lag, resulting in a poor user experience.

[0055] Specifically, one situation where background applications occupy a large amount of memory is as follows: In the related art, when the electronic device releases memory, it does not release a certain object in the page alone, but releases the entire page. As long as there is one object in the objects stored in the page that is accessed, the page will not be released. In this way, when the objects accessed by an application, such as a background-running application, are stored in multiple different pages within a period of time, none of these multiple pages will be released. This may not only affect the operation of the foreground application due to the large amount of memory occupied by the background application, but also cause waste of memory resources. For example, if the size of a page is 4096 bytes, and the size of the object accessed by the application in the page is 50 bytes, and the size of the object not accessed by the application is 4046 bytes, this will result in the 4046-byte storage area occupied by the object not accessed by the application not being able to be released, causing waste of memory resources. If the sizes of two objects accessed by the application are both 50 bytes and are stored in two pages respectively, and the sizes of the objects not accessed by the application in these two pages are 2 * 4046 bytes, this will result in the 2 * 4046-byte storage area occupied by the objects not accessed by the application not being able to be released, which may affect the operation of the foreground application and also cause waste of memory resources.

[0056] To solve this technical problem, an embodiment of the present application discloses a memory management method. The method includes: within a period of time after an application switches to running in the background, the electronic device can determine the objects accessed by the application. If the objects accessed by the application (hereinafter simply referred to as the accessed objects) within the aforesaid period are stored in different pages respectively, and there are also other objects stored in these pages, and these other objects have not been accessed by the application within the aforesaid period (hereinafter simply referred to as the unaccessed objects), then the electronic device can first centrally store the accessed objects stored in these pages into a target page, then copy all the data stored in these pages to a storage area other than the memory, and then release these pages.

[0057] Before executing the memory management method provided by the embodiment of the present application, there are some pages that store both accessed objects and unaccessed objects. Then, the memory space occupied by the unaccessed objects in these pages cannot be recycled (or released). By executing the memory management method provided by the embodiment of the present application, by integrating the above-mentioned accessed objects into one or more target pages and releasing the pages that originally stored the accessed objects, the memory space occupied by the unaccessed objects in these pages can be recycled, and the remaining available capacity of the memory becomes larger. In this way, the electronic device can obtain more available memory, which can, to a certain extent, enable the applications running in the foreground to have sufficient memory for use, making them run more smoothly and improving the user experience.

[0058] Exemplarily, Figure 1 take Application 1 in Figure 2 as an example to illustrate the memory management method provided by the embodiment of the present application. Figure 2 shows a schematic diagram of an electronic device executing the memory management method of the present application for the memory space allocated to Application 1. As

[0059] shown in Figure 2As shown in (a) of the figure, within a period of time after Application 1 is switched to run in the background, the electronic device can determine that the objects accessed by the application are: Object 0, Object 1, Object 2, and Object 3, and Object 0, Object 1, Object 2, and Object 3 are respectively stored in different pages, and unaccessed objects are also stored in these pages. For example, Object 1 is stored in Page 1, Object 2 and Object 3 are stored in Page 2, and Object 0 is stored in Page 3. There are 4 objects stored in Page 1. During the aforementioned period, Object 1 is accessed by the application, and the other three objects are not accessed by the application. There are 4 objects stored in Page 2. During the aforementioned period, Object 2 and Object 3 are accessed by the application, and the other two objects are not accessed by the application. There are 4 objects stored in Page 3. During the aforementioned period, Object 0 is accessed by the application, and the other three objects are not accessed by the application.

[0060] Then, as Figure 2 shown in (b) of the figure, the electronic device can concentrate Object 0, Object 1, Object 2, and Object 3 in a target page for storage, and copy all the data stored in Page 1, Page 2, and Page 2 to a storage area other than the memory. Then, release Page 1, Page 2, and Page 3. Among them, as an example, the target page is a different storage area from Page 1, Page 2, and Page 3.

[0061] Before executing the memory management method of the embodiment of the present application, both accessed objects and unaccessed objects are stored in Page 1, Page 2, and Page 3. Then, the memory space occupied by the unaccessed objects in Page 1, Page 2, and Page 3 cannot be recycled. By executing the memory management method of the embodiment of the present application, by integrating Object 0, Object 1, Object 2, and Object 3 into a target page and releasing Page 1, Page 2, and Page 3 that originally stored the accessed objects, the memory space occupied by the unaccessed objects in Page 1, Page 2, and Page 3 can be recycled, and the remaining available capacity of the memory becomes larger. In this way, the electronic device can obtain more available memory, ensuring that the application running in the foreground has sufficient memory to use, enabling it to run more smoothly and improving the user experience.

[0062] The electronic device can switch the application to run in the background in response to the user's operation of switching the application from the foreground to the background. The user's operation of switching the application from the foreground to the background can be a touch operation, a voice operation, or an air gesture operation, but is not limited thereto. Here, taking the operation of triggering the application to switch from the foreground to the background as a touch operation as an example, the process of the interface change when the electronic device switches the application from the foreground to the background in response to the user's operation is introduced.

[0063] For example, Figure 3The figure shows a schematic diagram of the interface change of an electronic device when an application is switched from the foreground to the background in response to a user's operation of switching the application from the foreground to the background. Take the electronic device as a mobile phone as an example. Figure 3 As shown in (a) of FIG. 1 , the mobile phone 100 displays an interface 101, which includes icons of multiple applications, such as an icon 102 of a chat application and an icon of a reading application. In response to the user clicking the icon 102 of the chat application, the mobile phone 100 starts the chat application and performs the following operations: Figure 3 As shown in (b) of FIG. 1 , the mobile phone 100 can display the interface 103 of the chat application. In this way, the user can use various functions provided by the chat application. Later, when the user wants to switch the chat application to the background, the user can slide upward from the bottom on the display screen of the mobile phone 100. In response to the sliding operation, the mobile phone 100 switches the chat application to the background. In addition, the mobile phone 100 can also display the following Figure 3 The solution provided in the embodiment of the present application can be executed after the mobile phone 100 receives the above-mentioned operation of triggering the application to switch from the foreground to the background, or it can be executed after the mobile phone 100 detects that the application switches from the foreground to the background, and the embodiment of the present application does not make any specific restrictions here.

[0064] In the embodiment of the present application, the electronic device is an electronic device that supports the application to run in the foreground and the background. Specifically, the electronic device can be a portable electronic device or other suitable electronic device. For example, the electronic device can be a mobile phone, a tablet personal computer, a laptop computer, a personal digital assistant (PDA), a personal computer, a notebook computer, a vehicle-mounted device, etc.

[0065] The following uses a mobile phone as an example of an electronic device to illustrate the memory management method provided in an embodiment of the present application.

[0066] Figure 4 FIG. 1 shows a schematic diagram of the structure of the mobile phone 100. Figure 4As shown in the figure, the mobile phone 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor, a gyroscope sensor, a fingerprint sensor, a temperature sensor, etc.

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

[0068] The internal memory 121 may be used to store computer-executable program code, and the executable program code includes instructions. The processor 110 executes various functions of the mobile phone 100 by running the instructions stored in the internal memory 121. In the embodiments of the present application, the internal memory 121, also known as the internal storage, is the memory, and a page may be a storage area divided in the internal memory 121. The mobile phone 100 can implement the functions of an application (such as the function of sending and receiving messages in a chat application) by accessing the objects stored in the page.

[0069] The internal memory 121 has the advantage of fast access speed. The internal memory 121 usually adopts dynamic random access memory (DRAM). In addition to DRAM, the internal memory 121 may also be other random access memories, such as static random access memory (SRAM), etc.

[0070] The external memory interface 120 may be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the mobile phone 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement the data storage function. For example, music, video and other files are saved in the external memory card.

[0071] The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), etc. Among them, different processing units may be independent devices or integrated in one or more processors. Among them, the controller may be the nerve center and command center of the mobile phone 100. The controller may generate operation control signals according to the instruction operation code and timing signal to complete the control of fetching and executing instructions.

[0072] In the embodiments of the present application, within a period of time after the application switches to the background operation, the processor 110 may determine the objects accessed by the application. If the objects accessed by the application (hereinafter simply referred to as the accessed objects) are stored in different pages respectively during the foregoing period, and other objects are also stored in these pages, and these other objects have not been accessed by the application during the foregoing period (hereinafter simply referred to as the unaccessed objects), the processor 110 may first centrally store the accessed objects stored in these pages to the target page, then copy all the data stored in these pages to the storage area other than the memory, and then release these pages. In some embodiments, the storage area other than the memory may be the external memory connected to the external memory interface 120 or the internal disk integrated in the mobile phone 100.

[0073] It can be understood that the interface connection relationship between the modules illustrated in the embodiments of the present invention is only for illustrative purposes and does not constitute a limitation on the structure of the mobile phone 100. In other embodiments of the present application, the mobile phone 100 may also adopt different interface connection methods in the foregoing embodiments or a combination of multiple interface connection methods.

[0074] The charging management module 140 is used to receive charging input from the charger. 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 the inputs from the battery 142 and / or the charging management module 140 and supplies power to the processor 110, the internal memory 121, the external memory, the display screen 194, the camera 193, the wireless communication module 160, etc.

[0075] The wireless communication function of the mobile phone 100 can be implemented through Antenna 1, Antenna 2, the mobile communication module 150, the wireless communication module 160, the modulation and demodulation processor, and the baseband processor, etc. Antenna 1 and Antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in the mobile phone 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be multiplexed to improve the utilization rate of the antennas. For example, Antenna 1 can be multiplexed 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. In some embodiments, Antenna 1 of the mobile phone 100 is coupled to the mobile communication module 150, and Antenna 2 is coupled to the wireless communication module 160, so that the mobile phone 100 can communicate with the network and other devices through wireless communication technologies.

[0076] The display function of the mobile phone 100 is implemented through the GPU, the display screen 194, and the application processor, etc. The GPU is a microprocessor for image processing, and is connected to 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, which execute program instructions to generate or change display information.

[0077] The display screen 194 is used to display images, videos, etc. In some embodiments, the mobile phone 100 may include 1 or N display screens 194, where N is a positive integer greater than 1. In the embodiments of the present application, an application running in the foreground means that the interface of the application is displayed on the display screen 194 of the mobile phone 100, and the user can directly interact with the interface displayed on the display screen 194. An application running in the background means that the interface of the application is not displayed on the display screen 194, and the mobile phone 100 can execute some functions of the application without displaying the interface of the application.

[0078] The shooting function of the mobile phone 100 can be implemented through the ISP, the camera 193, the video codec, and the display screen 194, etc.

[0079] The audio function of the mobile phone 100 can be implemented through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor, etc. Such as music playback, recording, etc.

[0080] The keys 190 include a power-on key, volume keys, etc. The keys 190 can be mechanical keys or touch keys. The mobile phone 100 can receive key inputs to generate key signal inputs related to the user settings and function control of the mobile phone 100. The motor 191 can generate vibration prompts. The indicator 192 can be an indicator light, which can be used to indicate the charging state, the change in battery power, and can also be used to indicate messages, missed calls, notifications, etc.

[0081] Figure 5 A flowchart of a memory management method is shown. The execution subject of this process can be an electronic device, such as the mobile phone 100 mentioned above. As Figure 5 shown, this process includes the following steps:

[0082] 501. In response to the application switching from the foreground to the background, determine the first object; where the first object is the object accessed by the application within the first time period.

[0083] Among them, after the application is started, one or more objects will be created to support the application to implement the corresponding functions. The objects created by the application can be stored in at least one page allocated by the mobile phone 100 for the application in the memory.

[0084] It can be understood that when the application is switched from the foreground to the background, the application can still access the objects stored in the page to implement some functions of the application.

[0085] In the embodiments of the present application, in response to the application switching from the foreground to the background, it can be replaced with in response to the mobile phone 100 receiving an operation that triggers the application to switch from the foreground to the background, or it can also be replaced with: in response to the mobile phone 100 detecting that the application switches from the foreground to the background.

[0086] A property that the application has when running in the background is that after the application runs in the background, as time goes by, the number of objects accessed by the application grows slowly, or even no longer grows, that is, no new accessed objects will appear after the application switches to the background for a period of time. This period of time can be called the first time period. Based on this property, the mobile phone 100 can, in response to the application switching from the foreground to the background, count the objects accessed by the application within the first time period, and determine the objects accessed by the application within the first time period as the first object. Among them, for the specific implementation of identifying the objects accessed by the application, refer to the subsequent Figure 10 embodiments shown, which will not be elaborated here for the time being.

[0087] In some embodiments, the first time period can be a time period after a preset duration starting from the moment when the application switches from the foreground to the background.

[0088] The above preset duration can be set in advance. For example, the preset duration can be 4 minutes, 5 minutes, or any duration between 4 minutes and 5 minutes. Of course, it can also be other durations.

[0089] In some other embodiments, the first time period can be a time period from the moment when the application switches from the foreground to the background to the target moment. Among them, the target moment can be determined based on the growth rate of the accessed objects recognized by the mobile phone 100.

[0090] Exemplarily, the mobile phone 100 can start from the moment when the application switches from the foreground to the background and periodically obtain the objects accessed by the application, and count the number of the objects accessed by the application obtained. Then, the mobile phone 100 can compare the growth rate of the objects accessed by the application obtained each time with a preset threshold 1. When the growth rate of the objects accessed by the application recognized by the mobile phone 100 is less than or equal to the preset threshold 1, it can be considered that most of the objects accessed by the application in the background have been collected. The mobile phone 100 can terminate the confirmation of the accessed objects to reduce the power consumption of the mobile phone 100. That is to say, the target moment is the moment when the growth rate of the accessed objects recognized by the mobile phone 100 is less than or equal to the preset threshold 1. Correspondingly, the first time period is the time period between the moment when the application switches from the foreground to the background and the moment when the growth rate of the accessed objects recognized by the mobile phone 100 is less than or equal to the preset threshold 1.

[0091] Among them, the calculation formula for the growth rate of the objects accessed by the mobile phone 100 is:

[0092] (The number of the objects accessed by the application obtained in the (i + 1)-th time - The number of the objects accessed by the application obtained in the i-th time) / The duration between the moment when the application starts to detect the existence of the accessed objects in the (i + 1)-th time and the moment when the application starts to detect the existence of the accessed objects in the i-th time, and the (i + 1)-th detection and the i-th detection are two adjacent detections.

[0093] In some other embodiments, the first time period can be determined by comparing the time period after a preset duration from the moment when the application switches from the foreground to the background with the target moment for the mobile phone 100. If the target moment is within the time period after a preset duration from the moment when the application switches from the foreground to the background, the first time period is the time period from when the application switches from the foreground to the background to the target moment; if the target moment exceeds the time period after a preset duration from the moment when the application switches from the foreground to the background, the first time period is the time period within the preset duration after the application switches from the foreground to the background.

[0094] Among them, for the sake of simplicity of description, the objects accessed by the application within the above-mentioned first time period are hereinafter simply referred to as accessed objects, and the objects not accessed by the application within the first time period are hereinafter simply referred to as unaccessed objects. According to the storage situation of the accessed objects and the unaccessed objects in the page, the pages allocated for the application can be divided into three types of pages:

[0095] The first type of page is the page that stores the accessed objects and the unaccessed objects, and the first type of page can be called the first page. For example, Figure 6 shows a schematic diagram of three types of pages. As Figure 6As shown, Page 1 is the first type of page, and Page 1 stores two accessed objects and two unaccessed objects.

[0096] The second type of page is a page in which all stored objects are accessed objects, and the second type of page can be called the second page. For example, as Figure 6 shown, Page 2 is the second type of page, and the 4 objects stored in Page 2 are all accessed objects.

[0097] The third type of page is a page in which all stored objects are unaccessed objects, and the third type of page can be called the third page. For example, as Figure 6 shown, Page 3 is the third type of page, and the 4 objects stored in Page 3 are all unaccessed objects.

[0098] Another property that the application has when running in the background is that when the application runs in the background, the objects accessed by the application are generally scattered and stored in multiple different pages, that is, when the application runs in the background, there will be multiple first pages (such as Page 1) in the pages allocated for the application. As described above, for a page of the type like the first page, since there are accessed objects in the first page, the first page cannot be released, resulting in the memory space occupied by the unaccessed objects stored in the first page not being able to be released. Based on this property, the mobile phone 100 can perform the following steps 502-504 to release the memory space occupied by the unaccessed objects in the first page.

[0099] 502. Determine the first page to be processed from the multiple pages allocated for the application based on the first object.

[0100] The mobile phone 100 can determine the type of the page according to the distribution of the accessed objects and the unaccessed objects in the page. Specifically, in some embodiments, if a page allocated for the application stores both accessed objects and unaccessed objects, then it is determined that the page is a first page. All the first pages determined in this way are the first pages to be processed in the subsequent step 503.

[0101] It can be understood that in some embodiments, if among all the first pages determined based on the above method, there is a situation where there are too many accessed objects in a first page, in order to maintain the integrity of page management, then no processing is performed on this first page, and the other first pages except this one are the first pages to be processed in the subsequent step 503.

[0102] Specifically, if the ratio of the sizes of all the accessed objects stored in the first page to the size of the page is greater than the preset threshold 2, no processing is performed on this type of first page. For other first pages, that is, the first pages where the ratio of the sizes of all the accessed objects stored to the size of the page is less than or equal to the preset threshold 2, they are the first pages to be processed in the subsequent step 503.

[0103] 503. When the number of the first pages to be processed is greater than or equal to 2, copy the objects accessed by the application in the first pages to be processed to the target page in the memory, copy all the data in the first pages to be processed to the storage area other than the memory, and release the first pages to be processed.

[0104] If the number of the first pages to be processed is greater than or equal to 2, this indicates that the distribution of the accessed objects in the pages is relatively sparse, the memory space occupied by the unaccessed objects is relatively large, and there is a large waste of memory resources. To save memory, the mobile phone 100 can first centrally store the accessed objects distributed in different pages to the target page, copy all the data stored in these pages to the storage area other than the memory, and release these pages.

[0105] As described above, the mobile phone 100 can access the objects in the page to implement the running of the functions of the application in the background. Since the objects accessed by the application in the first time period have been determined previously, and these objects are more likely to be accessed subsequently, while those objects that were not accessed by the application in the first time period are less likely to be accessed subsequently, therefore, the above-mentioned first objects should continue to be stored in the memory so that the mobile phone 100 can efficiently access these objects, and the above-mentioned unaccessed objects can be copied to other storage areas, thereby releasing the memory space they occupy.

[0106] In addition, as described above, the mobile phone 100 manages the memory in units of pages. Therefore, the mobile phone 100 can first centrally copy the accessed objects stored in the first page, that is, the first objects, to the target page, and copy all the data in the first page to the storage area other than the memory. Then, the mobile phone 100 releases the first page.

[0107] For example, Figure 7 shows a schematic diagram of the page change before and after the execution of a memory management method. As Figure 7As shown in (a) therein, the pages allocated for Application 1 include Page 1, Page 2, and Page 3. By performing the above steps, Page 2 and Page 3 can be determined as the first pages. For example, Object 4 that has been accessed is stored in Page 2, and Object 5 and Object 6 that have been accessed are stored in Page 3. It can be understood that there are two first pages among the pages allocated for Application 1, which indicates that the distribution of the first objects in the pages allocated for Application 1 is relatively sparse, and the memory space occupied by the unaccessed objects is relatively large. Then, as Figure 7 shown in (b) therein, the mobile phone 100 can centrally store Object 4, Object 5, and Object 6 stored in Page 2 and Page 3 into a target page in the memory, copy all the data in Page 2 and Page 3 to a storage area other than the memory, and then release Page 2 and Page 3.

[0108] Before executing the memory management method provided in the embodiments of the present application, both accessed objects and unaccessed objects are stored in Page 2 and Page 3. Then, the memory space occupied by the unaccessed objects in Page 2 and Page 3 cannot be recycled. By executing the memory management method of the embodiments of the present application, by integrating Object 0, Object 1, Object 2, Object 3, Object 4, Object 5, and Object 6 into a target page and releasing Page 2 and Page 3 that originally stored the accessed objects, the two pages occupied by the unaccessed objects in Page 2 and Page 3 can be recycled, and the remaining available capacity of the memory becomes larger. In this way, the electronic device can obtain more available memory, ensuring that the applications running in the foreground have relatively sufficient memory to use, enabling them to run more smoothly and improving the user experience.

[0109] The target page is a page allocated for the application by the operating system of the mobile phone 100 based on the sizes of the accessed objects in all the first pages to be processed. The target page may include one or more pages, and the size of the target page satisfies the following condition: the difference between the size of the target page and the sizes of all the first objects is less than the size of one page.

[0110] Specifically, the application can send a memory allocation request to the operating system to request the operating system to allocate memory space for the application. Information about the sizes of the accessed objects in the first pages to be processed is carried in the memory allocation request. After receiving the memory allocation request sent by the application, the operating system allocates a page for the application based on the sizes of the accessed objects in all the first pages to be processed.

[0111] Specifically, in some embodiments, the operating system of the mobile phone 100 can divide the sizes of the accessed objects in all the first pages to be processed by the size of one page, and allocate a page for the application based on the division result.

[0112] If the division result is less than or equal to 1, the number of target pages is 1, and the operating system of the mobile phone 100 allocates 1 page for the application, and this 1 page is the target page. For example, as Figure 2 shown, the sizes of object 0, object 1, object 2, and object 3 are 4096 bytes, the size of one page is 4096 bytes, and the division of the previous two values is 1, then the operating system of the mobile phone 100 allocates 1 page for the application, and this 1 page is the target page.

[0113] If the division result is greater than 1 and is an integer, the value of the division result is the number of target pages, and the operating system of the mobile phone 100 allocates the corresponding number of pages for the application, and these pages are the target pages.

[0114] If the division result is greater than 1 and is a decimal, the value obtained by adding 1 to the integer part of the division result is the number of target pages, and the operating system of the mobile phone 100 allocates the corresponding number of pages for the application, and these pages are the target pages.

[0115] When the number of the first pages to be processed is 1, if all the data in the 1 first page to be processed is copied to a storage area other than the memory and the first page is released, this processing of the first page to be processed does not save memory. Therefore, when the number of the first pages to be processed is 1, no processing is performed on this one first page to be processed.

[0116] For example, Figure 8 shows a schematic diagram of page changes before and after the execution of a memory management method. As Figure 8 shown in (a) therein, the pages allocated for the application are page 1, page 2, and page 3. Page 1 is the second page, page 2 is the third page, and page 3 is the first page. The pages allocated for the application only include 1 first page. As Figure 8 shown in (b) therein, the mobile phone 100 does not perform any processing on page 3. It can be understood that if the page is the second page, no processing is performed on the second page either. For example, as Figure 8 shown in (b) therein, no processing is performed on page 1. If the page is the third page, all the data stored in the third page is copied to a storage area other than the memory, and then the third page is released. For example, as Figure 8 shown in (b) therein, all the data stored in page 2 is copied to a storage area other than the memory, and page 2 is released.

[0117] Copying the objects accessed by the application in the first page to be processed to the target pages in the memory can be achieved through the following steps:

[0118] Step 1: Obtain the target physical address of the target page.

[0119] In some embodiments, the application may first send a memory allocation request to the operating system. The memory allocation request is used to request the allocation of pages for a first object. After receiving the memory allocation request sent by the application, the operating system checks whether the remaining available capacity in the memory meets the size of the memory requested by the application. If the remaining available capacity in the memory meets the size of the memory requested by the application, the operating system allocates one or more pages to the application, such as sending the physical addresses of the allocated pages to the application. The one or more pages are target pages. After the mobile phone 100 obtains the physical addresses of the pages allocated to the application, it can store data in the pages corresponding to the physical addresses.

[0120] Step 2: Based on the physical addresses of the objects accessed by the application in the first page to be processed in the preset queue, obtain the objects accessed by the application in the first page to be processed.

[0121] For the introduction of the preset queue, refer to the embodiments shown later Figure 10 and will not be elaborated here temporarily.

[0122] Step 3: Based on the target physical address, store the objects accessed by the application in the first page to be processed into the target page.

[0123] In addition, the storage area other than the memory in the embodiments of the present application may be a swap area allocated by the virtual machine. The relationship between the virtual machine and the storage area other than the memory will be introduced below.

[0124] Figure 9 shows a schematic diagram of the virtualization architecture of a mobile phone 100. As Figure 9 shown, the mobile phone 100 includes a processor, a memory, and a virtual machine.

[0125] With the help of virtualization technology, the mobile phone 100 can form software modules with independent operating environments on the mobile phone 100. In the embodiments of the present application, the software modules with independent operating environments formed on the mobile phone 100 are called virtual machines. A virtual machine is a "complete computer" that is simulated through virtualization technology and has the functions of a complete hardware system and runs in a completely isolated environment. All the work that can be completed in the entity of the mobile phone 100 can be realized in the virtual machine. The virtual machine has components such as a processor (this processor is also called a virtual processor) and a memory. The processor, memory, etc. of the virtual machine are all virtualized from the processor, memory, etc. of the mobile phone 100. An operating system is installed on the virtual machine, and the operating system on the virtual machine is independent of the operating system of the mobile phone 100 itself.

[0126] The processor includes a Memory Management Unit (MMU), which is also known as a Paged Memory Management Unit (PMMU). In the embodiments of the present application, the MMU can manage the memory space in page granularity (i.e., paged memory management). The MMU can use the Garbage Collection (GC) mechanism in the virtual machine to copy the accessed objects stored in the first page to the target page, copy the data stored in the first page to the storage area other than the memory, and release the first page.

[0127] The garbage collection mechanism involves physical memory and virtual memory. The physical memory is the memory of the mobile phone 100. The virtual memory is the storage area other than the memory.

[0128] The virtual memory can be called a swap area, which is usually located in the local storage device of the mobile phone 100. The local storage device of the mobile phone 100 can include the storage area integrated inside the mobile phone 100, or can also include a storage device connected to the interface of the mobile phone 100 (such as Figure 4 the external memory interface 120 in). The storage area integrated inside the mobile phone 100 can be the hard disk of the mobile phone 100, etc. The hard disk of the mobile phone 100 refers to the hardware device for storing data, which usually exists in the form of storage chips such as Embedded Multi Media Card (eMMC), Universal Flash Storage (UFS), or Non-Volatile Memory Express (NVMe).

[0129] In the embodiments of the present application, the virtual memory can also be located in a remote storage device. The remote storage device refers to a storage device connected to the mobile phone 100 through a network. The remote storage device can be a device corresponding to a private cloud or a public cloud.

[0130] Next, an exemplary method for determining a first object based on an Access Violation event is introduced.

[0131] Figure 10 A flowchart showing a method for confirming a first object is shown. The execution subject of this process can be an electronic device, such as the mobile phone 100, as Figure 10 shown, this process includes the following steps:

[0132] 1001. In response to the application switching from the foreground to the background, mark the read / write permissions of all pages allocated to the application as non-readable / writable.

[0133] When the read / write permission of the page in the memory is not readable / writable, if the application accesses the object stored in the page, the object cannot be accessed because the read / write permission of the page where it is located is not readable / writable, and an access violation event will occur. The physical address of the accessed object will be carried in the generated access violation event. In this way, when the mobile phone 100 detects an access violation event, it can determine the object stored in the corresponding storage area corresponding to the physical address carried in the access violation event as the first object.

[0134] Based on this, in the embodiment of the present application, when the application switches from the foreground to the background, the mobile phone 100 can set the read / write permissions of all pages allocated to the application to not readable / writable. In this way, if the application accesses the objects stored in these pages, an access violation event will occur. Based on the access violation events generated within a period of time after the application switches from the foreground to the background, the mobile phone 100 can determine the above-mentioned first object. The specific implementation can refer to the following steps 1002-step 1004.

[0135] 1002. Detect whether there is an access violation event. If there is an access violation event, determine the object corresponding to the physical address carried in the access violation event as the first object, and store the physical address of the first object in a preset queue.

[0136] In some embodiments, the preset queue can be a Hot Object Queue. A hot object queue generally refers to a queue used to store and manage frequently accessed or objects that need to be processed preferentially (such as called hot objects).

[0137] 1003. Mark the read / write permission of the page where the object corresponding to the physical address carried in the access violation event is located as readable / writable.

[0138] After the mobile phone 100 stores the physical address carried in the access violation event in the preset queue, the mobile phone 100 can mark the read / write permission of the page where the object corresponding to the physical address carried in the access violation event is located as readable / writable to ensure that the application can continue to execute.

[0139] 1004. Determine whether the time period from the moment when the application switches to the background to the current moment exceeds the first time period.

[0140] As described in the previous embodiments, no new accessed objects will appear after a period of time when the application switches from the foreground to the background, such as after the first time period. Therefore, in some embodiments, the mobile phone 100 can determine whether the time period from the moment when the application switches to the background to the current moment exceeds the first time period. If the time period from the moment when the application switches to the background to the current moment exceeds the first time period, which indicates that basically no new accessed objects will appear, then the mobile phone 100 can stop determining the first object. After that, the mobile phone 100 can mark the read / write permissions of all the pages allocated to the application as readable / writable, so that the application can continue to execute, that is, the mobile phone 100 can execute 1005.

[0141] In some embodiments, when the first time period is the time period after a preset duration starting from the moment when the application switches from the foreground to the background and the preset duration is pre-set, the mobile phone 100 can start timing the preset duration from the moment when the application switches to the background and execute 1005 when the preset duration is reached.

[0142] In some embodiments, when the first time period is the time period from the moment when the application switches from the foreground to the background to the target moment and the target moment is determined based on the growth rate of the accessed objects recognized by the mobile phone 100, the mobile phone 100 can start from the moment when the application switches to the background and periodically determine the number of accessed objects obtained, and execute 1005 when the growth rate of the accessed objects is less than or equal to the preset threshold 1. The formula for determining the growth rate of the accessed objects has been introduced above and will not be repeated here.

[0143] In some embodiments, the first time period can be determined by the mobile phone 100 by comparing the time period after a preset duration starting from the moment when the application switches from the foreground to the background with the target moment. Specifically, if the target moment is within the time period after a preset duration starting from the moment when the application switches from the foreground to the background, the mobile phone 100 can start from the moment when the application switches to the background and periodically determine the number of accessed objects obtained, and execute 1005 when the growth rate of the accessed objects is less than or equal to the preset threshold 1. If the target moment exceeds the time period after a preset duration starting from the moment when the application switches from the foreground to the background, then the first time period is the time period within the preset duration after the application switches from the foreground to the background, and the mobile phone 100 can start from the moment when the application switches to the background and periodically determine the number of accessed objects obtained, and execute 1005 when the preset duration is reached.

[0144] If the time period from the moment when the application switches to the background to the current moment does not exceed the first time period, the mobile phone 100 can continue to determine the objects accessed by the application. For example, the mobile phone 100 can re-execute step 1001-step 1004 after a preset time. In the embodiments of the present application, the preset time can be 5 seconds, or 4 seconds, 4.5 seconds, 6 seconds, etc. The embodiments of the present application do not specifically limit this preset time. That is to say, within the first time period, the mobile phone 100 can collect the accessed objects at a fixed frequency. For example, the above steps 1001-step 1004 are executed every 5 seconds to collect the objects accessed by the application during a period of time when the application exits and runs in the background.

[0145] Although the access of the application to the objects on the page can only be collected once at a fixed frequency, since there are only a few objects accessed by the application on the page allocated to the application, this method is sufficient to collect most of the objects accessed by the application to a certain extent.

[0146] 1005. Mark the read-write permissions of all the pages allocated to the application as readable and writable.

[0147] Mark the read-write permissions of all the pages allocated to the application as readable and writable so that the application can run normally.

[0148] The following introduces the proof data of memory savings after executing the memory management method provided by the embodiments of the present application.

[0149] Table 1 shows the memory capacity saved after multiple applications execute the memory management method provided by the embodiments of the present application. As shown in Table 1, each row in the table indicates the ratio of an application, the number of the first pages to the total number of pages allocated to the application, and the memory capacity that the application can save after executing the memory management method provided by the embodiments of the present application. Among them, the first page in the table refers to the page with the number of stored accessed objects less than 5.

[0150] Table 1

[0151]

[0152] As shown in Table 1 above, after the mobile phone 100 executes the memory management method provided by the embodiments of the present application, when the existing application 1 runs in the background, 8.45 MB of memory can be saved. When the existing application 2 runs in the background, 4.82 MB of memory can be saved. When the existing application 3 runs in the background, 13.06 MB of memory can be saved. When the existing application 4 runs in the background, 9.31 MB of memory can be saved. Other applications will not be elaborated.

[0153] It can be understood that the above proof data can prove that the mobile phone 100 executes the memory management method provided in the embodiments of the present application, which can reduce the memory consumption of applications in the background and save memory resources.

[0154] In summary, after the mobile phone 100 executes the memory management method provided in the embodiments of the present application, on the one hand, it can save memory, reduce the memory consumption of applications in the background, save memory resources, and ensure that the applications running in the foreground have relatively sufficient memory to use, enabling them to run more smoothly and improving the user experience. On the other hand, it can be understood that a page fault means that the object to be accessed by the mobile phone 100 is not in the memory, and the mobile phone 100 needs to write the object stored in the storage area other than the memory into the memory before accessing it. When a page fault occurs, the mobile phone 100 needs to read the object from the storage area outside the memory and write the object into the memory, which is much slower than directly accessing the memory. Therefore, frequent page faults will cause the mobile phone 100 to run applications more slowly. In the memory management method provided in the embodiments of the present application, the mobile phone 100 can centrally store the first objects distributed in different pages to the target page, which reduces the number of page faults to a certain extent and improves the speed at which the mobile phone 100 runs applications. The mobile phone 100 can reduce the number of page faults to a certain extent and improve the speed at which the mobile phone 100 runs applications.

[0155] The embodiments of the present application also provide a computer storage medium, which includes computer instructions. When the computer instructions run on the above terminal device, the terminal device is enabled to execute each function or step that the mobile phone executes in the above method embodiments.

[0156] The embodiments of the present application also provide a computer program product. When the computer program product runs on a computer, the computer is enabled to execute each function or step that the mobile phone executes in the above method embodiments.

[0157] It can be understood that in order to implement the above functions, the terminal device provided in the embodiments of the present application includes the corresponding hardware structure and / or software module for executing each function. Those skilled in the art should easily realize that, combining the units and algorithm steps of each example described in the embodiments disclosed in this article, the embodiments of the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a certain function is executed in the way of hardware or computer software driving the hardware depends on the specific application and design constraints of the technical solution. Professional technicians 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 embodiments of the present application.

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

[0159] From the description of the above embodiments, those skilled in the art can clearly understand that for the convenience and brevity of description, only the division of the above functional modules is used as an example. In actual applications, the above functions can be allocated to different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above.

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

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

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

[0163] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on such an understanding, the technical solution of the embodiments of the present application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. The software product is stored in a storage medium and includes several instructions for causing a device (which can be a single-chip microcomputer, a chip, etc.) or a processor to execute all or part of the steps of the methods described in the embodiments of the present application. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read only memory (ROM), random access memory (RAM), magnetic disks, or optical discs that can store program codes.

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

Claims

1. A memory management method, characterized in that Applied to an electronic device, the method includes: In response to the application switching from the foreground to the background, determining a first page from among a plurality of pages allocated for the application in the memory of the electronic device; wherein, the pages allocated for the application are used to store objects corresponding to implementing the functions of the application. When the number of the first pages is multiple, centrally copying the first objects stored in the multiple first pages to a target page in the memory, and copying all the objects stored in the multiple first pages to a storage area other than the memory, releasing the multiple first pages, and the difference between the size of the target page and the size of all the first objects is less than the size of one page. Wherein, the first page is a page among the multiple pages that meets the conditions, and the conditions include: the page includes a first object and a second object; the first object is an object accessed by the application within a first time period after the application switches from the foreground to the background, and the second object is an object not accessed by the application within the first time period.

2. The method according to claim 1, wherein The conditions further include: The ratio of the size of the first object included in the page to the size of the page is less than a threshold.

3. The method according to claim 1 or 2, characterized in that Before obtaining the first page from among the multiple pages allocated for the application in the memory of the electronic device, the method further includes: Determining the first object.

4. The method according to claim 3, wherein The determining the first object includes: Marking the read / write permissions of the multiple pages as non-read / writable. Within the first time period, detecting whether there is an access violation event. When detecting the access violation event, determining the object corresponding to the physical address carried in the access violation event as the first object.

5. The method according to claim 4, wherein The detecting whether there is an access violation event within the first time period includes: periodically detecting whether there is an access violation event within the first time period. The method further includes: in each detection, when detecting the access violation event, marking the read / write permissions of the page where the object corresponding to the physical address carried in the access violation event is located as read / writable, and before starting the next detection, re-marking the read / write permissions of the page where the object corresponding to the physical address carried in the access violation event is located as non-read / writable.

6. The method according to claim 4, wherein The method further includes: After the first time period, marking the read / write permissions of the multiple pages as read / writable.

7. The method according to any one of claims 4-6, wherein The method further includes: storing the physical address of the first object in a preset queue. The copying the first objects stored in the multiple first pages to the target page in the memory includes: according to the physical addresses of the first objects in the multiple first pages stored in the preset queue, copying the first objects stored in the multiple first pages to the target page in the memory.

8. The method according to any one of claims 4-6, wherein The first time period is a time period within a preset duration after the application switches from the foreground to the background, and the preset duration is set in advance; Or; The first time period is a time period from when the application switches from the foreground to the background to a target time, and the target time is determined based on the growth rate of the first object determined in two adjacent detections; Or; The first time period is determined based on the target time and the preset duration.

9. The method according to any one of claims 1, 2, 5, and 6, characterized in that Before copying the first objects stored in the multiple first pages to the target page in the memory, the method further includes: Allocating the target page for the application based on the sizes of all the first objects stored in the multiple first pages.

10. An electronic device, characterized in that, The electronic device includes at least a memory and one or more processors; the memory is used to store computer instructions, and when the one or more processors execute the computer instructions, the electronic device executes the method according to any one of claims 1 to 9.

11. A computer-readable storage medium, characterized in that, Includes computer instructions, and when the computer instructions run on an electronic device, the electronic device executes the method according to any one of claims 1 to 9.

12. A computer program product, characterized in that, When the computer program product runs on an electronic device, the electronic device executes the method according to any one of claims 1 to 9.

Citation Information

Patent Citations

  • Object management method and device

    CN116737350A

  • Memory management method and electronic device

    WO2024027544A1