Garbage recycling method, electronic device and storage medium

By copying memory objects to different partitions during the garbage collection process, the problem of low efficiency of memory objects after garbage collection is solved, the locality of memory objects is improved and the performance of electronic devices is improved.

CN119248463BActive Publication Date: 2025-09-02HONOR DEVICE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410175913.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-02-07
Publication Date
2025-09-02
Estimated Expiration
2044-02-07

AI Technical Summary

Technical Problem

At this stage, how to improve the efficiency of memory objects used by electronic devices after performing garbage collection.

Method used

During the garbage collection process, the memory objects are copied to different partitions according to the frequency of the memory objects. Memory objects with high frequency are copied to the first subpartition with continuous addresses, and memory objects with low frequency are copied to the second subpartition, and the locality principle of memory objects is used to improve usage efficiency.

Benefits of technology

It improves the efficiency of memory objects used by electronic devices after performing garbage collection and processing, and improves the performance of electronic devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119248463B_ABST
    Figure CN119248463B_ABST
Patent Text Reader

Abstract

The embodiments of the present application relate to the field of terminal technology, and in particular to a garbage collection method, electronic device and storage medium. In the above method, after the target application is started, if the memory size occupied by the target application is greater than or equal to the memory threshold, the electronic device performs GC processing on the memory object corresponding to the target application. Wherein, when the frequency of the memory object is higher than or equal to the degree threshold, the memory object is copied to the first sub-partition; when the frequency of the memory object is lower than the degree threshold, the memory object is copied to the second sub-partition. Through the above method, the memory objects with high frequency of use by the target application can be concentrated on the first sub-partition, thereby improving the locality of the memory objects, thereby improving the efficiency of the electronic device in using the memory objects and improving the performance of the electronic device.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of terminal technology, and in particular to a garbage collection method, electronic device, and storage medium. Background Art

[0002] Garbage collection (GC) is a memory management mechanism that frees up the memory space occupied by memory objects by deleting them when they are no longer needed. This storage resource management mechanism is called garbage collection.

[0003] Currently, the memory space of an electronic device can be divided into a first partition and a second partition. Memory objects are stored in the second partition. During the GC process, the electronic device copies memory objects that meet preset conditions from the second partition to the first partition and empties the second partition. In this way, memory objects that do not meet the preset conditions are deleted, that is, garbage memory objects are deleted.

[0004] At present, how to improve the efficiency of electronic devices in using memory objects after performing GC processing is a problem to be solved. Summary of the Invention

[0005] In view of this, embodiments of the present application provide a garbage collection method, electronic device, and storage medium. In this method, memory objects with similar frequency of use by the electronic device are placed in close proximity on the first partition after GC processing, thereby improving the locality of the memory objects. This can improve the operating efficiency and performance of the electronic device.

[0006] To achieve the above objectives, the present invention adopts the following technical solutions:

[0007] In a first aspect, an embodiment of the present application provides a garbage collection method, which can be applied to electronic devices such as mobile phones and tablets. The memory space of the electronic device includes a first partition and a second partition, the first partition includes a first sub-partition and a second sub-partition, the memory addresses of the first sub-partition are continuous, the first partition and the second partition are different, and the first sub-partition is different from the second sub-partition. The method includes: after a target application is started, if the memory size occupied by the target application is greater than or equal to a memory threshold, the electronic device performs garbage collection (GC) processing on the memory object corresponding to the target application. The electronic device performs GC processing on the memory object corresponding to the target application, including: if the memory object meets a preset condition, the electronic device copies the memory object from the second partition to the first partition. Wherein, if the frequency of the memory object is greater than or equal to the frequency threshold, the memory object is copied to the first sub-partition; if the frequency of the memory object is less than the frequency threshold, the memory object is copied to the second sub-partition. If the memory object does not meet the preset condition, the electronic device deletes the memory object from the second partition. Furthermore, the memory object meeting the preset condition can be understood as the memory object passing the reachability test, and the memory object not meeting the preset condition can be understood as the memory object failing the reachability test.

[0008] In the above method, memory objects with different frequencies are copied to different partitions. For example, memory objects with a frequency higher than the threshold are copied to the first sub-partition, and memory objects with a frequency lower than the threshold are copied to the second sub-partition. In other words, memory objects with a frequency higher than the threshold are copied to the first sub-partition. At the same time, since the memory addresses of the first sub-partition are continuous, the positions of the memory objects on the first sub-partition are close. It can be seen that the above method can concentrate memory objects with high frequency used by the target application on the first sub-partition, thereby improving the locality of memory objects, thereby improving the efficiency of electronic devices in using memory objects and improving the performance of electronic devices.

[0009] In a possible design of the first aspect, the above-mentioned memory objects include new generation memory objects or old generation memory objects; old generation memory objects are memory objects that have undergone K rounds of GC processing, where K is a positive integer; new generation memory objects are memory objects other than old generation memory objects. That is, the above-mentioned memory objects can be divided into new generation memory objects or old generation memory objects according to the survival time of the memory objects. The above-mentioned electronic device copies the memory object from the second partition to the first partition when the memory object meets the preset conditions, including: when the new generation memory object meets the preset conditions, the electronic device copies the new generation memory object from the second partition to the first partition. And, the above-mentioned electronic device deletes the memory object from the second partition when the memory object does not meet the preset conditions, including: when the new generation memory object does not meet the preset conditions, the electronic device deletes the new generation memory object from the second partition. And, the above-mentioned execution of garbage collection GC processing on the memory object corresponding to the target application also includes: the electronic device copies the old generation memory object from the second partition to the first sub-partition.

[0010] This design considers that determining whether a memory object meets the preset conditions also consumes electronic device resources. Therefore, for old generation memory objects, the electronic device can skip the check and directly copy the old generation memory to the first subpartition. This saves resources consumed by the electronic device during the check and further improves the efficiency of the electronic device during GC processing.

[0011] In another possible design of the first aspect, the proportion of the first sub-partition in the first partition is positively correlated with the number of old generation memory objects.

[0012] In this design, the proportion of the first subpartition in the first partition is positively correlated with the number of old generation memory objects. That is, the greater the number of old generation memory objects, the greater the proportion of the first subpartition in the first partition. The fewer the number of old generation memory objects, the smaller the proportion of the first subpartition in the first partition. This can alleviate the problem of wasted first subpartition space due to an overly large first subpartition, as well as the problem of insufficient first subpartition space due to an undersized first subpartition.

[0013] In another possible design of the first aspect, before the electronic device performs GC processing on the memory object corresponding to the target application, the above method further includes: the electronic device obtains hot spot information of the memory object, and the hot spot information is used to indicate the frequency of the target application using the memory object. And, in the case where the frequency of the memory object is higher than or equal to the degree threshold, the memory object is copied to the first sub-partition, including: in the case where the hot spot information indicates that the frequency of the target application using the memory object is higher than or equal to the degree threshold, the memory object is copied to the first sub-partition. And, in the case where the frequency of the memory object is lower than the degree threshold, the memory object is copied to the second sub-partition, including: in the case where the hot spot information indicates that the frequency of the memory object is lower than the degree threshold, the memory object is copied to the second sub-partition.

[0014] In this design, the electronic device can accurately obtain hotspot information indicating how frequently the target application uses the memory object through the hotspot information, and determine the subpartition to which the memory object is copied, such as the first subpartition or the second subpartition, based on the hotspot information.

