Memory management method, electronic device, storage medium and program product
By distinguishing the application objects of electronic devices into hot objects and cold objects according to access probability, and prioritizing the compression of cold objects in the cold object area when memory is insufficient, the system load increase caused by repeated compression and decompression of memory pages is solved, and the response speed and performance of electronic devices are improved.
Patent Information
- Application Number
- CN202510141512.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-08
- Publication Date
- 2025-07-04
- Estimated Expiration
- 2045-02-08
AI Technical Summary
In the prior art, electronic devices randomly compress and decompress memory pages when memory is insufficient lead to increased system load, reducing response speed and usage performance.
The application objects in the electronic device are divided into hot objects and cold objects according to the access probability, stored in different target areas, and the areas corresponding to the cold objects are compressed first.
By reducing the probability of decompression of compressed areas caused by cold objects being accessed, the response speed and usage performance of electronic devices are improved.
Smart Images

Figure CN119576806B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of memory management, and in particular, to a memory management method, an electronic device, a storage medium, and a program product. Background Art
[0002] When the application program of an electronic device is in the startup state, different objects corresponding to the application program will be created according to different task requirements. For example, if the task is to add contact A in an instant messaging application program, the application program can create an object corresponding to contact A to store the information corresponding to contact A. Another example is that if the task is to load the corresponding configuration file when the instant messaging application program is started, the application program can create a configuration object to store and manage the configuration file.
[0003] It can be understood that each object created by the application program will be stored in the memory of the electronic device and occupy a certain amount of memory space. Specifically, the memory can be divided into multiple memory pages of a fixed size, and the objects of the application program can be randomly stored in different memory pages. Since the objects of the application program include objects with a relatively high access probability (hereinafter referred to as hot objects) and objects with a relatively low access probability (hereinafter referred to as cold objects), each memory page may store hot objects and / or cold objects. If the available memory space of the electronic device is insufficient, the electronic device will randomly compress some of the multiple memory pages to increase the available memory space.
[0004] It can be understood that if one or more objects in the compressed memory page need to be accessed, the compressed memory page needs to be decompressed. Therefore, in the above-mentioned random memory page compression scheme, the probability that the compressed memory page includes hot objects is relatively high, which leads to a relatively high probability that the compressed memory page needs to be decompressed because the hot object needs to be accessed. This repeated compression and decompression of the memory page will increase the load of the system, reduce the response speed of the electronic device, and thus affect the usage performance of the electronic device. Summary of the Invention
[0005] Embodiments of this application provide a memory management method, an electronic device, a storage medium, and a program product to improve the response speed of the electronic device, and thus improve the usage performance of the electronic device.
[0006] In a first aspect, the present application provides a memory management method, which is applied to an electronic device. The method includes: detecting that a first application program of the electronic device is in a startup state, where the first application program corresponds to multiple application objects; determining a first object belonging to a first category among the multiple application objects, and transferring the first object from a first initial area to a first target area; determining a second object belonging to a second category among the multiple application objects, and transferring the second object from a second initial area to a second target area; performing a compression operation on the second target area, where the probability of the first object belonging to the first category being accessed is greater than the probability of the second object belonging to the second category being accessed.
[0007] This method transfers the first object and the second object to different target areas (i.e., the first target area and the second target area), and preferentially compresses the second target area corresponding to the second object. Since the probability of the second object in the second target area being accessed is small, this method can reduce the probability of the compressed second target area being decompressed due to the access of the second object, improve the response speed of the electronic device, and thus improve the usage performance of the electronic device.
[0008] In a possible implementation of the first aspect, before determining the first object belonging to the first category among the multiple application objects, the method further includes: detecting that the remaining memory of the electronic device meets the memory cleaning condition.
[0009] In this case, after the electronic device determines the first object and the second object, it can directly perform a compression operation on the second target area.
[0010] In a possible implementation of the first aspect, before determining the first object belonging to the first category among the multiple application objects, the method further includes: detecting a first instruction, or detecting that the time interval between the current moment and a first moment is greater than or equal to a time threshold, where the first moment is the starting moment when the first application program is in a startup state; before performing a compression operation on the second target area, the method further includes: detecting that the remaining memory of the electronic device meets the memory cleaning condition.
[0011] In this case, the electronic device can start to determine the first object and the second object when detecting the first instruction or the time interval is greater than or equal to the time threshold. Then, when it is determined that the remaining memory meets the memory cleaning condition, a compression operation is performed on the second target area.
[0012] In a possible implementation of the first aspect, the multiple application objects include multiple foreground objects and multiple background objects, where the multiple foreground objects are objects created when the first application program runs in the foreground, and the multiple background objects are objects created when the first application program runs in the background.
[0013] In a possible implementation of the first aspect, when the first application is running in the background, determining a first object belonging to the first category among multiple application objects includes: regarding multiple background objects as the first object belonging to the first category; determining a second object belonging to the second category among multiple application objects includes: regarding multiple foreground objects as the second object belonging to the second category.
[0014] It can be understood that the startup state of the first application includes the foreground running state or the background running state. That is to say, whether the electronic device detects that the first application is in the foreground running state or the background running state, it can determine that the first application is in the startup state, and then determine the first object and the second object.
[0015] Since foreground objects are responsible for displaying information and receiving user input, such as the display window and related controls of the application program, etc., therefore, when the first application is running in the background, the probability of foreground objects being accessed is relatively small, and the probability of background objects being accessed is relatively large. When the first application is in the background running state, the electronic device can regard background objects as the first object belonging to the first category; and regard multiple foreground objects as the second object belonging to the second category.
[0016] In a possible implementation of the first aspect, when the first application is running in the background, determining a first object belonging to the first category among multiple application objects includes: determining a first foreground object belonging to the first category among multiple foreground objects; regarding multiple background objects and the first foreground object as the first object belonging to the first category; determining a second object belonging to the second category among multiple application objects includes: regarding the objects among multiple foreground objects except the first foreground object as the second object belonging to the second category.
[0017] It can be understood that when the first application is running in the background, there may still be some foreground objects with a relatively high probability of being accessed. Therefore, in this method, in addition to directly regarding multiple background objects as the first object belonging to the first category, the electronic device can also determine a first foreground object belonging to the first category from multiple foreground objects, and then regard multiple background objects and the first foreground object together as the first object.
[0018] In a possible implementation of the first aspect, determining a first foreground object belonging to a first category among multiple foreground objects includes: obtaining at least one creation thread corresponding to a first application, where the at least one creation thread is used to create multiple foreground objects; determining at least one first creation object created by a first creation thread among the at least one creation thread, and the at least one first creation object belongs to the multiple foreground objects; corresponding to the at least one first creation object, if the ratio of the number of first objects that are accessed to the second number of the at least one first creation object is greater than a first ratio threshold, taking the at least one first creation object as the first foreground object belonging to the first category.
[0019] It can be understood that if the ratio of the first number to the second number is greater than the first ratio threshold, it means that the probability of the first creation object created by the first creation thread being accessed when the first application runs in the background is relatively high. Therefore, all at least one first creation object corresponding to the first creation thread is taken as the first foreground object.
[0020] In this method, when the electronic device determines that the ratio of the number of accessed objects among the at least one first creation object is greater than the first ratio threshold, all at least one first creation object is taken as the first object and transferred and stored in the first target area. In this case, the objects other than the first creation object whose corresponding ratio is greater than the first ratio threshold among the foreground objects are taken as the second objects and stored in the second target area.
[0021] In this way, the probability of the second object in the second target area being accessed is relatively small, which can reduce the probability of the compressed second target area being decompressed again due to the access of the second object. Therefore, the response speed of the electronic device can be improved, and the usage performance of the electronic device can be improved.
[0022] In a possible implementation of the first aspect, determining a first foreground object belonging to a first category among multiple foreground objects further includes: obtaining at least one access thread corresponding to the at least one first creation object, where the at least one access thread is used to access the at least one first creation object; determining the third number of the first creation objects accessed by a first access thread among the at least one access thread; corresponding to the ratio of the third number to the second number being greater than a second ratio threshold, taking the foreground object accessed by the first access thread as the first foreground object belonging to the first category.
[0023] In this method, the electronic device can further determine the first foreground object based on the access thread on the basis of determining the first foreground object based on the creation thread.
[0024] It can be understood that if the proportion of the third quantity occupying the second quantity is greater than the second proportion threshold, it indicates that the probability of the foreground objects accessed by the first access thread being accessed when the first application is running in the background is relatively high. Therefore, the foreground objects accessed by the first access thread can be regarded as the first foreground objects and transferred and stored in the first target area.
[0025] In this way, the probability of the second object in the second target area being accessed is further reduced, and the probability of the compressed second target area being decompressed again due to the access of the second object can be further reduced. Therefore, the response speed of the electronic device can be improved, and the usage performance of the electronic device can be improved.
[0026] In a possible implementation of the first aspect, the object header of each application object among multiple application objects includes a creation thread data bit and an access thread data bit; obtaining at least one creation thread corresponding to the first application includes: determining the creation thread of each application object based on the data corresponding to the creation thread data bit in each application object, so as to obtain at least one creation thread for creating multiple application objects; obtaining at least one access thread corresponding to at least one first creation object includes: determining the access thread of each first creation object based on the data corresponding to the access thread data bit of each first creation object among at least one first creation object, so as to obtain at least one access thread for accessing at least one first creation object.
[0027] As described later Figure 6 and Figure 12 As shown, when creating an application object, the electronic device can respectively add a creation thread data bit and an access thread data bit to the object header. Among them, the creation thread data bit is used to store (or record) the creation thread of the application object. The access thread data bit is used to store (or record) the access thread of the application object. In this way, the electronic device can respectively determine the creation thread and the access thread of the application object based on the data corresponding to the creation thread data bit and the data corresponding to the access thread data bit. It can be understood that in the structure of the application object before and after adding the creation thread data bit and the access thread data bit, the size of the data part remains unchanged, so that the data carrying capacity of the application object can be ensured to remain unchanged.
[0028] Taking the electronic device using Android TM system as an example, if the electronic device wants to change the data structure of the application object, it can modify the creation logic of the application object in the Android runtime (ART), such as recording the creation thread corresponding to the application object when the application object is created and recording the access thread of the application object when the application object is accessed.
[0029] In some embodiments, the electronic device may modify the creation logic of application objects in the Android runtime environment based on a switch interpreter. For example, the electronic device modifies acquisition instructions (such as sget instructions, aget instructions, etc.) based on the switch interpreter, so that the electronic device can obtain the creation thread and access thread corresponding to the application object based on the modified acquisition instructions.
[0030] By changing the creation logic of the application object, the method enables the electronic device to obtain the creation thread and access thread through the data structure of the application object.
[0031] In a possible implementation of the first aspect, determining a first object belonging to a first category among multiple application objects includes: obtaining an object type set including multiple object types; regarding application objects among the multiple application objects whose object types belong to the object type set as the first objects belonging to the first category; determining a second object belonging to a second category among the multiple application objects includes: regarding application objects other than the first objects among the multiple application objects as the second objects belonging to the second category.
[0032] It can be understood that the object type set can be the object type of the first object determined in advance. Therefore, if the electronic device determines that the object type of a certain application object belongs to the object type set, it can determine that the application object is a first object. If the electronic device determines that the object type of a certain application object does not belong to the object type set, it can determine that the application object is not a first object but a second object.
[0033] In this way, the electronic device can determine the first object and the second object from multiple application objects based on the pre-determined object type set. The determination speed of the first object and the second object is relatively fast and the accuracy is relatively high. When the determination accuracy of the second object is relatively high, the probability of the second object in the second target area being accessed is reduced, and the probability of the compressed second target area being decompressed again due to the access of the second object can be reduced. Therefore, the response speed of the electronic device can be improved and the usage performance of the electronic device can be improved.
[0034] In a second aspect, the present application provides an electronic device, which includes: one or more processors; one or more memories; one or more programs are stored in the one or more memories, and when the one or more programs are executed by the one or more processors, the electronic device executes the memory management method of the first aspect and any possible implementation of the first aspect.
[0035] In a third aspect, the present application provides a storage medium, on which instructions are stored, and when the instructions are executed on the electronic device, the electronic device executes the memory management method of the first aspect and any possible implementation of the first aspect.
[0036] In a fourth aspect, the present application provides a program product, which includes instructions that, when running on an electronic device, cause the electronic device to execute the memory management method of the first aspect and any possible implementation of the first aspect.
[0037] Among them, for the beneficial effects of the second aspect to the fourth aspect, reference can be made to the beneficial effects of the first aspect and any possible implementation of the first aspect, which will not be elaborated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 According to some embodiments of the present application, a schematic diagram showing a storage method of application objects of a virtual machine and (physical) memory in an electronic device is shown;
[0039] Figure 2 According to some embodiments of the present application, a schematic flowchart showing a memory management method provided by the present application is shown;
[0040] Figure 3 According to some embodiments of the present application, a schematic diagram showing a transfer storage process of a first object and a second object is shown;
[0041] Figure 4 According to some embodiments of the present application, a schematic flowchart showing a method for determining a first foreground object is shown;
[0042] Figure 5 According to some embodiments of the present application, a schematic flowchart showing a method for determining at least one creation thread is shown;
[0043] Figure 6 According to some embodiments of the present application, a schematic diagram showing the structure of an application object before and after adding a creation thread data bit is shown;
[0044] Figure 7 According to some embodiments of the present application, a schematic flowchart showing another method for determining a first foreground object is shown;
[0045] Figure 8 According to some embodiments of the present application, a schematic flowchart showing a method for determining a target creation thread is shown;
[0046] Figure 9 According to some embodiments of the present application, a schematic flowchart showing a method for recording thread information is shown;
[0047] Figure 10 According to some embodiments of the present application, a schematic flowchart showing another method for determining a first foreground object is shown;
[0048] Figure 11According to some embodiments of the present application, a flowchart of a method for determining at least one access thread is shown;
[0049] Figure 12 According to some embodiments of the present application, a schematic structural diagram of an application object before and after adding access thread data bits is shown;
[0050] Figure 13 According to some embodiments of the present application, a flowchart of a method for determining an access thread of an accessed application object is shown;
[0051] Figure 14 According to some embodiments of the present application, a flowchart of a method for determining an initial object type is shown;
[0052] Figure 15 According to some embodiments of the present application, a flowchart of another memory management method provided by the present application is shown;
[0053] Figure 16 According to some embodiments of the present application, a schematic diagram of the virtual machine and memory after the application object is transferred and stored is shown;
[0054] Figure 17 According to some embodiments of the present application, another schematic diagram of the virtual machine and memory after the application object is transferred and stored is shown;
[0055] Figure 18 According to some embodiments of the present application, a flowchart of another memory management method provided by the present application is shown;
[0056] Figure 19 According to some embodiments of the present application, a schematic diagram of the change of an application object during the memory management process is shown;
[0057] Figure 20 According to some embodiments of the present application, a schematic structural diagram of an electronic device is shown. Detailed implementation manners
[0058] Exemplary embodiments of the present application include, but are not limited to, memory management methods, electronic devices, storage media, and program products.
[0059] For ease of understanding, the following first explains the proprietary terms involved in the present application.
[0060] Application Object: A specific instance generated by an application during runtime according to the definition of a class through instantiation (such as using the "new" keyword). Each application object has its own properties and behaviors, and different application objects can perform different tasks when called. Taking the task of adding contact A in an instant messaging application as an example, assuming there already exists a class: Contact (name, age), then the application object created by the application for this task can be: Contact A = Contact (Zhang San, thirty).
[0061] In this application, the application objects of the application can be divided into foreground objects and background objects according to the creation time. Among them, foreground objects are application objects created when the application is in the foreground running state. For example, they can be application objects that directly interact with users, responsible for displaying information and receiving user input, such as the display window and related controls of the application.
[0062] Background objects are application objects created when the application is in the background running state, without directly interacting with users, and usually perform tasks such as logical processing, data storage, and retrieval in the background. Among them, when the application interface of the application is displayed on the display screen of the electronic device, the application is in the foreground running state; when the application interface of the application is not displayed on the display screen of the electronic device and the application is not closed, the application is in the background running state.
[0063] In this application, the application objects of the application can be divided into hot objects and cold objects according to the access probability, and both hot objects and cold objects belong to the reference chain corresponding to the root object, that is, both have the possibility of being accessed by other application objects. Among them, hot objects refer to application objects with a relatively high access probability (such as greater than the access probability threshold), and cold objects refer to application objects with a relatively low access probability (such as greater than 0 and less than or equal to the access probability threshold). It can be understood that the access probability represents the probability that a certain application object is accessed by other application objects.
[0064] Memory Page: The unit of the memory of an electronic device. Each memory page occupies a fixed size of memory in the electronic device and can store application objects of the application inside. When the available memory space (i.e., the remaining memory) of the electronic device is insufficient, the memory page can be compressed to increase the available memory space. If the application object in the compressed memory page needs to be accessed, the compressed memory page needs to be decompressed.
[0065] Virtual machine: A complete computer system simulated by software. In this application, the process in which an application object (such as an access object) of an application accesses another application object (such as an object to be accessed) is implemented based on a virtual machine. Specifically, the virtual address of the object to be accessed is stored inside the virtual machine. When the virtual machine accesses the object to be accessed, the virtual address can be converted into the physical address of the physical memory (i.e., the memory actually storing the object instance) of the electronic device. In this way, the virtual machine can successfully access the object to be accessed based on the physical address of the object to be accessed in the physical memory.
[0066] Figure 1 FIG. shows a schematic diagram of the storage manner of application objects of a virtual machine and (physical) memory in an electronic device. As Figure 1 shown, the virtual addresses of multiple application objects (such as Figure 1 Object A, Object B, Object C, Object D, Object E, Object F in Figure 1 are stored in the virtual machine. In addition, the application objects in the memory are randomly stored in different memory pages. As
[0067] shown, Object A, Object B, and Object C can be stored in memory page a of the memory; Object D, Object E, and Object F can be stored in memory page b of the memory. It can be understood that the storage manner of the application objects in different memory pages in the memory is managed by the kernel of the electronic device.
[0067] Based on the different storage manners of the virtual machine and the application objects in the memory as Figure 1 shown above, the virtual machine accesses the object to be accessed in units of application objects, while the memory stores the application objects in units of memory pages. Taking Figure 1 the compression of memory page a in as an example, if Object A is a hot object, that is, it means that the probability of the virtual machine accessing Object A is relatively high, then the probability of memory page a being decompressed will be relatively high. This way of repeatedly compressing and decompressing the memory page will cause an increase in the load of the system, reduce the response speed of the electronic device, and further affect the usage performance of the electronic device.
[0068] Therefore, to solve the above technical problems, the present application provides a method. This method stores application objects with a relatively high access probability (i.e., hot objects) and application objects with a relatively low access probability (i.e., cold objects) in different memory regions (such as memory pages). In this way, when the memory space is insufficient and the memory region needs to be compressed, the memory region corresponding to the cold object can be compressed preferentially. Specifically, the method includes: after the electronic device detects that the first application program is in the startup state, determining a first object (i.e., hot object) belonging to the first category from multiple application objects corresponding to the first application program, and transferring the first object from the first initial region to the first target region; determining a second object (i.e., cold object) belonging to the second category from the multiple application objects, and transferring the second object from the second initial region to the second target region. Then, the electronic device can preferentially perform a compression operation on the second target region.
[0069] This method transfers and stores the first object and the second object in different target regions (i.e., the first target region and the second target region), and preferentially compresses the second target region corresponding to the second object. Since the second object in the second target region has a relatively low access probability, this method can reduce the probability that the compressed second target region is decompressed due to the access of the second object, improve the response speed of the electronic device, and thus improve the usage performance of the electronic device.
[0070] In some embodiments, the timing for the electronic device to determine the first object and the second object can be when the electronic device detects that the remaining memory meets the memory cleaning condition. In this case, after the electronic device determines the first object and the second object, it can directly perform a compression operation on the second target region.
[0071] In some other embodiments, the timing for the electronic device to determine the first object and the second object can also be when the electronic device detects a first instruction, or when the time interval between the current moment and the start moment when the first application program is in the startup state is greater than or equal to a time threshold. Wherein, the first instruction can be an instruction indicating to determine the first object and the second object among the multiple application objects.
[0072] In the case of the above timing for determining the first object and the second object, the timing for the electronic device to perform a compression operation on the second target region can be when the electronic device detects that the remaining memory meets the memory cleaning condition.
[0073] In some embodiments, the electronic device can preferentially perform a compression operation on the entire region of the second target region, or perform a compression operation on a partial region of the second target region, and the embodiments of the present application do not limit this.
[0074] In some other embodiments, after the second target area is compressed, if the remaining memory of the electronic device is still insufficient, the compression operation can be further performed on part or all of the first target area to further free up memory.
[0075] In some other embodiments, the memory management method provided in this application can be applied to the garbage collection (GC) process of the electronic device.
[0076] Before introducing in detail the memory management method provided in this application, the electronic device applicable to the memory management method will be described first.
[0077] It can be understood that the electronic devices applicable to the memory management method provided in the embodiments of this application may include, but are not limited to, mobile phones, smart TVs, wearable devices, tablets (Pads), computers with wireless transceiver functions, virtual reality (VR) devices, augmented reality (AR) devices, wireless devices in industrial control, wireless devices in self-driving, wireless devices in remote medical surgery, wireless devices in smart grids, wireless devices in transportation safety, wireless devices in smart cities, wireless devices in smart homes, etc. The embodiments of this application do not limit the type of the electronic device.
[0078] First, in combination with Figure 2 , taking the case where the electronic device determines the first object and the second object when it detects that the remaining memory meets the memory cleaning condition as an example, the memory management method provided in this application will be described in detail.
[0079] Figure 2 FIG. shows a schematic flowchart of the first memory management method provided in this application. Figure 2 The execution subject of the shown process is the electronic device. For the convenience of description, the execution subject will not be repeatedly described when introducing the Figure 2 shown processes below. As Figure 2 shown, the method includes, but is not limited to, the following solutions:
[0080] 201: Detect that the first application program of the electronic device is in the startup state.
[0081] It can be understood that in the method provided in this application, the electronic device can determine the first object and the second object among the multiple application objects corresponding to the first application when the first application is in the startup state. Among them, the embodiments of this application do not limit the manner in which the electronic device determines whether the first application is in the startup state. Exemplarily, if the electronic device determines that the process of the first application exists in the list of running processes, it can be determined that the first application is in the startup state.
[0082] In the embodiments of this application, the startup state of the first application includes the foreground running state or the background running state. That is to say, whether the electronic device detects that the first application is in the foreground running state or the background running state, it can be determined that the first application is in the startup state.
[0083] The embodiments of this application do not limit the manner in which the electronic device determines the foreground / background running state of the first application. For example, if the electronic device determines that the display interface of the first application is currently displayed on the display screen, it can be determined that the first application is currently in the foreground running state. If the electronic device determines that the display interface of the first application is not currently displayed on the display screen and the first application is in the opened state, it can be determined that the first application is currently in the background running state.
[0084] 202: It is detected that the remaining memory of the electronic device meets the memory cleaning condition.
[0085] It can be understood that if the electronic device determines that the remaining memory of the electronic device meets the memory cleaning condition, it can execute the subsequent 203 and 204 to respectively determine the first object and the second object among the multiple application objects corresponding to the first application, and transfer and store the first object and the second object to clean the memory of the second object.
[0086] The embodiments of this application do not limit the content of the memory cleaning condition. For example, the memory cleaning condition can be that the remaining memory of the electronic device is less than a preset memory threshold; for another example, the memory cleaning condition can be that the proportion of the remaining memory of the electronic device in the total memory is less than a preset proportion threshold, etc. Among them, the values of the preset memory threshold and the preset proportion threshold can be set according to experience or flexibly adjusted according to the actual application scenario.
[0087] 203: Determine the first object belonging to the first category among the multiple application objects, and transfer and store the first object from the first initial area to the first target area.
[0088] Among them, the first object of the first category is the hot object involved above, that is, the application object with a relatively high probability of being accessed. The determination method of the first object of the first category will be described in detail later and will not be elaborated here for the time being.
[0089] It can be understood that after each of multiple application objects is created, it is first randomly stored in different memory pages. Therefore, taking an application object that is first stored in a first initial area (such as a memory page) as an example, after the electronic device determines that the application object is a first object belonging to the first category, the first object can be transferred and stored from the first initial area to a first target area, where the first target area can be, for example, a memory page for storing the first object.
[0090] The embodiment of the present application does not limit the number of first target areas for storing the first object. For example, the number of first target areas can be one or more.
[0091] When the number of first target areas is multiple, the number of first objects included in each first target area can be the same or different, and the number of first objects included in each first target area can be set according to experience or flexibly adjusted according to the actual application scenario.
[0092] 204: Determine a second object belonging to the second category among multiple application objects, and transfer and store the second object from a second initial area to a second target area.
[0093] Among them, the second object of the second category is the cold object involved above, that is, an application object with a relatively low access probability. The determination method of the second object of the second category will be described in detail later and will not be elaborated here for the time being.
[0094] It can be understood that after each of multiple application objects is created, it is first randomly stored in different memory pages. Therefore, taking an application object that is first stored in a second initial area (such as a memory page) as an example, after the electronic device determines that the application object is a second object belonging to the second category, the second object can be transferred and stored from the second initial area to a second target area, where the second target area can be, for example, a memory page for storing the second object.
[0095] It can be understood that the first initial area and the second initial area in the embodiment of the present application can be the same or different. And the first target area and the second target area are different.
[0096] The embodiment of the present application also does not limit the number of second target areas for storing the second object. For example, the number of second target areas can be one or more.
[0097] When the number of second target areas is multiple, the number of second objects included in each second target area can be the same or different, and the number of second objects included in each second target area can be set according to experience or flexibly adjusted according to the actual application scenario.
[0098] Figure 3 A schematic diagram showing the transfer storage process of a first object and a second object is presented. As Figure 3 shown, taking the number of the first target area and the second target area both being one as an example, the initial area _1 stores object A1, object A2, object A3, and object A4; the initial area _2 stores object B1, object B2, object B3, and object B4. If the electronic device determines that object A1 and object A2 in the initial area _1 are the first object and object A3 and object A4 are the second object, it can transfer and store object A1 and object A2 from the initial area _1 to the first target area, and transfer and store object A3 and object A4 from the initial area _1 to the second target area.
[0099] It can be understood that for object A1 and object A2, the initial area _1 is the first initial area; for object A3 and object A4, the initial area _1 is the second initial area. In this case, the first initial area and the second initial area are the same, both being the initial area _1.
[0100] Similarly, if the electronic device determines that object B1 and object B2 in the initial area _2 are the first object and object B3 and object B4 are the second object, it can transfer and store object B1 and object B2 from the initial area _1 to the first target area, and transfer and store object B3 and object B4 from the initial area _2 to the second target area.
[0101] It can be understood that for object B1 and object B2, the initial area _2 is the first initial area; for object B3 and object B4, the initial area _2 is the second initial area. In this case, the first initial area and the second initial area are the same, both being the initial area _2.
[0102] As Figure 3 shown, after the transfer storage of the first object and the second object, the first target area stores the first object, that is, object A1, object A2, object B1, and object B2, and the second target area stores the second object, that is, object A3, object A4, object B3, and object B4.
[0103] It can be understood that in the method provided by this application, the electronic device can transfer and store the first object and the second object to the first target area and the second target area respectively in a garbage collection thread or a Java (a programming language) thread.
[0104] 205: Perform a compression operation on the second target area, where the access probability of the first object of the first category is greater than the access probability of the second object of the second category.
[0105] When the remaining memory of the electronic device meets the memory cleaning condition, the first object and the second object are determined, and after the first object and the second object are respectively transferred and stored in the first target area and the second target area, the memory cleaning operation can be performed. It can be understood that since the probability of the first object being accessed is greater than that of the second object being accessed, the electronic device can preferentially perform a compression operation on the second target area, that is, perform a compression operation on the memory of the second object.
[0106] Based on the foregoing, the number of second target areas can be one or more. Therefore, when the number of second target areas is multiple, in some embodiments, the electronic device can perform a compression operation on all or part of the second target areas.
[0107] For the case where the electronic device performs a compression operation on part of the second target areas, exemplarily, the electronic device can randomly determine a first preset number of second target areas from multiple second target areas, and then perform a compression operation on the determined second target areas. The first preset number can be set according to experience or flexibly adjusted according to the actual application scenario, and the embodiments of the present application do not limit this.
[0108] Exemplarily, the electronic device can also arrange multiple second target areas in a certain order. For example, when the number of second objects in each second target area is different, the electronic device can arrange multiple second target areas in descending order of the number of second objects included. Then, the electronic device can perform a compression operation on the first second preset number of second target areas. The second preset number can be set according to experience or flexibly adjusted according to the actual application scenario, and the first preset number and the second preset number can be the same or different.
[0109] In some other embodiments, after the second target area is compressed, if the remaining memory of the electronic device is still insufficient, the electronic device can further perform a compression operation on part or all of the first target area to further release memory.
[0110] The memory management method provided by the present application transfers and stores the first object and the second object in different target areas (i.e., the first target area and the second target area), and preferentially compresses the second target area corresponding to the second object. Since the probability of the second object in the second target area being accessed is small, this method can reduce the probability of the compressed second target area being decompressed due to the access of the second object, improve the response speed of the electronic device, and thus improve the usage performance of the electronic device.
[0111] Based on the foregoing, the startup states of the first application include the foreground running state and the background running state. That is to say, in the embodiments of the present application, the electronic device can start memory management both when the first application is in the foreground running state and when the first application is in the background running state. For example, determine the first object and the second object among the multiple application objects of the first application.
[0112] Taking the first application of the electronic device being in the background running state as an example, the determination methods of the first object and the second object respectively involved in the foregoing 203 and 204 are described in detail below. Specifically, the determination methods of the first object and the second object can include the following three types.
[0113] Method 1: The electronic device takes multiple background objects as the first objects belonging to the first category; and takes multiple foreground objects as the second objects belonging to the second category.
[0114] It can be understood that since the foreground objects are responsible for displaying information and receiving user input, such as the display window and related controls of the application, etc., when the first application is running in the background, the probability of the foreground objects being accessed is relatively small. Since the background objects do not directly interact with the user and usually perform tasks such as logical processing, data storage, and retrieval in the background, when the first application is running in the background, the probability of the background objects being accessed is relatively large.
[0115] Therefore, in this method, when the first application is in the background running state, the electronic device can take multiple background objects among the multiple application objects corresponding to the first application as the first objects belonging to the first category (i.e., hot objects); and take multiple foreground objects as the second objects belonging to the second category (i.e., cold objects).
[0116] It can be understood that since the above Method 1 describes the determination methods of the first object and the second object when the first application is running in the background, the first object includes background objects and the second object includes foreground objects. However, it should be understood that when the first application is running in the foreground, the first object can include foreground objects and the second object includes background objects.
[0117] Method 2: The electronic device takes multiple background objects and the first foreground object belonging to the first category among the multiple foreground objects as the first object; and takes the objects other than the first foreground object among the multiple foreground objects as the second objects belonging to the second category.
[0118] It can be understood that when the first application is running in the background, there may still be some foreground objects with a relatively high probability of being accessed. Therefore, in this method, in addition to directly taking multiple background objects as the first objects belonging to the first category, the electronic device can also determine the first foreground objects belonging to the first category from multiple foreground objects, and then take the multiple background objects and the first foreground objects together as the first objects.
[0119] In this case, the electronic device can take the foreground objects other than the first foreground objects belonging to the first category among the multiple foreground objects as the second objects belonging to the second category.
[0120] In some embodiments, the electronic device can determine the first foreground object based on the creation thread corresponding to the application object. Specifically, as Figure 4 shown, the method for determining the first foreground object includes the following process.
[0121] 401: Obtain at least one creation thread corresponding to the first application, and the at least one creation thread is used to create multiple foreground objects.
[0122] It can be understood that each application object corresponding to the first application is created by a creation thread. Therefore, in order to determine the first foreground object from multiple foreground objects, in this process, the electronic device can obtain at least one creation thread used to create multiple foreground objects.
[0123] The embodiments of the present application do not limit the way for the electronic device to obtain at least one creation thread. Exemplarily, the electronic device can change the data structure of each application object. For example, a creation thread data bit can be added to the data structure of the application object, and the creation thread data bit can be used to store (or record) the creation thread of the application object. In this way, the electronic device can determine the creation thread of each application object according to the data corresponding to the creation thread data bit in each application object, and further obtain at least one creation thread of multiple foreground objects among multiple application objects.
[0124] In the above method, when creating a foreground object, the electronic device can record the creation thread of the foreground object based on the data structure of the foreground object. Then, when the electronic device determines that the remaining memory meets the memory cleaning condition and starts to determine the first foreground object, it can directly obtain the recorded at least one creation thread.
[0125] Specifically, as Figure 5 shown, the recording method of at least one creation thread in the present application may include the following process.
[0126] 4011: Create multiple application objects.
[0127] In the embodiments of the present application, when an electronic device creates an application object, it can change the data structure of the created application object. For example, a creation thread data bit can be added to the data structure of the application object to store (or record) the creation thread of the application object.
[0128] Figure 6 The structural schematic diagram of an application object before and after adding a creation thread data bit is shown. As Figure 6 shown in (1) therein, the data structure of the application object mainly includes an object header part, a data part, and an object layout control part. Among them, the object header part can include class reference information and a monitor word. The class reference information is a pointer or reference to the metadata of the class to which the application object belongs. Through the class reference information, the application object can access the attributes and methods of its class. Information such as the hash code of the application object during runtime is stored in the monitor word.
[0129] The data part of the application object is used to store various data of the application object during runtime. The object layout control part can include padding information. In the design of a graphical user interface, padding is used to define the space between the element content and the border.
[0130] The structure of the application object after adding a creation thread data bit can be as shown in (2) therein. It can be understood that the creation thread data bit can be added to the object header part of the application object, and in the structures of the application object before and after adding the creation thread data bit, the size of the data part remains unchanged. In this way, the size of the data structure of the application object after adding the creation thread data bit is larger than the size of the data structure of the application object when the creation thread data bit is not added. Figure 6
[0131] It can be understood that taking an electronic device using the Android TM system as an example, if the electronic device wants to change the data structure of the application object, it can modify the creation logic of the application object in the Android runtime (ART), such as recording the creation thread corresponding to the application object when the application object is created.
[0132] In some embodiments, the electronic device can modify the creation logic of the application object in the Android runtime based on a switch interpreter. For example, the electronic device modifies the fetch instructions (such as sget instructions, aget instructions, etc.) based on the switch interpreter so that the electronic device can obtain the creation thread corresponding to the application object based on the modified fetch instructions.
[0133] 4012: Allocate memory space for each application object.
[0134] After the electronic device creates multiple application objects in the manner involved in 4011 above, it can allocate memory space for each application object, that is, store each application object in the initial area (such as the first initial area and the second initial area involved above).
[0135] 4013: Record the creation threads of the foreground objects among the multiple application objects to obtain at least one creation thread corresponding to the first application program.
[0136] It can be understood that since there is a creation thread data bit for storing the creation thread of the application object in the data structure of each application object created by the electronic device, the electronic device can determine (or record) the creation thread of each foreground object among the multiple application objects based on the data corresponding to the creation thread data bit, and then obtain at least one creation thread corresponding to the first application program.
[0137] It can be understood that the number of application objects corresponding to the first application program is more than the number of creation threads. That is to say, one creation thread may be used to create multiple different application objects. For example, one creation thread may be used to create multiple different foreground objects.
[0138] Taking the application object A1, the application object A2, and the application object A3 as foreground objects as an example, if the creation thread recorded in the creation thread data bit in the data structure of the application object A1 is the thread B1, the creation thread recorded in the creation thread data bit in the data structure of the application object A2 is the thread B1, and the creation thread recorded in the creation thread data bit in the data structure of the application object A3 is the thread B2, then the electronic device can determine based on the data structure of each application object that: the creation thread of the application object A1 is the thread B1, the creation thread of the application object A2 is the thread B1, and the creation thread of the application object A3 is the thread B2. That is to say, the thread B1 created the application object A1 and the application object A2, and the thread B2 created the application object A3.
[0139] Based on this, the electronic device can determine that at least one creation thread corresponding to the first application program is the thread B1 and the thread B2.
[0140] It can be understood that after the electronic device records at least one creation thread, when it determines that the remaining memory meets the memory cleaning condition and starts to determine the first foreground object, it can directly obtain the at least one creation thread recorded previously.
[0141] 402: Determine at least one first creation object created by the first creation thread among the at least one creation thread, and the at least one first creation object belongs to the multiple foreground objects.
[0142] After the electronic device determines at least one creation thread corresponding to the first application, it can determine at least one creation object created by each creation thread. Taking the first creation thread as an example, the electronic device can determine at least one first creation object corresponding to the first creation thread. It can be understood that the at least one first creation object belongs to multiple foreground objects of the first electronic device.
[0143] Taking the first creation thread as thread B1 as an example, the electronic device can determine that at least one first creation object created by thread B1 includes application object A1 and application object A2.
[0144] 403: Among the at least one first creation objects, if the ratio of the first quantity of the accessed objects to the second quantity of the at least one first creation objects is greater than the first ratio threshold, the at least one first creation objects are regarded as the first foreground objects belonging to the first category.
[0145] After the electronic device determines at least one first creation object created by the first creation thread, it can determine the first quantity of the objects that have been accessed (for example, accessed between the start time and the current time when the first application is in the background running state) among the at least one first creation objects during the background running of the first application. If the ratio of the first quantity to the second quantity is greater than the first ratio threshold, it indicates that the probability of the first creation objects created by the first creation thread being accessed during the background running of the first application is relatively high. Then, all the at least one first creation objects corresponding to the first creation thread are regarded as the first foreground objects.
[0146] The embodiments of the present application do not limit the value of the first ratio threshold, which can be set according to experience or flexibly adjusted according to the actual application scenario.
[0147] It can be understood that Figure 4 In the method for determining the first foreground objects shown, when the electronic device determines that the ratio of the quantity of the accessed objects among the at least one first creation objects is greater than the first ratio threshold, all the at least one first creation objects are regarded as the first objects and transferred and stored in the first target area. In this case, the objects other than the first creation objects corresponding to the ratio greater than the first ratio threshold among the foreground objects are regarded as the second objects and stored in the second target area.
[0148] In this way, the probability of the second objects in the second target area being accessed is relatively small, which can reduce the probability of the compressed second target area being decompressed again due to the access of the second objects. Therefore, the response speed of the electronic device can be improved, and the usage performance of the electronic device can be improved.
[0149] In the foregoing Figure 4In the method shown, the electronic device determines the proportion of accessed objects among at least one created object created by each created thread, and uses at least one created object whose corresponding proportion is greater than the first proportion threshold as the first foreground object. In some other embodiments, the electronic device may also directly determine the proportion of accessed objects among at least one target created object created by the target created thread of the accessed foreground object.
[0150] Figure 7 FIG. shows a schematic flowchart of another way for an electronic device to determine a first foreground object based on a created thread corresponding to an application object. As Figure 7 shown, the determination method of the first foreground object includes the following process.
[0151] 701: Obtain at least one created thread corresponding to a first application program, where the at least one created thread is used to create a plurality of foreground objects.
[0152] It can be understood that the execution principle of 701 is the same as that of 401 described above, and will not be elaborated here. In some embodiments, this process is optional.
[0153] 702: Determine at least one target created thread corresponding to at least one foreground object accessed during the background operation of the first application program.
[0154] In the embodiments of the present application, each target created thread in the at least one target created thread may be recorded when the foreground object is accessed. Then, when the electronic device detects insufficient remaining memory and further determines the first foreground object, it can obtain the recorded at least one target created thread.
[0155] In some embodiments, as Figure 8 shown, the way for the electronic device to record the target created thread corresponding to the accessed foreground object may include the following process.
[0156] 7021: Determine the accessed application object.
[0157] It can be understood that the accessed application object may be an accessed foreground object or a background object. Therefore, the electronic device also needs to execute the subsequent 7022, that is, determine whether the accessed application object is a foreground object or a background object.
[0158] 7022: Determine whether the accessed application object is a foreground object.
[0159] It can be understood that the purpose of accessing the application object is to execute relevant instructions or tasks. Therefore, after the electronic device determines the accessed application object, it can start to determine whether the accessed application object is a foreground object during the execution of relevant instructions or tasks, or after the execution of relevant instructions or tasks.
[0160] If the electronic device determines that the accessed application object is a foreground object, it executes 7023 to determine whether the accessed foreground object is accessed for the first time. If the electronic device determines that the accessed application object is not a foreground object, it executes 7025 and does not record the creation thread of the application object.
[0161] 7023: Determine whether the accessed application object is accessed for the first time.
[0162] It can be understood that if the electronic device determines that the application object is accessed for the first time, it executes 7024 to record the creation thread of the application object. If the electronic device determines that the application object is not accessed for the first time, it executes 7025 and does not record the creation thread of the application object.
[0163] 7024: Record the creation thread of the accessed application object and use it as the target creation thread.
[0164] Exemplarily, the electronic device can determine the target creation thread corresponding to the application object according to the data corresponding to the creation thread data bit in the data structure of the accessed application object. Among them, the recording method of the target creation thread of the application object is the same as that in the previous Figure 5 The principle of the shown method is the same and will not be elaborated here.
[0165] In this application, after the electronic device records the creation thread of the accessed application object, it can continue to execute the relevant instructions or tasks involved in 7022 above.
[0166] 7025: Do not record the creation thread of the accessed application object.
[0167] In this application, after the electronic device determines not to record the creation thread of the accessed application object, it can continue to execute the relevant instructions or tasks involved in 7022 above.
[0168] 703: Determine at least one first target creation object created by the first target creation thread in at least one target creation thread, and at least one first target creation object belongs to multiple foreground objects.
[0169] 704: Corresponding to at least one first target creation object, if the proportion of the first target quantity of the accessed application object in the second target quantity of at least one first target creation object is greater than the first proportion threshold, regard at least one first target creation object as the first foreground object belonging to the first category.
[0170] It can be understood that the execution principles of the above 703-704 are similar to those of the relevant processes in 402-403 above and will not be elaborated here.
[0171] Figure 7In the method shown, since at least one target creation thread corresponds to at least one foreground object to be accessed, compared with Figure 4 at least one creation thread in the method shown, the number of at least one target creation thread is smaller. Based on Figure 7 the method shown can make the determination speed of the first foreground object faster, improve the determination efficiency of the first foreground object, and further improve the memory cleaning efficiency.
[0172] It can be understood that the previous Figure 5 and Figure 8 shown methods respectively involve the recording methods of at least one creation thread and the target creation thread corresponding to the foreground object to be accessed. Figure 9 shows a flowchart of a method for recording thread information. As Figure 9 shown, the method for recording thread information includes the following processes.
[0173] 901: Determine that the first application is in the foreground running state.
[0174] 902: Execute related tasks and create foreground objects.
[0175] It can be understood that when the first application is running in the foreground, the electronic device can execute related tasks (that is, the process of executing related programs), and the creation of foreground objects may be involved during the execution of tasks. For example, in response to a user's operation of adding a contact, the electronic device can create a foreground object corresponding to a new contact to add contact information.
[0176] 903: Record at least one creation thread of the created foreground object.
[0177] It can be understood that the execution principle of 903 can refer to the execution principle of the method shown in the previous Figure 5 shown method, and will not be elaborated here.
[0178] 904: Determine that the first application switches to the background running state.
[0179] 905: Record at least one target creation thread corresponding to at least one foreground object accessed when the first application is running in the background.
[0180] It can be understood that the execution principle of 905 can refer to the execution principle of the method shown in the previous Figure 8 shown method, and will not be elaborated here.
[0181] 906: Output the recording result.
[0182] It can be understood that the recording result includes at least one creation thread and at least one target creation thread.
[0183] The embodiments of the present application do not limit the timing of the electronic device to output the recording result. For example, the electronic device may output the recording result when detecting that the remaining memory is insufficient, so as to determine the first object based on the recording result. For another example, the electronic device may also output the recording result after determining that the first application program switches to the foreground running state. In this case, the recording result can be used to determine the first object when the first application program switches to the background running state next time.
[0184] It can be understood that the method described above Figures 4 to 9 shows the way for the electronic device to determine the first foreground object only based on creating threads. In some other embodiments, the electronic device may further determine the first foreground object based on the access threads on the basis of determining the first foreground object based on creating threads. Specifically, as Figure 10 shown, the determination method of the first foreground object in this case includes the following process.
[0185] 1001: Obtain at least one access thread corresponding to at least one first created object, and the at least one access thread is used to access the at least one first created object.
[0186] The embodiments of the present application do not limit the way for the electronic device to obtain at least one access thread corresponding to at least one first created object. Exemplarily, the electronic device may change the data structure of each application object, for example, add an access thread data bit to the data structure of the application object, and the access thread data bit can be used to store (or record) the access thread of the application object. In this way, the electronic device can determine the access thread of each first created object according to the data corresponding to the access thread data bit in each first created object, and then obtain at least one access thread corresponding to at least one first created object.
[0187] Specifically, as Figure 11 shown, the determination method of at least one access thread in the present application may include the following process.
[0188] 1011: Create multiple application objects.
[0189] In the embodiments of the present application, when the electronic device creates an application object, it may change the data structure of the created application object, for example, add an access thread data bit to the data structure of the application object to store (or record) the access thread of the foreground object.
[0190] Figure 12 shows a schematic structural diagram of an application object before and after adding an access thread data bit. Among them, Figure 12 the (1) in Figure 6 is the same as the data structure of the application object shown by the (1) in the foregoing
[0191] The structure of the foreground object after adding the access thread data bit can be as shown in Figure 12 (2) in. It can be understood that the access thread data bit can be added to the object header part of the application object, and in the structures of the application object before and after adding the access thread data bit, the size of the data part remains unchanged, so that the size of the data structure of the application object after adding the access thread data bit is larger than the data structure of the application object when the access thread data bit is not added.
[0192] It can be understood that taking an electronic device using Android TM system as an example, if the electronic device wants to change the data structure of the application object, it can modify the creation logic of the application object in the Android runtime (ART), such as adding an access thread data bit when the application object is created to record the access thread that has accessed the foreground object.
[0193] In some embodiments, the electronic device can modify the creation logic of the application object in the Android runtime based on a switch interpreter. For example, the electronic device modifies the fetch instructions (such as sget instructions, aget instructions, etc.) based on the switch interpreter so that the electronic device can obtain the access thread corresponding to the application object based on the modified instructions.
[0194] 1012: Allocate memory space for each application object.
[0195] It can be understood that the execution principle of 1012 is the same as that of 4012 described above, so it will not be elaborated here.
[0196] 1013: Obtain the access threads of the first created objects that are accessed among multiple application objects, and obtain at least one access thread corresponding to at least one first created object.
[0197] It can be understood that since each application object created by the electronic device has an access thread data bit for storing the access thread of the application object in its data structure, the electronic device can determine the access thread of the first created object that is accessed based on the data corresponding to the access thread data bit, and then obtain at least one access thread corresponding to at least one first created object.
[0198] In the above manner, the electronic device can record the access thread of the application object based on the data structure of the application object when the application object is accessed. Then, when the electronic device determines that the remaining memory meets the memory cleaning condition and starts to determine the first foreground object, it can directly obtain at least one access thread corresponding to at least one first created object that is recorded.
[0199] In some embodiments, such as Figure 13As shown, the way for the electronic device to record the access thread of the accessed application object may include the following process.
[0200] 1301: Determine the accessed application object.
[0201] It can be understood that the accessed application object may belong to at least one first created object or may not belong to at least one first created object. Therefore, the electronic device also needs to execute the subsequent 1302, that is, determine whether the accessed application object belongs to at least one first created object.
[0202] 1302: Determine whether the accessed application object is accessed for the first time.
[0203] It can be understood that the purpose of accessing the application object is to execute relevant instructions or tasks. Therefore, after the electronic device determines the accessed application object, it can start to determine whether the accessed application object is accessed for the first time during the execution of relevant instructions or tasks or after the execution of relevant instructions or tasks.
[0204] It can be understood that if the electronic device determines that the accessed application is accessed for the first time, it executes 1303 to record the access thread of the accessed application object. If the electronic device determines that the accessed application object is not accessed for the first time, it executes 1304 and does not record the access thread of the application object.
[0205] 1303: Record the access thread of the accessed application object.
[0206] Exemplarily, the electronic device can determine the access thread corresponding to the application object according to the data corresponding to the access thread data bit in the data structure of the accessed application object. Among them, the recording method of the access thread of the application object has been described in the corresponding text content above Figure 11 and will not be elaborated here.
[0207] In this application, after the electronic device records the access thread of the accessed application object, it can continue to execute the relevant instructions or tasks involved in 1302 above.
[0208] 1304: Do not record the access thread of the accessed application object.
[0209] In this application, after the electronic device determines not to record the access thread of the accessed application object, it can continue to execute the relevant instructions or tasks involved in 1302 above.
[0210] It can be understood that after the electronic device records the access thread of the accessed application object, when it determines that the remaining memory meets the memory cleaning condition and starts to determine the first foreground object, it can directly obtain the access thread of the accessed first created object from the access thread of the accessed application object.
[0211] 1002: Determine the third quantity of the first created objects accessed by the first access thread among at least one access thread.
[0212] After the electronic device determines at least one access thread corresponding to at least one first created object, it can determine the quantity of the first created objects accessed by each first access thread. Taking the first access thread among at least one access thread as an example, the electronic device can determine the third quantity of the first created objects accessed by the first access thread.
[0213] 1003: If the proportion of the third quantity occupying the second quantity is greater than the second proportion threshold, regard the foreground objects accessed by the first access thread as the first foreground objects belonging to the first category.
[0214] It can be understood that if the proportion of the third quantity occupying the second quantity is greater than the second proportion threshold, it indicates that the probability of the foreground objects accessed by the first access thread being accessed when the first application is running in the background is relatively high. Therefore, the foreground objects accessed by the first access thread can be regarded as the first foreground objects.
[0215] In the embodiments of the present application, the value of the second proportion threshold is not limited. It can be set according to experience or flexibly adjusted according to the actual application scenario.
[0216] It can be understood, Figure 10 In the method for determining the first foreground objects shown, in addition to regarding at least one first created object created by the first creation thread as the first foreground objects, the electronic device also regards the foreground objects accessed by the first access thread as the first foreground objects. Then, the electronic device can transfer and store the above-mentioned first foreground objects to the first target area. In this case, the objects other than the first foreground objects among the foreground objects will be regarded as the second objects and stored in the second target area.
[0217] In this way, the probability of the second objects in the second target area being accessed is further reduced, and the probability of the compressed second target area being decompressed again due to the access of the second objects can be further reduced. Therefore, the response speed of the electronic device can be improved and the usage performance of the electronic device can be improved.
[0218] It can be understood that since the above-mentioned method 2 describes the determination methods of the first object and the second object when the first application is running in the background, the first object includes the background objects and some foreground objects, and the second object includes the remaining foreground objects. However, it should be understood that when the first application is running in the foreground, the first object can include the foreground objects and some background objects, and the second object includes the remaining background objects. Whether the first application is running in the foreground or in the background, the determination principles of the first object and the second object are similar and will not be elaborated one by one.
[0219] Method 3: The electronic device obtains a set of object types including multiple object types, and uses the application objects among the multiple application objects whose object types belong to the set of object types as the first objects; and uses the objects other than the first objects among the multiple application objects as the second objects.
[0220] It can be understood that the electronic device can first determine the set of object types, and then, after determining that the remaining memory meets the memory cleaning condition, obtain the already determined set of object types. Among them, the electronic device can first obtain at least one initial object type of at least one accessed application object, and then determine the object types that can be stored in the set of object types from the at least one initial object type.
[0221] In some embodiments, as Figure 14 shown, the method for determining the initial object type includes the following process.
[0222] 1401: Determine the accessed application object.
[0223] 1402: Determine whether the accessed application object is accessed for the first time.
[0224] It can be understood that the purpose of accessing the application object is to execute relevant instructions or tasks. Therefore, after the electronic device determines the accessed application object, it can start to determine whether the accessed application object is accessed for the first time during the execution of relevant instructions or tasks, or after the execution of relevant instructions or tasks.
[0225] It can be understood that if the electronic device determines that the accessed application object is accessed for the first time, execute 1403 to record the object type of the accessed application object; if the electronic device determines that the accessed application object is not accessed for the first time, execute 1404 and do not record the object type of the accessed application object.
[0226] 1403: Record the object type of the accessed application object and use it as the initial object type.
[0227] In this application, after the electronic device records the initial object type of the accessed application object, it can continue to execute the relevant instructions or tasks involved in 1402 above.
[0228] 1404: Do not record the object type of the accessed application object.
[0229] In this application, after the electronic device determines not to record the object type of the accessed application object, it can continue to execute the relevant instructions or tasks involved in 1402 above.
[0230] The electronic device is based on Figure 14After obtaining at least one initial object type of at least one application object by the method shown, the target number of application objects accessed among the first initial application objects corresponding to the first initial object type in the at least one initial object type can be determined. Then, when the proportion of the target number in the total number of the first initial application objects is greater than the third proportion threshold, the first initial object type is stored in the object type set.
[0231] In this way, the electronic device can determine a first object and a second object from multiple application objects based on a pre-determined object type set. The determination speed of the first object and the second object is relatively fast and the accuracy is relatively high. The probability that the second object in the second target area is accessed is reduced, and the probability that the compressed second target area is decompressed again due to the access of the second object can be reduced. Therefore, the response speed of the electronic device can be improved and the usage performance of the electronic device can be improved.
[0232] It can be understood that the above Figures 2 to 14 only takes the determination timing of the first object and the second object and the execution timing of the compression operation on the second target area as being the same, that is, both are when it is detected that the remaining memory meets the memory cleaning condition, as an example to describe the memory cleaning process. In some other embodiments, the determination timing of the first object and the second object and the execution timing of the compression operation on the second target area may also be different. Such a memory management method is as follows Figure 15 shown.
[0233] 1501: It is detected that the first application program of the electronic device is in a startup state.
[0234] It can be understood that the execution principle of 1501 is the same as that of 201 described above, and will not be elaborated here.
[0235] 1502: A first instruction is detected, or it is detected that the time interval between the current moment and the first moment is greater than or equal to a time threshold.
[0236] Wherein, the first instruction is an instruction for instructing the electronic device to determine a first object and a second object among multiple application objects, and the first moment is the starting moment when the first application program is in a startup state.
[0237] The embodiments of the present application do not limit the value of the time threshold, which can be set according to experience or flexibly adjusted according to the actual application scenario.
[0238] 1503: Determine a first object belonging to the first category among multiple application objects, and transfer and store the first object from the first initial area to the first target area.
[0239] 1504: Determine the second objects belonging to the second category among multiple application objects, and transfer and store the second objects from the second initial area to the second target area.
[0240] It can be understood that the execution principles of the above 1503-1504 are the same as those of the corresponding processes in the foregoing 203-204, and will not be elaborated here.
[0241] 1505: Detect that the remaining memory of the electronic device meets the memory cleaning condition.
[0242] It can be understood that the execution principle of this 1505 is the same as that of the foregoing 202, and will not be elaborated here.
[0243] 1506: Perform a compression operation on the second target area, where the probability of the first object of the first category being accessed is greater than the probability of the second object of the second category being accessed.
[0244] It can be understood that the execution principle of this 1506 is the same as that of the foregoing 205, and will not be elaborated here.
[0245] It can be understood that in this method, since there may be a time interval between the determination times of the first object and the second object and the determination time when the remaining memory meets the memory cleaning condition, in some embodiments, the electronic device may also use the foreground objects created by the first creation thread in the foregoing 403 during this time interval, or the foreground objects accessed by the first access thread in 1003 during this time interval, as the first object, and transfer and store them in the first target area.
[0246] In Figure 1 Based on the storage method of the virtual machine and application objects in the memory shown, Figure 16 a schematic diagram of the situation after the transfer and storage of application objects is shown. As Figure 16 shown, taking object A, object B, and object D as the first objects, and object C, object E, and object F as the second objects as an example, after the virtual machine determines the first object and the second object, it can store the virtual addresses of object A, object B, and object D in the first target virtual area, and store the virtual addresses of object C, object E, and object F in the second target virtual area.
[0247] Then, the virtual machine can send a message to the kernel to notify the kernel to transfer and store the determined first object and second object, and the kernel can then transfer and store the first object and the second object to different areas in the memory. For example, store object A, object B, and object D in the first target area, and store object C, object E, and object F in the second target area.
[0248] During the memory cleaning process (e.g., under the GC mechanism), when the electronic device determines that the remaining memory meets the memory cleaning condition, it can compress the second target area to release memory. It can be understood that when this method is applied in the GC mechanism, the second target area can be preferentially compressed during memory recycling, which can optimize the GC mechanism (or algorithm), that is, optimize the memory recycling process.
[0249] In some other embodiments, as Figure 17 shown, when the remaining memory is insufficient, the kernel can also control to re-store at least some of the second objects in the second target area to the swap area again. In this way, the memory space can also be released. For example, it can Figure 17 transfer object E and object F in
[0250] Figure 18 shows a schematic flowchart of another memory management method provided by an embodiment of the present application. As Figure 18 shown, the method includes the following processes.
[0251] 1801: Add identifiers to multiple application objects corresponding to the first application.
[0252] In some embodiments, taking the number of the first target area for storing the first object and the second target area for storing the second object as one for example, the electronic device can randomly set the first target area and the second target area in advance. As Figure 19 shown, taking the electronic device pre-setting two memory pages in the memory as the first target area and the second target area respectively as an example, at this time, the first target area initially stores object A, object B, and object C, and the second target area initially stores object D, object E, and object F.
[0253] Next, the electronic device can determine the first object belonging to the first category and the second object belonging to the second category among the multiple application objects, and then add a first identifier (such as a hot identifier) to the first object and a second identifier (such as a cold identifier) to the second object.
[0254] Continuing as Figure 19 shown, if the electronic device determines that object A, object B, and object D are the first objects, it can add the first identifier to object A, object B, and object D. If the electronic device determines that object C, object E, and object F are the second objects, it can add the second identifier to object C, object E, and object F.
[0255] 1802: Determine whether the identifier of the first application object among the multiple application objects matches the current area.
[0256] It can be understood that after the electronic device adds corresponding identifiers to each application object, it can determine whether each application object matches the current area where it is located.
[0257] If the electronic device determines that the identifier of the first application object matches the area where it is located, it executes the subsequent 1803; if the electronic device determines that the identifier of the first application object does not match the area where it is located, it executes the subsequent 1804.
[0258] Taking the first application object as object A as an example, based on the first identifier that object A has, the electronic device can determine that object A is the first object, that is, a hot object. Moreover, the first target area where object A is currently located is the area for storing the first object. Therefore, the electronic device can determine that the identifier of object A matches the area where it is located.
[0259] Taking the first application object as object C as an example, based on the second identifier that object C has, the electronic device can determine that object C is the second object, that is, a cold object. However, the first target area where object C is currently located is the area for storing the first object (i.e., the hot object). Therefore, the electronic device can determine that the identifier of object C does not match the area where it is located.
[0260] 1803: Continue to store the first application object in the current area.
[0261] Taking the first application object as object A as an example, after the electronic device determines that the identifier of object A matches the area where it is located, it can continue to store object A in the first target area.
[0262] 1804: Transfer and store the first application object to an area different from the current area where it is located.
[0263] Taking the first application object as object C as an example, after the electronic device determines that the identifier of object C does not match the area where it is located, it can transfer and store object C to the second target area.
[0264] It can be understood that after the electronic device executes 1803 and 1804, the application objects in the first target area are all the first objects with the first identifier, and the application objects in the second target area are all the second objects with the second identifier.
[0265] Such as Figure 19 As shown, object A, object B, and object D are stored in the first target area; object C, object E, and object F are stored in the second target area.
[0266] The above 1803 and 1804 can also be understood as the process of storing the first application object in the area where the first application object should be located.
[0267] 1805: After each first application object among multiple application objects is stored in the corresponding area, if it is determined that the remaining memory of the electronic device meets the memory cleaning condition, compress the second target area.
[0268] In the case where the remaining memory of the electronic device is insufficient, the second target area can be compressed. For example, release the memory of object C, object E, and object F.
[0269] 1806: Move at least some of the second objects in the second target area to the swap area.
[0270] It can be understood that this process is optional. After the electronic device compresses the second target area, it can also choose whether to move at least some of the second objects in the already compressed second target area to the swap area (for example, move object E and object F to the swap area), so as to further free up memory space.
[0271] In the method provided by this application, the electronic device transfers and stores the first object and the second object in different target areas (i.e., the first target area and the second target area), and preferentially compresses the second target area corresponding to the second object. Since the probability of the second object in the second target area being accessed is relatively small, this method can reduce the probability of the compressed second target area being decompressed due to the access of the second object, improve the response speed of the electronic device, and thus improve the usage performance of the electronic device.
[0272] In addition, in this application, the first foreground object in the first object is distinguished based on threads. This way can make the determination of the first object lighter, reduce the determination time of the first object, and thus improve the efficiency of memory cleaning.
[0273] In addition, in some other embodiments, the first object and the second object are distinguished based on the type of application object. This way can also make the determination of the first object and the second object lighter, reduce the determination time of the first object and the second object, and thus improve the efficiency of memory cleaning.
[0274] In some embodiments, this application embodiment also provides a storage medium, on which instructions are stored. When the instructions are executed on an electronic device, the electronic device is caused to execute the memory management method described in the above embodiments.
[0275] In some embodiments, this application embodiment also provides a program product, including: instructions, when the instructions run on an electronic device, the electronic device is caused to implement the memory management method described in the above embodiments.
[0276] In some embodiments, the embodiments of the present application further provide an electronic device, which includes: one or more processors; one or more memories; one or more programs are stored in one or more memories, and when the one or more programs are executed by the one or more processors, the electronic device executes the memory management method described in the above embodiments.
[0277] Figure 20 The structural schematic diagram of the electronic device provided by the embodiments of the present application is shown. As Figure 20 shown, the electronic device 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 jack 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, a barometric pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.
[0278] It can be understood that the structure schematically shown in the embodiments of the present application does not constitute a specific limitation on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than those shown, or combine certain components, or split certain components, or have different component arrangements. The components shown may be implemented in hardware, software, or a combination of software and hardware.
[0279] The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Among them, different processing units may be independent devices or integrated in one or more processors.
[0280] The controller may generate operation control signals according to the instruction operation code and timing signals to complete the control of fetching and executing instructions.
[0281] A memory may also be provided in the processor 110 for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory may save the instructions or data that the processor 110 has just used or recycled. If the processor 110 needs to use the instruction or data again, it can be directly called from the above-mentioned memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the system. The processor can be used to execute the memory management method mentioned in this application.
[0282] The external memory interface 120 can be used to connect to an external memory card, such as a Micro SD card, to implement the storage capacity expansion of the electronic device. The external memory card communicates with the processor 110 through the external memory interface 120 to achieve the data storage function. For example, files such as music and videos are saved in the external memory card.
[0283] The internal memory 121 can be used to store computer-executable program code, which includes instructions. The internal memory 121 can include a program storage area and a data storage area. Among them, the program storage area can store an operating system, application programs required for at least one function (such as a sound playback function, an image playback function, etc.). The data storage area can store data created during the use of the electronic device (such as audio data, a phone book, etc.). In addition, the internal memory 121 can include high-speed random access memory and can also include non-volatile memory, such as at least one magnetic 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 by running the instructions stored in the internal memory 121 and / or the instructions stored in the memory provided in the processor.
[0284] The SIM card interface 195 is used to connect a SIM card.
[0285] It can be understood that, as used herein, the term "module" can refer to or include an application-specific integrated circuit (ASIC), an electronic circuit, a processor (shared, dedicated, or grouped) that executes one or more software or firmware programs, and / or a memory, combinational logic circuits, and / or other suitable hardware components that provide the described functions, or can be a part of these hardware components.
[0286] It can be understood that, in the embodiments of the present application, the processor can be a microprocessor, a digital signal processor, a microcontroller, etc., and / or any combination thereof. According to another aspect, the processor can be a single-core processor, a multi-core processor, etc., and / or any combination thereof.
[0287] The embodiments disclosed in the present application can be implemented in hardware, software, firmware, or a combination of these implementation methods. The embodiments of the present application can be implemented as a computer program or program code executed on a programmable system, which includes at least one processor, a storage system (including volatile and non-volatile memory and / or storage elements), at least one input device, and at least one output device.
[0288] The program code can be applied to the input instructions to execute the various functions described in the present application and generate output information. The output information can be applied to one or more output devices in a known manner. For the purposes of the present application, the processing system includes any system having a processor such as a digital signal processing (DSP), a microcontroller, an application-specific integrated circuit, or a microprocessor.
[0289] The program code can be implemented in a high-level procedural language or an object-oriented programming language to communicate with the processing system. When necessary, the program code can also be implemented in assembly language or machine language. In fact, the mechanisms described in this application are not limited to the scope of any specific programming language. In any case, the language can be a compiled language or an interpreted language.
[0290] In some cases, the disclosed embodiments can be implemented in hardware, firmware, software, or any combination thereof. The disclosed embodiments can also be implemented as instructions carried or stored on one or more transitory or non-transitory machine-readable (e.g., computer-readable) storage media, which can be read and executed by one or more processors. For example, the instructions can be distributed via a network or via other computer-readable media. Thus, machine-readable media can include any mechanism for storing or transmitting information in a form readable by a machine (e.g., a computer), including but not limited to, floppy disks, optical disks, optical discs, CD-read-only memories (CD-ROMs), magneto-optical disks, read-only memory (ROM), random access memory (RAM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), magnetic or optical cards, flash memory, or tangible machine-readable memories for transmitting information (e.g., carrier waves, infrared signals, digital signals, etc.) in electrical, optical, acoustic, or other forms via the Internet. Thus, machine-readable media include any type of machine-readable media suitable for storing or transmitting electronic instructions or information in a form readable by a machine (e.g., a computer).
[0291] In the drawings, some structural or method features may be shown in a particular arrangement and / or order. However, it should be understood that such a particular arrangement and / or ordering may not be required. Instead, in some embodiments, these features may be arranged in a different manner and / or order than shown in the illustrative drawings. Additionally, the inclusion of a structural or method feature in a particular figure does not imply that such a feature is required in all embodiments, and in some embodiments, these features may not be included or may be combined with other features.
[0292] It should be noted that each unit / module mentioned in the device embodiments of the present application is a logical unit / module. Physically, a logical unit / module can be a physical unit / module, a part of a physical unit / module, or can be implemented as a combination of multiple physical units / module. The physical implementation manner of these logical units / modules themselves is not the most important. The combination of the functions implemented by these logical units / modules is the key to solving the technical problems proposed in the present application. In addition, in order to highlight the innovative part of the present application, the above device embodiments of the present application do not introduce units / modules that are not closely related to solving the technical problems proposed in the present application, which does not mean that there are no other units / modules in the above device embodiments.
[0293] It should be noted that in the examples and descriptions of the present application, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprising", "including" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the phrase "comprising one" does not exclude the presence of additional identical elements in the process, method, article or device comprising the element.
[0294] Although the present application has been illustrated and described by referring to some preferred embodiments of the present application, those of ordinary skill in the art should understand that various changes can be made in form and detail without departing from the scope of the present application.
Claims
1. A memory management method, characterized in that, Applied to an electronic device, the method includes: Detect that a first application of the electronic device is running in the background. Among them, the first application corresponds to multiple application objects, and the multiple application objects include multiple foreground objects and multiple background objects. The multiple foreground objects are objects created when the first application runs in the foreground, and the multiple background objects are objects created when the first application runs in the background; Determine a first object belonging to a first category among the multiple application objects, and transfer and store the first object from a first initial area to a first target area; Determine a second object belonging to a second category among the multiple application objects, and transfer and store the second object from a second initial area to a second target area; Perform a compression operation on the second target area, where the probability of the first object belonging to the first category being accessed is greater than the probability of the second object belonging to the second category being accessed; The determining the first object belonging to the first category among the multiple application objects includes: Determine a first foreground object belonging to the first category among the multiple foreground objects; Take the multiple background objects and the first foreground object as the first object belonging to the first category; The determining the second object belonging to the second category among the multiple application objects includes: Take the objects among the multiple foreground objects other than the first foreground object as the second object belonging to the second category.
2. The method according to claim 1, characterized in that, Before determining the first object belonging to the first category among the multiple application objects, the method further includes: Detect that the remaining memory of the electronic device meets the memory cleaning condition.
3. The method according to claim 1, wherein Before determining the first object belonging to the first category among the multiple application objects, the method further includes: Detect a first instruction, or detect that the time interval between the current moment and a first moment is greater than or equal to a time threshold, where the first moment is the starting moment when the first application is in the startup state; Before performing the compression operation on the second target area, the method further includes: Detect that the remaining memory of the electronic device meets the memory cleaning condition.
4. The method according to any one of claims 1 to 3, wherein The determining the first object belonging to the first category among the multiple application objects includes: Take the multiple background objects as the first object belonging to the first category; The determining the second object belonging to the second category among the multiple application objects includes: Take the multiple foreground objects as the second object belonging to the second category.
5. The method according to any one of claims 1 to 3, characterized in that The determining the first foreground object belonging to the first category among the multiple foreground objects includes: Obtain at least one creation thread corresponding to the first application, and the at least one creation thread is used to create the multiple foreground objects; Determine at least one first creation object created by a first creation thread among the at least one creation thread, and the at least one first creation object belongs to the multiple foreground objects; Among the at least one first created object, if the ratio of the first quantity of the accessed objects to the second quantity of the at least one first created object is greater than a first ratio threshold, the at least one first created object is taken as the first foreground object belonging to the first category.
6. The method according to claim 5, wherein The determining the first foreground objects belonging to the first category among the multiple foreground objects further includes: Obtaining at least one access thread corresponding to the at least one first created object, where the at least one access thread is used to access the at least one first created object; Determining a third quantity of the first created objects accessed by a first access thread among the at least one access thread; Among the foreground objects accessed by the first access thread, if the ratio of the third quantity to the second quantity is greater than a second ratio threshold, the foreground objects accessed by the first access thread are taken as the first foreground objects belonging to the first category.
7. The method according to claim 6, wherein The object header of each application object among the multiple application objects includes a creation thread data bit and an access thread data bit; The obtaining at least one creation thread corresponding to the first application program includes: Based on the data corresponding to the creation thread data bits in each application object, determining the creation thread of each application object, and obtaining the at least one creation thread for creating the multiple application objects; The obtaining at least one access thread corresponding to the at least one first created object includes: Based on the data corresponding to the access thread data bits of each first created object among the at least one first created object, determining the access thread of each first created object, and obtaining the at least one access thread for accessing the at least one first created object.
8. The method according to any one of claims 1 to 3, characterized in that The determining the first objects belonging to the first category among the multiple application objects includes: Obtaining an object type set including multiple object types; Taking the application objects among the multiple application objects whose object types belong to the object type set as the first objects belonging to the first category; The determining the second objects belonging to the second category among the multiple application objects includes: Taking the objects among the multiple application objects other than the first objects as the second objects belonging to the second category.
9. An electronic device, characterized in that, including: One or more processors; One or more memories; the one or more memories store one or more programs, and when the one or more programs are executed by the one or more processors, the electronic device executes the memory management method according to any one of claims 1 to 8.
10. A storage medium, characterized in that, Instructions are stored on the storage medium, and when the instructions are executed on the electronic device, the electronic device executes the memory management method according to any one of claims 1 to 8.
11. A program product, characterized in that, including: instructions, when the instructions run on the electronic device, the electronic device executes the memory management method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Method for controlling memory and electronic equipment
CN116820734A
Physical page cold and hot identification method and device, chip and storage medium
CN117762830A