Garbage collection method, electronic equipment and storage medium
By copying memory objects to different memory partitions in electronic devices according to the frequency of memory objects, and using hotspot information to optimize the location of memory objects, the problem of low efficiency of memory objects after garbage collection is solved, and the performance of electronic devices is improved .
Patent Information
- Application Number
- CN202410175913.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-07
- Publication Date
- 2025-01-03
- Estimated Expiration
- 2044-02-07
AI Technical Summary
At this stage, how to improve the efficiency of memory objects after electronic devices perform garbage collection and processing is a problem that needs to be solved.
By introducing a frequency threshold into electronic devices, copying it to different memory partitions according to the frequency of memory objects, using hot spot information to optimize the location of memory objects, improving the locality of memory objects, thereby improving the working efficiency of electronic devices.
By concentrating frequently used memory objects on continuous memory addresses, the locality of memory objects is improved and the memory usage efficiency and performance of electronic devices are improved.
Smart Images

Figure CN119248463A_ABST
Abstract
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, an electronic device, and a storage medium. Background Art
[0002] Garbage collection (GC) is a memory management mechanism. It can be understood that when some memory objects are no longer needed, they are deleted to release the memory space occupied by these memory objects. This storage resource management mechanism is called garbage collection.
[0003] At present, the memory space of an electronic device can be divided into a first partition and a second partition. The memory objects are in the second partition. When the electronic device performs GC processing, the electronic device copies the memory objects that meet the preset conditions from the second partition to the first partition and empties the second partition. In this way, the memory objects that do not meet the preset conditions are deleted, that is, the garbage memory objects are deleted.
[0004] At present, how to improve the efficiency of using memory objects by electronic devices after performing GC processing is a problem to be solved. Summary of the invention
[0005] In view of this, the embodiments of the present application provide a garbage collection method, an electronic device, and a storage medium. In the method, memory objects with similar frequency of use by the electronic device are located close to each other on the first partition after GC processing, thereby improving the locality of the memory objects. This can improve the working efficiency of the electronic device and improve the performance of the electronic device.
[0006] In order to achieve the above purpose, the embodiment of the present application adopts the following technical solution:
[0007] In the first aspect, the embodiment of the present application provides a garbage collection method, which can be applied to electronic devices such as mobile phones and tablet computers. 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 address of the first sub-partition is continuous, the first partition and the second partition are different, and the first sub-partition is different from the second sub-partition. The above 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, the electronic device performs garbage collection GC processing on the memory object corresponding to the target application. The above electronic device performs GC processing on the memory object corresponding to the target application, including: when the memory object meets the preset conditions, the electronic device copies the memory object from the second partition to the first partition. Among them, 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. When the memory object does not meet the preset conditions, the electronic device deletes the memory object from the second partition. And, the memory object meets the preset conditions can be understood as the memory object passing the reachability test, and the memory object does not meet the preset conditions 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, such as memory objects with a frequency higher than a 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, that is, 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 be used to concentrate memory objects with high frequency used by the target application on the first sub-partition, thereby improving the locality of the 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 memory objects include new generation memory objects or old generation memory objects; the old generation memory objects are memory objects that have undergone K rounds of GC processing, K being a positive integer; the new generation memory objects are memory objects other than the old generation memory objects. That is, the 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 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, when the memory object does not meet the preset conditions, the electronic device deletes the memory object from the second partition, 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 garbage collection GC processing is performed on the memory object corresponding to the target application, and also includes: the electronic device copies the old generation memory object from the second partition to the first sub-partition.
[0010] In this design, considering that the electronic device determines whether the memory object meets the preset conditions, it will also consume the resources of the electronic device. Therefore, for the old generation memory object, the electronic device may not determine whether it meets the preset conditions, and the electronic device directly copies the old generation memory to the first sub-partition, thus saving the resource consumption of the electronic device to determine whether the memory object meets the preset conditions, and further, the efficiency of the electronic device in the GC processing process can be improved.
[0011] In yet 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 more old generation memory objects there are, 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. In this way, the phenomenon of the first subpartition space being wasted due to the first subpartition space being too large and the phenomenon of the first subpartition space being insufficient due to the first subpartition space being too small can be alleviated.
[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 method further includes: the electronic device obtains hotspot information of the memory object, and the hotspot 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 hotspot information indicates that the target application uses the memory object at a frequency 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 hotspot 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 hot spot 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, and also includes: 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 can indicate a high frequency of memory objects. Therefore, memory objects in a relocked state that meet the preset conditions can be directly copied to the first sub-partition. In this way, the electronic device can save resource consumption for obtaining hot spot information of memory objects in a relocked state, and can further improve 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 the number of lightly locked times for the memory object. The electronic device writes the hotspot information into the file header of the memory object, including: the electronic device writes the hotspot information into the first field.
[0020] In this design, the electronic device can conveniently store the 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 writes the hotspot information into the file header of the memory object, including: the electronic device writes the hotspot information into the second field.
[0022] In this design, the electronic device can conveniently store the 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 hot spot 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 hot spot information of the electronic device through read instructions and write instructions for the memory object.
[0026] In another possible design of the first aspect, the electronic device obtains the hotspot information based on a read instruction for a memory object and a write instruction for a memory object, including:
[0027] 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, the electronic device obtains hotspot information indicating that the frequency of the target application using the memory object is greater than or equal to the 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, the electronic device obtains hotspot information indicating that the frequency of the target application using the memory object is less than the degree threshold.
[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 the disk of the electronic device. In this design, the electronic device can store the second subpartition on the disk of the electronic device through virtual memory technology, thereby saving the memory space of the electronic device.
[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] According to 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] According to 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 a software architecture of an electronic device provided in an embodiment of the present application;
[0036] Figure 4 A schematic diagram of the location of a memory object provided in an embodiment of the present application;
[0037] Figure 5 A schematic diagram of a garbage collection method provided in an embodiment of the present application;
[0038] Figure 6 A schematic diagram of a process for generating a hotspot flag by an 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] Fig. 9 A flowchart of another garbage collection method provided in an embodiment of the present application;
[0042] Fig.10 A schematic diagram of another garbage collection method provided in an embodiment of the present application;
[0043] Fig.11A Another schematic diagram of the location of a memory object provided in an embodiment of the present application;
[0044] Fig. 11B Another schematic diagram regarding the location of memory objects provided for 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 kind of association relationship describing 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. And, 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 its similar expressions refers to any combination of these items, including any combination of single items 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, in order to clearly describe the technical solutions of the embodiments of the present application, in the embodiments of the present application, words such as "first" and "second" are used to distinguish the same items or similar items with substantially the same functions and effects. Those skilled in the art can understand that words such as "first" and "second" do not limit the quantity and execution order, and words such as "first" and "second" do not necessarily limit the difference.
[0046] Meanwhile, in the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a concrete manner for ease of understanding.
[0047] In the technical solutions disclosed in this application, the collection, storage, use, processing, transmission, provision and disclosure of user personal information involved are in compliance with the 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 schemes, electronic devices can perform garbage collection by concurrent copying GC (CCGC). In this method, the memory space of the electronic device is divided into: a source (from) partition and a destination (to) partition. The electronic device determines whether to copy the memory object on the from partition to the to partition based on the accessibility of the memory object, and after the memory object on the from partition is copied, the memory object on the from partition is cleared; thus, garbage collection is achieved.
[0050] At present, how to improve the efficiency of using memory objects by electronic devices 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 characterize the frequency with which the electronic device uses the memory object. Afterwards, during the concurrent copying GC process, the electronic device copies the memory object to a location in the to partition that corresponds 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 the above method, memory objects with similar hotspot information, that is, memory objects with similar frequency of use by electronic devices, are also located close to each other in the to partition. Therefore, the locality of memory objects can be improved during the concurrent copying GC process. This can improve the efficiency of electronic devices in using memory objects and improve the performance of electronic devices.
[0053] Among them, locality, which can also be called the principle of locality, 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 the memory object will most likely be used by the processor in the future. And, for memory objects, temporal locality can be understood as, if a memory object is used by a processor, then the memory object will most likely be used by the processor in the future. According to the above analysis of spatial locality and temporal locality, for two memory objects, when the positions of the two memory objects are relatively close, the higher the frequency of use of the two memory objects, the better the locality of the memory objects. And, for two memory objects, when the frequency of use of the two memory objects is relatively high, the closer the positions of the two memory objects are, the better the locality of the memory objects. It can be seen that the more concentrated the positions 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 will preload other memory objects near a memory object when using it, the improvement of 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, so 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, so when the processor uses memory object B later, the processor also needs to load memory object B, and the processor's execution efficiency will be lower. It can be seen from this that the better the locality of the memory object, the higher the efficiency of the processor in using the memory object, and the better the performance of the electronic device.
[0057] For example, see Figure 1The technical solution provided in the embodiment of the present application can be applied to the process in which a user runs an application (APP) through an electronic device 100.
[0058] The electronic device may be a mobile phone, tablet computer, smart screen, augmented reality (AR) / virtual reality (VR) device, laptop computer, ultra-mobile personal computer (UMPC), netbook, personal digital assistant (PDA) and other electronic devices with memory. It may also be a smart watch, smart bracelet, electronic watch, sports bracelet and other wearable devices with memory.
[0059] Next, the hardware structure of the electronic device provided in the embodiment of the present application is introduced.
[0060] For example, see Figure 2 , Figure 2 A schematic structural diagram of an 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 is to be understood that the structure illustrated in the embodiment of the present invention does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or separate some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[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 processor (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), etc. 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] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0066] In some embodiments, the processor 110 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0067] The USB interface 130 is an interface that complies with the USB standard specification, and specifically can be a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface 130 can be used to connect a charger to charge the electronic device 100, and can also be used to transfer data between the electronic device 100 and a peripheral device. It can also be used to connect headphones to play audio through the headphones. The interface can also be used to connect other electronic devices, such as AR devices, etc.
[0068] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present invention is only a schematic illustration and does not constitute a structural limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may also adopt different interface connection methods in 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 through the external memory interface 120 to implement a data storage function, such as storing music, video and other files in 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 may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc. The data storage area may store data created during the use of the electronic device 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 may adopt a layered architecture, an event-driven architecture, a micro-core 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 exemplified.
[0073] Figure 3 1 is a 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 layer has 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] Understandably, 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 may have different layer division methods, and technicians may 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 a programming framework for the 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 in the form of a thread, waiting for requests from applications, processing the requests, and returning the results to the applications. The above subsystem services include, for example, window manager service (WMS), notification manager service (NMS), activity manager service (AMS), input manager service (IMS), etc.
[0078] The local layer, also known as the system runtime layer, can include multiple functional modules, such as surface manager, media library, 3D graphics processing library (such as OpenGL ES), 2D graphics engine (such as SGL), core library, etc.
[0079] The surface manager is used to manage the display subsystem and provides fusion of 2D and 3D layers for multiple applications. The media library supports playback and recording of a variety of commonly used 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 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 examples, ART may also be referred to as Android TM Runtime virtual machine, referred to as virtual machine. The virtual machine can include GC thread and interpreter. The interpreter is used to interpret and execute Java files into binary files. GC thread is used for garbage collection.
[0081] The kernel driver layer, also called the kernel layer, is the layer between hardware and software. The kernel layer includes at least display driver, camera driver, audio driver, and sensor driver.
[0082] The garbage collection method provided in the embodiment of the present application is introduced below in the software architecture of the electronic device 100 in combination with the process of the electronic device 100 running the APP.
[0083] For example, see Figure 4 , Figure 4 The process of concurrent copying GC of memory objects by CG threads according to the hot spot information of memory objects is shown. The memory space that can be used by the virtual machine, that is, the memory space that can be used by the threads established 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 is divided into the second partition hot zone and the second partition cold zone. For example, the first partition 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 objects 301a, 302a, 303a, and 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, copying memory object 301a to the hot zone of the second partition obtains memory object 301b, and copying memory object 302a to the hot zone of the second partition obtains memory object 302b. For another example, copying memory object 304a to the cold zone of the second partition obtains 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 can be referred to as the GC period. Also, the non-GC period can be understood as the period other than the GC period during the operation of the virtual machine.
[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 the memory object from the 1st partition to the 2nd partition. Therefore, in this GC period, the 1st partition can also be called the from partition, and the 2nd partition can also be called the to partition. Also, if the GC thread copies the memory object from the 2nd partition to the 1st partition in the next GC period, then in this GC period, the 2nd partition can also be called the from partition, and the 1st partition can also be called the to partition. In some other embodiments, the to partition can also be called the first partition, the from partition can also be called the second partition, the hot area of the to partition can also be called the first sub-partition, and the cold area of the to partition can also be called the second sub-partition.
[0087] And because in the above process, the memory objects of the from partition are selectively copied to the to partition, the memory objects copied from the from partition to the to partition can be understood as the same memory object in the from partition and in the to partition. For example, memory object 301a and memory object 301b can be understood as the same memory object. For another example, memory object 304a and memory object 304b can be understood as 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, in a certain time period (such as, 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, in a certain time period (such as, 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 are not limited to this.
[0089] After that, the GC thread deletes the memory objects in the first partition. After this process, the memory objects frequently used by the electronic device are all located in the hot zone of the second partition. Therefore, the locality of the memory objects can be improved, so the efficiency of the electronic device in using the memory objects can be improved, and the performance of the electronic device can be improved.
[0090] It should be pointed out that 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. 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 executes the concurrentcopying GC process, that is, after the electronic device executes the GC process 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 frequency with which the electronic device uses memory object 301a is higher than the frequency with which the electronic device uses memory object 304a; then, after the electronic device performs GC processing on the memory object, 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 can be generated by the GC thread according to the thread's usage of memory objects, or it can 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 may be many other ways to generate GC indications, which are not limited in the embodiments of the present application. Also, for the process of performing reachability analysis on the GC thread, reference may be made to the relevant technology, for example, performing reachability analysis through reference counting. For another example, the 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 may have more implementations, which are not limited in the embodiments of the present application. In some other embodiments, reachability analysis may also be referred to as reachability judgment, reachability test, and the like. Also, in some other embodiments, a memory object passing the reachability analysis may be expressed as a memory object meeting a preset condition, and a memory object failing the reachability analysis may be expressed as a memory object not meeting the preset condition.
[0095] Below, we will use the above Figure 1 In the usage scenario shown, the electronic device is a mobile phone, and the mobile phone 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 operation on an application, a virtual machine of the application is started.
[0098] It should be understood that the above application can be a third-party application or a system application. And the above user's operation of starting the application can be a user's click operation on the application icon or a user's voice command. Specifically, the user's operation of starting the application can be designed according to actual usage requirements, and the embodiments of the present application are not limited to 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 hot spot information of the memory object.
[0102] Among them, the hot spot information of the memory object can be used to represent the frequency of use of the memory object.
[0103] In some embodiments, it is considered that some memory objects may survive for a long time, such as these memory objects may experience multiple GC periods and non-GC periods. At the same time, the frequency of use of these memory objects may vary among the above multiple GC periods and non-GC periods. Therefore, hotspot information may also be time-sensitive, such as being updated according to the GC period. That is, in these embodiments, hotspot information can be used to characterize the frequency of use of memory objects in a non-GC period.
[0104] Exemplarily, 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, in the non-GC period, if a certain memory object is used by a thread for more than or equal to M times, the hotspot flag of the memory object is 1; in the non-GC period, if a certain memory object is used by a thread for 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 in a non-GC period, which may be referred to as the number of times used hereinafter. The memory object being used by a thread may be understood as a thread performing a read (retrieval) operation on the memory object, or a thread performing a write (storage) 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 fetch 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 hotspot information is a hotspot flag as an example to illustrate the technical solution provided by the embodiments of the present application.
[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 since the function of the interpreter is to execute the Java file as a binary file, that is, the interpreter is used to interpret the first code to be executed as the second code. For example, the first code is converted into the second code; wherein the first code can be a Java code, such as a bytecode, and the second code can be a native code, such as a machine code. Therefore, when the Java code runs to the read / write instruction of the memory object, the interpreter can obtain the hotspot information of the memory object. Specifically, this process can be implemented by inserting the first code.
[0111] For example, see Figure 6 When the first code, that is, the Java code, runs to the instruction fetching or writing of the 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. Among them, the above 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 7, the data format of the 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 referred to as a file header. And, in some cases, the above alignment padding area is optional.
[0116] Among them, the file header area includes: lockword and type pointer. The lockword is used to store basic information of memory objects in different states, such as hash information, memory lock related information, forwarding address, etc. The instance data area includes: instance data. Instance data is data stored by memory objects. 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. In the case that the file header area and instance data area of the memory object meet the 8-byte requirement, such as the sum of the file header area and the instance data area is an 8-byte whole byte, then the memory object may not include alignment data. Among them, the forward address can also be called a forwarding pointer, a forwarding address, etc.
[0117] Exemplarily, a lockword may be 32 bits, and basic information of a memory object in different states is stored through changes in each bit.
[0118] For example, see Figure 8 The lock word may include 32 bits, and the bit numbers are from 0 to 31. The 31st bit and the 30th bit record the state of the memory object. For example, 00 records the light lock state, 01 records the heavy lock state, 10 records the hash state, and 11 records the GC state. Among them, the 31st to 30th bits of the lock word can also be called the state bits of the memory object.
[0119] When the memory object is in a light lock state, that is, the 31st to 30th bits of the lockword are 00, the 29th bit records whether the memory object is alive, such as, m value 1 indicates that the memory object is alive, and m value 0 indicates that the memory object is not alive; the 28th bit records the read credentials of the memory object, such as, r value 1 indicates that the memory object can be read, and r value 0 indicates that the memory object cannot be read; the 26th to 16th bits record the light lock count information (lock count), such as the number of times the memory object uses a light lock in binary, and the 15th to 00th bits record the thread ID (thread id owner) holding the light lock. Among them, the 27th bit 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 the 31st to 30th bits of the lockword of a memory object are 00 and the 26th to 00th bits 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 hash state, that is, the 31st to 30th bits of the lockword are 10, the 29th bit records whether the memory object is alive; the 28th bit records the read credentials of the memory object; the 26th to 00th bits are hash information (hash code), such as the hash value of the memory object instance data. Among them, the 27th bit 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 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 for 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. And, 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 can be understood that in some other embodiments, other bits of the lockword of the memory object can also be set as hotspot flags. Specifically, they can be set 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 the above step S501, the interpreter obtains the hotspot flag bit of memory object A as 1, and the interpreter 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 can be understood 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 the memory object is 1, the GC thread copies the memory object to the hot zone of the to partition; if the hotspot flag of the memory object is 0, the GC thread copies the memory object to the cold zone of the to partition. For an introduction to the GC thread copying the memory object 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 also 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 also 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 embodiment of the present application does not limit 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 re-locked state and the memory object 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 area of the to partition.
[0139] For example, see Fig. 9 The hotspot information is a hotspot flag, and 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, for example, the above step S503 is introduced. Fig. 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, execute step S903.
[0145] If the memory object is in a re-locked state, execute step S904.
[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 also clear the from partition.
[0151] Therefore, through the above steps S900-S905, the frequently used memory objects can be copied to the hot area of the to partition, so that the locations of the frequently used memory objects on the to partition are also close, thereby improving the locality of the memory objects after the concurrentcopying GC process. Then, the efficiency of the mobile phone in using memory objects can be improved, and the performance of the mobile phone can be improved.
[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 for a certain memory object, its state 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 the 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 may 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 some other embodiments, the GC thread may also delete the hotspot information of the memory object during the concurrent copying GC process, for example, after the 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 hot spot 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 may be executed in a loop, for example, after step S504, steps S501-S502 are executed in a loop; thereafter, in response to a 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 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 concurrent copying GC and increase GC benefits.
[0161] As a possible implementation method, the GC thread improves the GC efficiency of concurrent copying GC and increases GC benefits through the survival time strategy. In other words, the GC thread can use the CC generational GC method to perform garbage collection.
[0162] In the CC generational GC method, the GC thread can classify memory objects into new generation (new) memory objects and old generation (old) memory objects according to the survival time of the memory objects. For example, if a memory object has undergone (executed) K rounds of GC, the memory object is an old memory object; if the memory object does not survive after K rounds of GC, the memory object is a new memory object, and K is a positive integer. Taking K as 1 as an example, after the first round of GC, memory object A survives, that is, memory object A passes the reachability test. In the first round of GC, memory object A is copied from the from partition to the to partition. In the second round of GC, since memory object A has undergone 1 round of GC, memory object A is an old memory object. If memory object B does not exist in the first round of GC, but exists in the second round of GC, that is, memory object B has not undergone 1 round of GC, then memory object B is a new memory object.
[0163] For old memory objects, the GC thread does not judge their reachability and directly copies them from the from partition to the to partition. For new memory objects, the GC thread judges 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] It is understandable that in the CC generational GC method, for old memory objects, since the GC thread does not perform reachability judgment on them, compared with the concurrent copying GC process, the cost of making reachability judgment on old memory objects can be saved. In this way, the GC efficiency of concurrent copying GC can be improved, that is, the GC benefit can be increased. In other words, when the interpreter obtains the hotspot information of the memory object, the interpreter can obtain the hotspot information of the new memory object.
[0165] As a possible example, during the above step S501, the interpreter can also obtain the survival time of the memory object. That is, 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 will also consume 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 may perform CC generational GC on the memory object according to the hotspot flag of the memory object. Fig.10 Taking the example that 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, the process of CC generational GC of the memory object by the GC thread according to the hotspot flag of the memory object is introduced. Fig.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 may 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, execute step S1005.
[0177] If the memory object is in a re-locked 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 clears the from partition.
[0182] In some other embodiments, the ratio of the partition occupied by the cold zone and the hot zone in the CC generational GC process may be changed in the CC generational GC process. For example, the more old objects there are in 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 to. In this way, the phenomenon of wasting the hot zone due to the hot zone being too large and the phenomenon of insufficient hot zone space due to the hot zone being too small can be alleviated.
[0183] Next, the above steps S1000-S1006 are introduced in conjunction with the change process of the memory object.
[0184] For example, see Fig.11A and Fig. 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 in 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. Fig.11A and Fig. 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: Fig.11A As shown in 1100A-1.
[0186] The first partition includes memory object 901a, memory object 902a, memory object 903a, and memory object 904a. Fig.11AIn the example, the numbers above the memory objects show the status of the memory objects. "00" means light lock status, "10" means heavy lock status, and "10" means hash status. Also, 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. Fig.11A As can be seen, memory object 901a is in a heavily locked state and has no hotspot flag; memory object 902a is in a hash state and the hotspot flag is 0; memory object 903a is in a lightly locked state and the hotspot flag is 0; memory object 904a is in a hash state and the hotspot flag is 1. In addition, 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: Fig.11A As shown in 1100A-2.
[0188] During the process of the GC thread executing steps S1000-S1006 in the first GC period, since memory object 903a did not pass the reachability analysis, the GC thread deleted 903a from the from partition. Also, since memory object 901a is in a re-locked state, the GC thread copied memory object 901a to the hot zone of the to partition, and obtained memory object 901b. Also, since memory object 902a is in a hash lock state and the hot spot flag is 0, the GC thread copied memory object 902a to the cold zone of the to partition, and obtained memory object 902b. Also, since memory object 904a is in a hash lock state and the hot spot flag is 1, the GC thread copied memory object 904a to the hot zone of the to partition, and obtained memory object 904b.
[0189] Then, the GC thread empties the from partition and executes step S503. After the GC thread empties the from partition and executes step S503, the memory of the mobile phone is as follows Fig.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 cyclically. After the interpreter executes step S500 and step S501 cyclically, the memory of the mobile phone is as follows: Fig. 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. Fig. 11B In the example, the numbers above the memory objects show the status of the memory objects, "00" for light lock status, "10" for heavy lock status, and "10" for hash status. 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, the memory object has no hotspot flag.
[0193] During the process of the interpreter looping through steps S500 and S501, since memory object 901c and memory object 904c are old memory objects, the interpreter obtains the hotspot flags of these two memory objects. Fig. 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: Fig. 11B As shown in 1100B-2.
[0195] During the process of the GC thread executing steps S1000-S1006 in the second GC period, 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 to obtain 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 to obtain 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 to obtain 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 to obtain memory object 905d.
[0196] Then, the GC thread empties the from partition and executes step S503. After the GC thread empties the from partition and executes step S503, the memory of the mobile phone is as follows Fig. 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 combination with the algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to be beyond the scope of the present application.
[0199] In this embodiment, the electronic device can be divided into functional modules according to the above method example. For example, each functional module can be divided according to each function, or two or more functions can be integrated into one processing module. The above integrated module can be implemented in the form of hardware. It should be noted that the division of modules in this embodiment is schematic and is only a logical function division. There may be other division methods in actual implementation.
[0200] An embodiment of the present application also provides a computer-readable storage medium, in which a computer program code is stored. 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. When the computer program product is run on an electronic device or a wearable device, the electronic device or the wearable device executes 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 the present 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 only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another 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 separated, 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 different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0206] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[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 part that contributes 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, including several instructions to enable a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.
[0208] The above contents are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application shall be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.
Claims
1. A 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 GC processing on the memory object 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, if the frequency of the memory object is higher 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 lower than the degree threshold, the memory object is copied to the second 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 the frequency threshold includes: 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, writing the hotspot information into a file header of the memory object; The performing GC processing on the memory object corresponding to the target application also includes: If the state of the memory object is a relocked state and the memory object meets the preset condition, the memory object is copied to the first sub-partition.
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, 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 is obtained; M is a positive integer; When the sum of the number of instruction fetches 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 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
Distributed relock downgrade
CN111459462A
Multi-thread synchronization method and electronic equipment
CN111552574A
Garbage collection method and device and computing equipment
CN113360274A
Memory garbage collection method and device, equipment, storage medium and program product
CN115421648A
Object management method and device
CN116737350A