[0015] In another possible design of the first aspect, the electronic device obtains the hotspot information of the memory object, including: the electronic device writes the hotspot information of the memory object into the file header of the memory object.

[0016] In this design, the electronic device can efficiently store the hotspot information of the memory object through the file header of the memory object.

[0017] In another possible design of the first aspect, the state of the memory object includes a lightly locked state, a heavily locked state, or a hash state. The electronic device writes the hotspot information of the memory object into the file header of the memory object, including: when the state of the memory object is a lightly locked state or a hash state, the electronic device writes the hotspot information into the file header of the memory object. And, the electronic device performs GC processing on the memory object corresponding to the target application, also including: if the state of the memory object is a heavily locked state and the memory object meets the preset conditions, the electronic device copies the memory object to the first sub-partition.

[0018] In this design, for memory objects in a relocked state, the relocked state itself indicates a high frequency of access. Therefore, relocked memory objects that meet preset conditions can be directly copied to the first subpartition. This saves resources on acquiring hotspot information for relocked memory objects, further improving the efficiency of the electronic device.

[0019] In another possible design of the first aspect, the state of the memory object is a lightly locked state, and the file header of the memory object in the lightly locked state includes a first field, and the first field is used to store information about the number of lightly locked times for the memory object. The electronic device writing hotspot information into the file header of the memory object includes: the electronic device writing the hotspot information into the first field.

[0020] In this design, the electronic device can conveniently store hotspot information in the memory object by writing the hotspot information into the first field.

[0021] In another possible design of the first aspect, the state of the memory object is a hash state, and the file header of the memory object in the hash state includes a second field, and the second field is used to store the hash value for the memory object. The electronic device writing the hotspot information into the file header of the memory object includes: the electronic device writing the hotspot information into the second field.

[0022] In this design, the electronic device can conveniently store hotspot information in the memory object by writing the hotspot information into the first field.

[0023] In another possible design of the first aspect, the electronic device obtains hotspot information of a memory object, including:

[0024] The electronic device obtains a read instruction for the memory object and obtains a write instruction for the memory object. Next, the electronic device obtains hotspot information based on the read instruction for the memory object and the write instruction for the memory object.

[0025] In this design, the electronic device can accurately determine the hotspot information of the electronic device through read instructions and write instructions for memory objects.

[0026] In another possible design of the first aspect, the electronic device obtains the hotspot information based on a read instruction and a write instruction for a memory object, including:

[0027] When the sum of the number of instruction fetches for a memory object and the number of write instructions for the memory object is greater than or equal to M times, the electronic device obtains hotspot information indicating that the target application uses the memory object with a frequency greater than or equal to a threshold value, where M is a positive integer. When the sum of the number of instruction fetches for a memory object and the number of write instructions for the memory object is less than M times, the electronic device obtains hotspot information indicating that the target application uses the memory object with a frequency less than the threshold value.

[0028] In another possible design of the first aspect, the first subpartition is located in the internal memory of the electronic device, and the second subpartition is located in the internal memory of the electronic device or in a disk of the electronic device. In this design, the electronic device can use virtual memory technology to save memory space in the electronic device by storing the second subpartition on the disk.

[0029] In a second aspect, an electronic device is provided, which includes a processor and a memory; the processor is coupled to the memory; the memory is used to store computer program code; the computer program code includes computer instructions, and when the processor executes the above-mentioned computer instructions, the electronic device executes the method provided by the above-mentioned first aspect and any possible design of the first aspect.

[0030] In a third aspect, a computer-readable storage medium is provided, which includes computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the method provided by the first aspect and any possible design of the first aspect.

[0031] In a fourth aspect, a computer program product is provided, which includes instructions. When the computer program product runs on an electronic device, the electronic device executes the method provided by the first aspect and any possible design of the first aspect.

[0032] Among them, the technical effects brought about by any design method in the second to fourth aspects can refer to the technical effects brought about by different design methods in the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 A schematic diagram of a usage scenario of an electronic device provided in an embodiment of the present application;

[0034] Figure 2 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application;

[0035] Figure 3 A schematic diagram of the software architecture of an electronic device provided in an embodiment of the present application;

[0036] Figure 4 A schematic diagram of the location of memory objects provided in an embodiment of the present application;

[0037] Figure 5 A flowchart of a garbage collection method provided in an embodiment of the present application;

[0038] Figure 6 A schematic diagram of the process of generating a hotspot flag by the interpreter provided in an embodiment of the present application;

[0039] Figure 7 A schematic diagram of the structure of a memory object provided in an embodiment of the present application;

[0040] Figure 8 A schematic diagram of the structure of the file header area of ​​a memory object provided in an embodiment of the present application;

[0041] Figure 9 A flowchart of another garbage collection method provided in an embodiment of the present application;

[0042] Figure 10 A flowchart of another garbage collection method provided in an embodiment of the present application;

[0043] Figure 11A Another schematic diagram of the location of memory objects provided in an embodiment of the present application;

[0044] Figure 11B Another schematic diagram of the location of memory objects provided in an embodiment of the present application. DETAILED DESCRIPTION

[0045] The technical solutions in the embodiments of the present application will be described below in conjunction with the drawings in the embodiments of the present application. Among them, in the description of the present application, unless otherwise specified, " / " indicates that the objects associated before and after are in an "or" relationship. For example, A / B can represent A or B; "and / or" in the present application is only a description of the association relationship of the associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. In addition, in the description of the embodiments of the present application, unless otherwise specified, "multiple" refers to two or more than two. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple. In addition, to facilitate the clear description of the technical solutions of the embodiments of the present application, in the embodiments of the present application, the words "first" and "second" are used to distinguish between identical or similar items with substantially the same functions and effects. Those skilled in the art will understand that the words "first" and "second" do not limit the quantity or execution order, and the words "first" and "second" do not necessarily mean different.

[0046] At the same time, in the embodiments of this application, words such as "exemplary" or "for example" are used to indicate examples, illustrations, or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of this application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Rather, the use of words such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner to facilitate understanding.

[0047] In the technical solutions disclosed in this application, the collection, storage, use, processing, transmission, provision and disclosure of user personal information involved comply with the provisions of relevant laws and regulations and do not violate public order and good morals.

[0048] Garbage collection (GC) can also be called memory garbage collection; GC can be understood as deleting some memory objects when they are no longer needed to release the memory space occupied by these memory objects. This storage resource management mechanism is called garbage collection.

[0049] In some solutions, electronic devices can perform garbage collection using concurrent copying garbage collection (CCGC). In this method, the electronic device's memory space is divided into a source (from) partition and a destination (to) partition. The electronic device determines whether to copy memory objects in the from partition to the to partition based on their reachability. After the memory objects in the from partition are copied, the memory objects in the from partition are cleared; this implements garbage collection.

[0050] At present, how to improve the efficiency of electronic devices in using memory objects after performing GC processing is a problem to be solved.

[0051] In view of this, an embodiment of the present application provides a garbage collection method, in which an electronic device obtains hotspot information of a memory object, and the hotspot information can represent the frequency with which the electronic device uses the memory object. Thereafter, during the concurrent copying GC process, the electronic device copies the memory object to a location in the to partition corresponding to the hotspot information based on the hotspot information of the memory object. For details about the concurrent copying GC process and the concept of the to partition, please refer to the following text and will not be elaborated here.

[0052] Through this method, memory objects with similar hotspot information—that is, memory objects used by electronic devices with similar frequency—are also located close together in the "to" partition. This improves the locality of memory objects during the concurrent copying GC process, thereby increasing the efficiency of memory object use by electronic devices and enhancing their performance.

[0053] Among them, locality, which can also be called the locality principle, can include spatial locality and temporal locality. For memory objects, spatial locality can be understood as: if a memory object is used by a processor, then memory objects near this memory object will likely be used by the processor in the future. Also, for memory objects, temporal locality can be understood as: if a memory object is used by a processor, then this memory object will likely be used by the processor in the future. Based on the above analysis of spatial locality and temporal locality, it can be seen that for two memory objects, when the locations of these two memory objects are relatively close, the higher the frequency of use of these two memory objects, the better the locality of the memory objects. Also, for two memory objects, when the frequency of use of these two memory objects is relatively high, the closer the locations of these two memory objects are, the better the locality of the memory objects. It can be seen that the more concentrated the locations of memory objects with high frequency of use, the better the locality of the memory objects.

[0054] It should be understood that the memory objects near the above-mentioned memory object can be understood as the memory addresses of these two memory objects being close.

[0055] At the same time, because the processor preloads other memory objects near a memory object when using it, improving the locality of memory objects can improve the efficiency of electronic devices in using memory objects and improve the performance of electronic devices.

[0056] For example, the processor is using memory object A, and then the processor needs to use memory object B. If the memory address of memory object B is close to that of memory object A, the processor will preload memory object B when using memory object A. Then, when the processor uses memory object B later, the processor's execution efficiency will be higher. If the memory address of memory object B is far away from that of memory object A, the processor cannot preload memory object B when using memory object A. Then, when the processor uses memory object B later, the processor will also need to load memory object B, and the processor's execution efficiency will be lower. This shows that the better the locality of memory objects, the more efficient the processor will be in using memory objects, and the better the performance of electronic devices.

[0057] For example, see Figure 1The technical solution provided in the embodiment of the present application can be applied to the process of a user running an application (APP) through the electronic device 100.

[0058] The electronic devices mentioned above may be mobile phones, tablet computers, smart screens, augmented reality (AR) / virtual reality (VR) devices, laptop computers, ultra-mobile personal computers (UMPCs), netbooks, personal digital assistants (PDAs), and other electronic devices with memory. They may also be wearable devices with memory, such as smart watches, smart bracelets, electronic watches, and sports bracelets.

[0059] Next, the hardware structure of the electronic device provided in the embodiments of the present application is introduced.

[0060] For example, see Figure 2 , Figure 2 A schematic structural diagram of the electronic device 100 is shown.

[0061] The electronic device 100 may include a processor 110 , an external memory interface 120 , an internal memory 121 , a universal serial bus (USB) interface 130 , and the like.

[0062] It should be understood that the structure illustrated in the embodiments of the present invention does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown, or may combine or separate certain components, or arrange the components differently. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.

[0063] 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 memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU). The different processing units may be independent devices or integrated into one or more processors.

[0064] The controller may be the nerve center and command center of the electronic device 100. The controller may generate an operation control signal according to the instruction operation code and the timing signal to complete the control of fetching and executing instructions.

[0065] Processor 110 may also include a memory for storing instructions and data. In some embodiments, the memory in processor 110 is a cache memory. This memory can store instructions or data that have just been used or are being recycled by processor 110. If processor 110 needs to use the same instruction or data again, it can directly access the memory. This avoids duplicate accesses, reduces processor 110 latency, and thus improves system efficiency.

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

[0067] The USB interface 130 is an interface that complies with USB standards and may be a Mini USB interface, a Micro USB interface, a USB Type-C interface, or the like. The USB interface 130 can be used to connect a charger to charge the electronic device 100, or to transfer data between the electronic device 100 and peripheral devices. It can also be used to connect headphones to play audio. This interface can also be used to connect other electronic devices, such as augmented reality devices.

[0068] It is understood that the interface connection relationship between the modules illustrated in the embodiment of the present invention is merely an illustrative illustration and does not constitute a structural limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may also adopt different interface connection methods from the above embodiments, or a combination of multiple interface connection methods.

[0069] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 via the external memory interface 120 to implement data storage functions. For example, files such as music and videos can be stored on the external memory card.

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

[0071] Next, the software architecture of the electronic device 100 is introduced.

[0072] The software system of the electronic device 100 can adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a micro-service architecture, or a cloud architecture. TM Taking the system as an example, the software structure of the electronic device 100 is exemplarily described.

[0073] Figure 3 Schematic diagram of the software architecture of the electronic device 100 according to an embodiment of the present invention. The layered architecture can divide the software into several (architecture) layers, each with a clear role and division of labor. The layers communicate with each other through software interfaces. In some embodiments, Android TM The system is divided into four layers, from top to bottom: application layer (can be referred to as application (APP) layer, application framework layer (framework, FWK)), native layer and kernel layer. For example, Figure 3As shown, the electronic device 100 may include an application layer, a framework layer, a local layer and a kernel layer.

[0074] It is understandable that Figure 3 The software architecture shown does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer architecture layers than shown in the figure, or combine some architecture layers, or split some architecture layers, etc. TM The system can also be divided into other layers, for example, into: application layer (can be referred to as application layer), application framework layer, system library and Android TM Runtime layer, kernel layer, etc. In addition, different operating systems can also have different layer division methods, and technicians can specifically divide the operating system into different layers according to actual usage.

[0075] The application layer can include a series of application packages. Figure 3 As shown, the application package may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, etc.

[0076] The application framework layer provides an application programming interface (API) and programming framework for applications in the application layer. The application framework layer includes some predefined functions. For example, the application layer may include system services and media services.

[0077] The system server is a process that provides many subsystem services. Each subsystem service runs as a thread, waiting for requests from applications, processing them, and returning the results to the applications. These subsystem services include, for example, the Window Manager Service (WMS), the Notification Manager Service (NMS), the Activity Manager Service (AMS), and the Input Manager Service (IMS).

[0078] The native layer, also known as the system runtime layer, can include multiple functional modules, such as the surface manager, media libraries, 3D graphics processing libraries (such as OpenGL ES), 2D graphics engines (such as SGL), core libraries, and so on.

[0079] The surface manager is used to manage the display subsystem and provides the fusion of 2D and 3D layers for multiple applications. The media library supports the playback and recording of a variety of common audio and video formats, as well as static image files. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc. The three-dimensional graphics processing library is used to implement three-dimensional graphics drawing, image rendering, synthesis, and layer processing. The two-dimensional (2D) graphics engine is a drawing engine for 2D drawing. The core library consists of two parts: one is the function that needs to be called by the Java language, and the other is the Android core library.

[0080] And, the local layer can also include Android TM Runtime (Android TM runtime, ART), ART is responsible for Android TM In some cases, ART can also be referred to as Android TM The runtime virtual machine, also known as the virtual machine, can include garbage collection threads and an interpreter. The interpreter interprets and executes Java files as binary files. The garbage collection thread performs garbage collection.

[0081] The kernel driver layer, also known as the core layer, is the layer between hardware and software. The kernel layer includes at least display drivers, camera drivers, audio drivers, and sensor drivers.

[0082] The following describes the garbage collection method provided in the embodiment of the present application in the software architecture of the electronic device 100 in combination with the process of the electronic device 100 running an APP.

[0083] For example, see Figure 4 , Figure 4 The figure shows the process of the CG thread performing concurrent copying GC on memory objects based on the hotspot information of the memory objects. The memory space available to the virtual machine, that is, the memory space available to the threads established by the virtual machine, can be divided into partition 1 and partition 2, and the memory objects are located in these partitions. In addition, partition 1 can be divided into partition 1 hot zone and partition 1 cold zone; partition 2 can be divided into partition 2 hot zone and partition 2 cold zone. For example, partition 1 includes memory object 301a, memory object 302a, memory object 303a, and memory object 304a.

[0084] Next, if the GC thread receives a GC indication, the GC thread performs a reachability analysis on the above-mentioned memory object 301a, memory object 302a, memory object 303a, and memory object 304a. If the memory object passes the reachability analysis, the memory object is copied to the second partition; if the memory object does not pass the reachability analysis, the memory object is not copied to the second partition. At the same time, when the GC thread copies the memory object to the second partition, if the hot spot information indicates that the electronic device uses the memory object more frequently, the memory object is copied to the hot zone of the second partition; and, when the GC thread copies the memory object to the second partition, if the hot spot information indicates that the electronic device does not use the memory object frequently, the memory object is copied to the cold zone of the second partition. For example, memory object 301a is copied to the hot zone of the second partition to obtain memory object 301b, and memory object 302a is copied to the hot zone of the second partition to obtain memory object 302b. For another example, memory object 304a is copied to the cold zone of the second partition to obtain memory object 304b.

[0085] It should be noted that the time period from when the GC thread obtains the GC indication to when the first partition is cleared is called the GC period. Furthermore, the non-GC period can be understood as the period of time during the virtual machine operation other than the GC period.

[0086] Also, in the GC period, the from partition and the to partition are relative concepts. For example, in the above Figure 4 In the GC period shown, the GC thread copies memory objects from partition 1 to partition 2. Therefore, in this GC period, the first partition can also be referred to as the from partition, and the second partition can also be referred to as the to partition. Furthermore, if the GC thread copies memory objects from partition 2 to partition 1 in the next GC period, then in this GC period, the second partition can also be referred to as the from partition, and the first partition can also be referred to as the to partition. In other embodiments, the to partition can also be referred to as the first partition, the from partition can also be referred to as the second partition, the hot area of ​​the to partition can also be referred to as the first sub-partition, and the cold area of ​​the to partition can also be referred to as the second sub-partition.

[0087] Since the memory objects in the "from" partition are selectively copied to the "to" partition in the above process, the memory objects copied from the "from" partition to the "to" partition can be considered the same memory object in both the "from" and "to" partitions. For example, memory objects 301a and 301b can be considered the same memory object. For another example, memory objects 304a and 304b can be considered the same memory object.

[0088] Among them, the above-mentioned electronic device uses the memory object more frequently, which can be understood as the frequency of the electronic device using the memory object is higher than or equal to the degree threshold, such as, within a certain time period (such as a non-GC time period), the number of times the electronic device uses the memory object is greater than or equal to the number threshold. And, the above-mentioned electronic device uses the memory object infrequently, which can be understood as the frequency of the electronic device using the memory object is lower than the degree threshold, such as, within a certain time period (such as a non-GC time period), the number of times the electronic device uses the memory object is less than the number threshold. The above-mentioned degree threshold can be 1 time / time period, 3 times / time period, 4 times / time period, etc. The above-mentioned number threshold can be 1 time, 3 times, 4 times, etc. Specifically, the degree threshold and the number threshold can be set according to actual usage needs, and the embodiments of the present application do not limit this.

[0089] Afterward, the GC thread deletes the memory objects in partition 1. After this process, the most frequently used memory objects are located in the hot zone of partition 2. This improves the locality of memory objects, thereby increasing the efficiency of memory object use and improving performance.

[0090] It should be pointed out that in Figure 4 In the corresponding process, for memory object 301a and memory object 301b, since the number of times the electronic device uses memory object 301a is greater than or equal to the number threshold, after the electronic device executes the concurrentcopying GC process, that is, after the electronic device executes the GC process on the memory object, memory object 301b is located in the hot zone of the second partition. And, Figure 4 In the corresponding process, for memory object 304a and memory object 304b, since the number of times the electronic device uses memory object 304a is less than the number threshold, after the electronic device performs the concurrent copying GC process, that is, after the electronic device performs GC processing on the memory object, memory object 304b is located in the cold area of ​​the second partition.

[0091] It should be noted that Figure 4 In the corresponding process, for memory object 301b and memory object 304b, the electronic device uses memory object 301a more frequently than the electronic device uses memory object 304a; then, after the electronic device performs GC processing on the memory objects, memory object 301b is located in the hot zone of the second partition, and 304b is located in the cold zone of the second partition.

[0092] It should be understood that the above-mentioned GC indication may be generated by the GC thread based on the thread's usage of memory objects, or may be sent to the GC thread by other modules, such as the memory monitoring module. For example, when the available memory of the mobile phone is less than or equal to the memory threshold A, the GC thread generates a GC indication. For another example, when the memory size occupied by the application is greater than or equal to the memory threshold B, the memory monitoring module sends a GC indication to the GC thread. Among them, the above-mentioned memory threshold A and memory threshold B can both be 32MB, 128MB, etc. In some embodiments, the above-mentioned memory threshold A and memory threshold B can change according to the operation of the application during the application running. Specifically, it can be set according to actual usage needs.

[0093] From this understanding, it can be seen that the electronic device performs GC processing in response to the GC indication; that is, if the memory size occupied by the application is greater than or equal to the memory threshold, the electronic device performs GC processing on the memory object corresponding to the target application.

[0094] It should also be understood that there are many other ways to generate GC indications, and the embodiments of the present application do not limit this. Also, regarding the process of reachability analysis performed by the GC thread, please refer to the relevant technology, for example, reachability analysis is performed through reference counting. For another example, reachability analysis is performed by judging whether the memory object is referenced by the root node of the memory object. Also, in some other examples, the reachability analysis process can have more implementations, and the embodiments of the present application do not impose any restrictions on this. In some other embodiments, reachability analysis can also be referred to as reachability judgment, reachability testing, etc. Also, in some other embodiments, a memory object passing the reachability analysis can be expressed as a memory object meeting the preset conditions, and a memory object failing the reachability analysis can be expressed as a memory object not meeting the preset conditions.

[0095] Below, we will use the above Figure 1 In the usage scenario shown, the electronic device is a mobile phone, which has the above Figure 2 The hardware structure shown, and Figure 3 Taking the software architecture shown as an example, the technical solution provided by the embodiment of the present application is introduced.

[0096] For example, see Figure 5 The garbage collection method provided in the embodiment of the present application may include steps S500-S504.

[0097] S500. In response to a user's start-up operation on an application, a virtual machine of the application is started.

[0098] It should be understood that the aforementioned application can be a third-party application or a system application. Furthermore, the aforementioned user operation for launching the application can be a click on the application icon or a voice command. Specifically, the user's operation for launching the application can be designed based on actual usage requirements, and the embodiments of this application do not limit this.

[0099] It should be pointed out that after the virtual machine is started, the GC thread of the virtual machine and the interpreter of the virtual machine are also in the started state.

[0100] In some embodiments, in response to a user's start-up operation on an application, the mobile phone generates a start-up instruction for the application. Next, in response to the start-up instruction for the application, the virtual machine of the application is started.

[0101] S501. The interpreter obtains hotspot information of the memory object.

[0102] The hotspot information of a memory object can be used to represent how frequently the memory object is used.

[0103] In some embodiments, some memory objects may survive for a relatively long time, such as when they experience multiple GC and non-GC periods. Furthermore, the frequency with which these memory objects are used may vary across these multiple GC and non-GC periods. Therefore, hotspot information may also be time-sensitive, such as being updated based on GC periods. That is, in these embodiments, hotspot information can be used to characterize the frequency with which a memory object is used within a non-GC period.

[0104] For example, the hotspot information may include a hotspot flag. If a memory object is frequently used by a thread during a non-GC period, the hotspot flag of the memory object is 1; if a memory object is not frequently used by a thread during a non-GC period, the hotspot flag of the memory object is 0.

[0105] For example, during a non-GC period, if a memory object is used by a thread more than or equal to M times, the hotspot flag of the memory object is 1; during a non-GC period, if a memory object is used by a thread less than M times, the hotspot flag of the memory object is 0. M is a positive integer.

[0106] As another example, the hotspot information may also include the number of times a memory object is used by a thread during a non-GC period, hereinafter referred to as the usage count. The memory object being used by a thread may be understood as the thread performing a read (or write) operation on the memory object, or the thread performing a write (or store) operation on the memory object.

[0107] For example, during the non-GC period, if a thread performs a read operation on a memory object once, the number of times the memory object is used is 1; during the non-GC period, if a thread performs 4 read operations and 2 write operations on a memory object, the number of times the memory object is used is 6.

[0108] In the following embodiments of the present application, the technical solutions provided by the embodiments of the present application will be described by taking the hotspot information being a hotspot flag as an example.

[0109] As a possible implementation, the interpreter may obtain hotspot information of the memory object based on fetch / write instructions for the memory object.

[0110] It should be understood that the interpreter's function is to execute a Java file as a binary file. In other words, the interpreter is used to interpret and execute a first code to be executed as a second code. For example, the first code is converted into a second code; the first code may be Java code, such as bytecode, and the second code may be native code, such as machine code. Therefore, when the Java code runs to a read / write instruction for a memory object, the interpreter can obtain hotspot information for the memory object. Specifically, this process can be achieved by instrumenting the first code.

[0111] For example, see Figure 6 When the first code, that is, the Java code, runs to a fetch instruction or a write instruction for a memory object, the interpreter sets the hotspot flag of the memory object to 1. Furthermore, the interpreter converts the first code into the second code.

[0112] For example, when the iget-object instruction, aget-object instruction, sget-object instruction, iput-object instruction, aput-object instruction, and sput-object instruction appear in the first code, the hotspot flag of the memory object is 1. Here, iget, aget, sget, iput, aput, and sput all represent instruction names, and object represents the address of the memory object indicated by the instruction.

[0113] S502. The interpreter stores the hotspot information of the memory object in the file header of the memory object.

[0114] It should be understood that the file header of a memory object is part of the data format of the memory object.

[0115] For example, see Figure 7The data format of a memory object can be divided into a file header area, an instance data area, and an alignment padding area. It should be understood that the file header area can also be called an object header area, and the file header area can also be simply called a file header. In some cases, the alignment padding area is optional.

[0116] The file header area includes: a lock byte and a type pointer. The lock byte is used to store basic information about memory objects in different states, such as hash code, memory lock related information, forwarding address, etc. The instance data area includes: instance data. Instance data is data stored by the memory object. The alignment padding area includes: alignment data. Alignment data is used to byte-align the entire memory object. For example, in a 64-bit operating system, the memory object needs to be aligned to 8 bytes. If the file header area and instance data area of ​​the memory object meet the 8-byte requirement, for example, the sum of the file header area and instance data area is a whole byte of 8 bytes, then the memory object may not include alignment data. The forward address can also be called a forwarding pointer, forwarding address, etc.

[0117] For example, a lockword may be 32 bits, and basic information of a memory object in different states is stored by changing each bit.

[0118] For example, see Figure 8 The lock word can contain 32 bits, numbered 0-31. Bits 31 and 30 record the state of the memory object. For example, bits 00 record the light lock state, bits 01 record the heavy lock state, bits 10 record the hash state, and bits 11 record the garbage collection state. Bits 31-30 of the lock word can also be called the state bits of the memory object.

[0119] When the memory object is in a lightly locked state, that is, bits 31-30 of the lockword are 00, bit 29 records whether the memory object is alive. For example, an m value of 1 indicates that the memory object is alive, and an m value of 0 indicates that the memory object is not alive. Bit 28 records the read credentials of the memory object. For example, an r value of 1 indicates that the memory object can be read, and an r value of 0 indicates that the memory object cannot be read. Bits 26-16 record the lightly locked count information (lock count), such as the number of times the memory object has used a light lock in binary. Bits 15-00 record the thread ID (thread id owner) holding the light lock. Bit 27 records the hotspot flag. Figure 8 It is represented by h. If h is 1, it means that the memory object is frequently used; if h is 0, it means that the memory object is frequently used.

[0120] It should be noted that, in some embodiments, when bits 31-30 of the lockword of a memory object are 00 and bits 26-00 are all 0, the state of the memory object may also be referred to as an unlocked state.

[0121] When the memory object is in a relocked state, that is, bits 31-30 of the lockword are 01, bit 29 records whether the memory object is alive; bit 28 records the read credentials of the memory object; bits 26-00 are relock information (monitor ID).

[0122] When the memory object is in a hashed state, bits 31-30 of the lockword are 10, bit 29 records whether the memory object is alive, bit 28 records the read credentials of the memory object, and bits 26-00 are hash information (hash code), such as the hash value of the memory object instance data. Bit 27 records the hotspot flag. Figure 8 It is represented by h. If h is 1, it means that the memory object is frequently used; if h is 0, it means that the memory object is not frequently used.

[0123] When the memory object is in the GC state, that is, bits 31-30 of the lockword are 11, bit 29 is 0, and bits 28-00 record the forwarding address. The forwarding address is used to indicate the memory address of the memory object in the from partition during the GC process.

[0124] It should be noted that in the lightly locked state, the 27th bit of the lockword of the memory object is the hotspot flag; in the hash state, the 27th bit of the lockword of the memory object is the hotspot flag; and, in the heavily locked state, the hotspot flag is not stored in the memory object. It can be understood that the heavily locked state is the state that the memory object will be converted to after being in the lightly locked state many times, that is, its state itself can be used to represent that the memory object is frequently used, so the hotspot flag can be not stored in the memory object in the heavily locked state. In other words, the heavily locked state can be understood as a hotspot flag with a value of 1. In addition, the GC state usually only appears when the GC thread performs GC on the memory object, so the hotspot flag can be not stored in the memory object in the GC state.

[0125] It is understandable that in some other embodiments, other bits of the lockword of the memory object may also be set as hotspot flags. Specifically, the setting may be made according to actual usage requirements.

[0126] As a possible implementation, when the interpreter obtains that the hotspot flag is 1, the interpreter may store the hotspot flag in the 27th bit of the lockword of the memory object according to the state of the memory object.

[0127] For example, in step S501 above, the interpreter obtains the hotspot flag of memory object A as 1, and obtains the status bit of memory object A. If the status bit of memory object A is 00, the 27th bit of the lockword of the memory object is set to 1. If the status bit of memory object A is 10, the 27th bit of the lockword of the memory object is set to 1.

[0128] It is understandable that in some embodiments, the above steps S501 and S502 may be executed multiple times in a loop, for example, after executing step S501, step S502 is executed, and then steps S501-S502 are executed again until the GC thread obtains a GC indication.

[0129] After the GC thread obtains the GC indication, the GC thread executes step S503 .

[0130] S503. In response to the GC instruction, the GC thread performs concurrent copying GC on the memory object according to the hot spot information of the memory object.

[0131] In some embodiments, if the hotspot flag of a memory object is 1, the GC thread copies the memory object to the hot zone of the to partition; if the hotspot flag of a memory object is 0, the GC thread copies the memory object to the cold zone of the to partition. For more information about how the GC thread copies memory objects to the hot zone of the to partition, see the above Figure 4 The corresponding description is not repeated here.

[0132] It should be noted that, for the hot zone, its physical address is continuous, and it can be deployed in the internal memory of the mobile phone. For the cold zone, its physical address can be continuous, and it can be deployed in the internal memory of the mobile phone, or it can be deployed in the external memory of the mobile phone, such as a disk. Among them, for the cold zone and the hot zone of the first partition, the ratio of the two to the first partition can be 5:5, or it can be 3:7, 4:6, etc. For the cold zone and the hot zone of the second partition, the ratio of the two to the second partition can be the same as that of the first partition, or it can be different, and the embodiments of the present application do not impose any restrictions on this.

[0133] It should be understood that for the cold zone, the mobile phone can set the cold zone in the mobile phone's disk through virtual memory technology, thereby saving space in the mobile phone's internal memory.

[0134] As a possible implementation, in response to a GC indication, when the memory object is in a lightly locked state and passes a reachability test, if the hotspot flag is 1, the memory object is copied from the from partition to the hot zone of the to partition.

[0135] As another possible implementation, in response to a GC indication, when the memory object is in a lightly locked state and passes a reachability test, if the hotspot flag is 0, the memory object is copied from the from partition to the cold zone of the to partition.

[0136] As another possible implementation, in response to a GC indication, when the memory object is in a relocked state and passes a reachability test, the memory object is copied from the from partition to the hot zone of the to partition.

[0137] As another possible implementation, in response to a GC indication, when the memory object is in a hash state and passes a reachability test, if the hotspot flag is 1, the memory object is copied from the from partition to the hot zone of the to partition.

[0138] As another possible implementation, in response to a GC indication, when the memory object is in a hash state and passes a reachability test, if the hotspot flag is 0, the memory object is copied from the from partition to the cold zone of the to partition.

[0139] For example, see Figure 9 , the hotspot information is a hotspot flag, the hotspot flag is 1, indicating that the memory object is frequently used, and the hotspot flag is 0, indicating that the memory object is not frequently used, as an example, the above step S503 is introduced. Figure 9 As shown, the above step S503 may include steps S900-S905.

[0140] S900.GC thread performs reachability check on each memory object.

[0141] If the memory object fails the reachability check, step S901 is executed. If the memory object passes the reachability check, step S902 is executed.

[0142] S901. The GC thread deletes the memory object from the from partition.

[0143] S902. The GC thread obtains the status of the memory object.

[0144] If the memory object is in a lightly locked state or a hashed state, step S903 is executed.

[0145] If the memory object is in a relocked state, step S904 is executed.

[0146] S903. The GC thread obtains the hotspot flag of the memory object.

[0147] If the hotspot flag of the memory object is 1, execute step S904; if the hotspot flag of the memory object is 0, execute step S905.

[0148] S904. The GC thread copies the memory object to the hot zone of the to partition.

[0149] S905. The GC thread copies the memory object to the cold area of ​​the to partition.

[0150] In some embodiments, after step S904 or step S905, the GC thread may execute step S901. Alternatively, after executing steps S900-S905 for each memory object, the GC thread may clear the from partition.

[0151] Thus, through steps S900-S905, frequently used memory objects can be copied to the hot zone of the "to" partition. This way, the frequently used memory objects are located close to each other in the "to" partition. This improves the locality of memory objects after the concurrent copying GC process. This improves the efficiency of the mobile phone's memory object usage and enhances the performance of the mobile phone.

[0152] Next, after step S503 or steps S900 - S905 , the GC thread is executed to execute step S504 .

[0153] S504. The GC thread deletes the hotspot information of the memory object.

[0154] It should be understood that the state of a memory object may or may not change during the GC process, but its state before and after GC should be consistent. For example, if a memory object is in a lightly locked state in the "from" partition, then after it passes reachability analysis and is copied to the "to" partition by the GC thread, the memory object will also be in a lightly locked state in the "to" partition.

[0155] In some embodiments, the GC thread can delete the hotspot information of the memory object after the concurrent copying GC process ends. For example, in response to the absence of the memory object in the from partition, the GC thread deletes the hotspot flag of each memory object in the to partition.

[0156] In other embodiments, the GC thread may also delete the hotspot information of a memory object during the concurrent copying GC process. For example, after a memory object is copied from the from partition to the to partition, the hotspot information of the memory object is deleted.

[0157] As a possible implementation, when the memory object is in a lightly locked state, the GC thread sets the 27th bit to 0 to delete the hotspot information of the memory object.

[0158] As a possible implementation, when the memory object is in a hash state, the GC thread sets the 27th bit to 0 to delete the hot spot information of the memory object.

[0159] It should be understood that in some embodiments, the above steps S501-S504 can be executed in a loop, for example, after step S504, steps S501-S502 are executed in a loop; thereafter, in response to the GC indication, steps S503-S504 are executed, and the loop is repeated in sequence until the virtual machine of the application is destroyed.

[0160] In some other embodiments, during the above-mentioned step S503, when the GC thread is performing concurrentcopying GC, the GC thread may also adopt one or more strategies including mobility partitioning strategy, object size partitioning strategy, and survival time strategy to improve the GC efficiency of concurrentcopying GC and increase GC benefits.

[0161] As a possible implementation, the GC thread can improve the GC efficiency of concurrent copying GC and increase GC benefits through a survival time strategy. In other words, the GC thread can use the parallel copying generational garbage collection (CCgenerational GC) method to perform garbage collection.

[0162] In the CC generational GC method, the GC thread can classify memory objects into new generation (new) and old generation (old) memory objects based on their lifetime. For example, if a memory object has undergone (or executed) K rounds of GC, then the memory object is an old memory object; if the memory object does not survive after K rounds of GC, then the memory object is a new memory object, where K is a positive integer. For example, if K is 1, after the first round of GC, memory object A survives, meaning that memory object A passes the reachability test. During the first round of GC, memory object A is copied from the from partition to the to partition. During the second round of GC, since memory object A has undergone one round of GC, memory object A is an old memory object. If memory object B does not exist during the first round of GC, but exists during the second round, meaning that memory object B has not undergone one round of GC, then memory object B is a new memory object.

[0163] For old memory objects, the GC thread does not check their reachability and directly copies them from the from partition to the to partition. For new memory objects, the GC thread checks the reachability of the new memory objects and copies them from the from partition to the to partition based on the reachability of the new memory objects.

[0164] As you can understand, in the CC generational GC method, since the GC thread does not perform reachability checks on old memory objects, compared to the concurrent copying GC process, the overhead of performing reachability checks on old memory objects can be reduced. This improves the efficiency of concurrent copying GC, thereby increasing GC benefits. In other words, when the interpreter obtains hotspot information for memory objects, it can also obtain hotspot information for new memory objects.

[0165] As a possible example, during the process of step S501 above, the interpreter can also obtain the lifetime of the memory object. In other words, the interpreter can also determine whether the memory object is an old memory object or a new memory object. If the memory object is an old memory object, the interpreter does not obtain its hotspot information. If the memory object is a new memory object, the interpreter obtains its hotspot information.

[0166] It is understandable that when the interpreter obtains the hotspot information of the memory object, it also consumes the resources of the electronic device. Therefore, not obtaining the hotspot information of the old memory object can further save the resources of the electronic device.

[0167] In some implementations, the GC thread can perform CC generational GC on memory objects based on the hotspot flag of the memory objects. Figure 10 Taking the example of a hotspot flag of 1 indicating that a memory object is frequently used and a hotspot flag of 0 indicating that a memory object is not frequently used, the process of CC generational GC by the GC thread based on the hotspot flag of the memory object is introduced. Figure 10 As shown, the process may include steps S1000-S1006.

[0168] S1000.GC thread obtains the survival time of each memory object.

[0169] It should be understood that the above step S1000 can also be expressed as the GC thread determining whether the memory object is an old memory object or a new memory object.

[0170] If the memory object is an old memory object, execute step S1001; if the memory object is a new memory object, execute step S1002.

[0171] S1001. The GC thread copies the memory object to the hot zone of the to partition.

[0172] S1002. The GC thread determines the reachability of the memory object.

[0173] If the memory object fails the reachability check, step S1003 is executed. If the memory object passes the reachability check, step S1004 is executed.

[0174] S1003. The GC thread deletes the memory object from the from partition.

[0175] S1004. The GC thread obtains the status of the memory object.

[0176] If the memory object is in a lightly locked state or a hashed state, step S1005 is executed.

[0177] If the memory object is in a relocked state, the above step S1001 is executed.

[0178] S1005. The GC thread obtains the hotspot flag of the memory object.

[0179] If the hotspot flag of the memory object is 1, execute step S1001; if the hotspot flag of the memory object is 0, execute step S1006.

[0180] S1006. The GC thread copies the memory object to the cold area of ​​the to partition.

[0181] Optionally, in some examples, after step S1006 or step S1001, the GC thread may execute step S1003. In other examples, after steps S1000-S1006 are executed for each memory object, the GC thread may clear the from partition.

[0182] In other embodiments, the ratio of the partition occupied by the cold zone and the hot zone during the CC generational GC process can change during the CC generational GC process. For example, the more old objects there are during the CC generational GC process, the larger the ratio of the partition occupied by the hot zone. In other words, the more old generation memory objects there are, the larger the ratio of the hot zone in the to partition. The fewer old generation memory objects there are, the smaller the ratio of the hot zone in the to partition. In this way, the phenomenon of hot zone being wasted due to overly large hot zones and the phenomenon of insufficient hot zone space due to overly small hot zones can be alleviated.

[0183] The above steps S1000-S1006 are described below in conjunction with the change process of the memory object.

[0184] For example, see Figure 11A and Figure 11B The memory space available to the virtual machine, that is, the memory space available to the threads created by the virtual machine, can be divided into the first partition and the second partition, and the memory objects are located on these partitions. In addition, the first partition can be divided into the first partition hot zone and the first partition cold zone; the second partition can be divided into the second partition hot zone and the second partition cold zone. Figure 11A and Figure 11B In the corresponding process, the partition ratio occupied by the hot zone remains unchanged.

[0185] In the first non-GC period, the interpreter executes step S500 and step S501 in a loop. After the interpreter executes step S500 and step S501 in a loop, the memory of the mobile phone is as follows: Figure 11A As shown in 1100A-1.

[0186] The first partition includes memory object 901a, memory object 902a, memory object 903a, and memory object 904a. Figure 11AIn the figure, the state of the memory object is shown by the number above the memory object, "00" is the light lock state, "10" is the heavy lock state, and "10" is the hash state. In addition, the hollow circle above the memory object shows that the hotspot flag of the memory object is 0; the solid circle above the memory object shows that the hotspot flag of the memory object is 1; if there is no hollow circle or solid circle above the memory object, then the memory object has no hotspot flag. Figure 11A As can be seen, memory object 901a is in the heavily locked state and has no hotspot flag; memory object 902a is in the hash state and has a hotspot flag of 0; memory object 903a is in the lightly locked state and has a hotspot flag of 0; memory object 904a is in the hash state and has a hotspot flag of 1. Furthermore, memory objects 901a, 902a, 903a, and 904a are all new memory objects.

[0187] After the GC thread obtains the GC indication, it proceeds to the first GC period. The GC thread executes steps S1000-S1006. After the GC thread executes steps S1000-S1006, the memory of the mobile phone is as follows: Figure 11A As shown in 1100A-2.

[0188] During the first GC period, the GC thread executes steps S1000-S1006. Since memory object 903a fails the reachability analysis, the GC thread deletes 903a from the from partition. Furthermore, since memory object 901a is in a heavily locked state, the GC thread copies memory object 901a to the hot zone of the to partition, obtaining memory object 901b. Furthermore, since memory object 902a is in a hash lock state and the hotspot flag is 0, the GC thread copies memory object 902a to the cold zone of the to partition, obtaining memory object 902b. Furthermore, since memory object 904a is in a hash lock state and the hotspot flag is 1, the GC thread copies memory object 904a to the hot zone of the to partition, obtaining memory object 904b.

[0189] Then, the GC thread clears the from partition and executes step S503. After the GC thread clears the from partition and executes step S503, the memory of the mobile phone is as follows Figure 11A As shown in 1100A-3.

[0190] Next, after the first GC period, the second non-GC period begins.

[0191] In the second non-GC period, the interpreter executes step S500 and step S501 in a loop. After the interpreter executes step S500 and step S501 in a loop, the memory of the mobile phone is as follows: Figure 11B As shown in 1100B-1.

[0192] The second partition includes memory object 901c, memory object 902c, memory object 904c, and memory object 905c. Among them, memory object 901c and memory object 904c are old memory objects; memory object 902c and memory object 905c are new memory objects. Figure 11B In the example, the number above the memory object indicates the state of the memory object: "00" indicates the lightly locked state, "10" indicates the heavily locked state, and "10" indicates the hash state. Furthermore, a hollow circle above a memory object indicates that the hotspot flag of the memory object is 0; a solid circle above a memory object indicates that the hotspot flag of the memory object is 1. If there is no hollow or solid circle above a memory object, then the memory object does not have a hotspot flag.

[0193] During the interpreter's loop execution of steps S500 and S501, since memory objects 901c and 904c are old memory objects, the interpreter obtains the hotspot flags of these two memory objects. Figure 11B As can be seen, memory object 901c is in a lightly locked state, memory object 904c is in a hash state; memory object 905c is in a lightly locked state and the hotspot flag is 0, and memory object 902c is in a lightly locked state and the hotspot flag is 1.

[0194] After the GC thread obtains the GC indication, it proceeds to the second GC period. The GC thread executes steps S1000-S1006. After the GC thread executes steps S1000-S1006, the memory of the mobile phone is as follows: Figure 11B As shown in 1100B-2.

[0195] During the second GC period, when the GC thread executes steps S1000-S1006, memory objects 901c, 902c, 904c, and 905c all pass the reachability test. Since memory object 901c is an old memory object, the GC thread copies memory object 901c to the hot zone of the to partition, obtaining memory object 901d. Since memory object 904c is an old memory object, the GC thread copies memory object 904c to the hot zone of the to partition, obtaining memory object 904d. Since memory object 902c is in a lightly locked state and the hotspot flag is 1, the GC thread copies memory object 902c to the hot zone of the to partition, obtaining memory object 902d. Since memory object 905c is in a lightly locked state and the hotspot flag is 0, the GC thread copies memory object 905c to the cold zone of the to partition, obtaining memory object 905d.

[0196] Then, the GC thread clears the from partition and executes step S503. After the GC thread clears the from partition and executes step S503, the memory of the mobile phone is as follows Figure 11BAs shown in 1100B-3.

[0197] It should be noted that the personal information used in the technical solution of this application is limited to information for which the individual’s separate consent has been obtained, including but not limited to notifying and reminding the user to read the relevant user agreement (notification) and sign the agreement (authorization) including authorization of relevant user information before the user uses the function.

[0198] In conjunction with the algorithmic steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in hardware or in a manner driven by computer software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in conjunction with the embodiments, but such implementation should not be considered to be beyond the scope of this application.

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

[0200] An embodiment of the present application also provides a computer-readable storage medium, which stores computer program code. When the above-mentioned processor executes the computer program code, the electronic device or wearable device executes the relevant method steps in the above-mentioned method embodiment.

[0201] An embodiment of the present application also provides a computer program product, which, when executed on an electronic device or a wearable device, enables the electronic device or the wearable device to execute the relevant method steps in the above method embodiment.

[0202] Among them, the electronic device, wearable device, computer-readable storage medium or computer program product provided in this application is used to execute the corresponding method provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding method provided above, and will not be repeated here.

[0203] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0204] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

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

[0206] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0207] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the contributing part or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a device (which can be a single-chip microcomputer, chip, etc.) or a processor to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0208] The above content is only a specific embodiment of this application, but the scope of protection of this application is not limited to this. Any changes or replacements within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.

Claims

1. A garbage collection method, characterized in that: The method is applied to an electronic device, wherein the memory space of the electronic device includes a first partition and a second partition, the first partition includes a first sub-partition and a second sub-partition, the memory addresses of the first sub-partition are continuous, the first partition is different from the second partition, and the first sub-partition is different from the second sub-partition; the method includes: After the target application is started, if the memory size occupied by the target application is greater than or equal to the memory threshold, garbage collection (GC) processing is performed on the memory objects corresponding to the target application; the performing of GC processing on the memory objects corresponding to the target application includes: If the memory object meets a preset condition, the memory object is copied from the second partition to the first partition; wherein, for a memory object in a lightly locked state or a hashed state, if the frequency of the memory object is greater than or equal to a degree threshold, the memory object is copied to the first sub-partition; if the frequency of the memory object is less than the degree threshold, the memory object is copied to the second sub-partition; if the memory object meets the preset condition, for a memory object in a heavily locked state, the memory object is copied to the first sub-partition; If the memory object does not meet the preset condition, the memory object is deleted from the second partition.

2. The method according to claim 1, characterized in that The memory object includes a new generation memory object or an old generation memory object; the old generation memory object is a memory object that has undergone K rounds of GC processing, where K is a positive integer; The copying of the memory object from the second partition to the first partition when the memory object meets the preset condition includes: If the new generation memory object meets the preset condition, copying the new generation memory object from the second partition to the first partition; The step of deleting the memory object from the second partition when the memory object does not meet the preset condition includes: If the new generation memory object does not meet the preset condition, deleting the new generation memory object from the second partition; The performing of garbage collection (GC) processing on the memory objects corresponding to the target application further includes: The old generation memory object is copied from the second partition to the first subpartition.

3. The method according to claim 2, characterized in that The proportion of the first sub-partition in the first partition is positively correlated with the number of the old generation memory objects.

4. The method according to any one of claims 1 to 3, characterized in that Before performing GC processing on the memory object corresponding to the target application, the method further includes: Acquire hotspot information of the memory object, where the hotspot information is used to indicate how frequently the target application uses the memory object; The copying of the memory object to the first sub-partition when the frequency degree of the memory object is higher than or equal to a frequency threshold comprises: In a case where the hotspot information indicates that the frequency of use of the memory object by the target application is greater than or equal to a frequency threshold, the memory object is copied to the first sub-partition; When the frequency degree of the memory object is lower than the frequency threshold, the memory object is copied to the second sub-partition, comprising: If the hotspot information indicates that the frequency of the memory object is lower than the frequency threshold, the memory object is copied to the second sub-partition.

5. The method according to claim 4, characterized in that The obtaining of hotspot information of the memory object includes: The hotspot information of the memory object is written into the file header of the memory object.

6. The method according to claim 5, characterized in that The state of the memory object includes a light lock state, a heavy lock state or a hash state; Writing the hotspot information of the memory object into the file header of the memory object includes: When the state of the memory object is the light lock state or the hash state, the hotspot information is written into the file header of the memory object.

7. The method according to claim 6, characterized in that The state of the memory object is a light lock state, and the file header of the memory object in the light lock state includes a first field, and the first field is used to store light lock count information for the memory object; Writing the hotspot information into the file header of the memory object includes: The hotspot information is written into the first field.

8. The method according to claim 6 or 7, characterized in that The state of the memory object is a hash state, and the file header of the memory object in the hash state includes a second field, and the second field is used to store a hash value for the memory object; Writing the hotspot information into the file header of the memory object includes: The hotspot information is written into the second field.

9. The method according to claim 4, characterized in that The obtaining of hotspot information of the memory object includes: Obtaining a read instruction for the memory object and obtaining a write instruction for the memory object; The hot spot information is obtained based on a read instruction for the memory object and a write instruction for the memory object.

10. The method according to claim 9, characterized in that The obtaining the hotspot information based on the read instruction and the write instruction for the memory object includes: When the sum of the number of fetch instructions for the memory object and the number of write instructions for the memory object is greater than or equal to M times, obtaining hotspot information indicating that the frequency of use of the memory object by the target application is greater than or equal to a degree threshold; M is a positive integer; When the sum of the number of fetch instructions for the memory object and the number of write instructions for the memory object is less than M times, hotspot information is obtained indicating that the frequency of use of the memory object by the target application is lower than a frequency threshold.

11. The method according to any one of claims 1 to 10, characterized in that The first sub-partition is located in the internal memory of the electronic device, and the second sub-partition is located in the internal memory of the electronic device or a disk of the electronic device.

12. An electronic device, characterized in that: The electronic device includes a processor and a memory; the processor is coupled to the memory; the memory is used to store computer program code; the computer program code includes computer instructions, and when the processor executes the above-mentioned computer instructions, the electronic device executes the method according to any one of claims 1 to 11.

13. A computer-readable storage medium, characterized in that The computer-readable storage medium includes computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the method according to any one of claims 1 to 11.

14. A computer program product, characterized in that The computer program product includes instructions, and when the computer program product is run on an electronic device, the electronic device is caused to execute the method according to any one of claims 1 to 11.

Citation Information

Patent Citations

  • Object management method and device

    CN116737350A

  • Method for controlling memory and electronic equipment

    CN116820734A