Garbage collection method, electronic equipment and storage medium
By using a collection of hot spot information in electronic devices to guide the garbage collection process, copying frequently used memory objects to a continuous area of memory addresses, solving the problem of low efficiency in memory objects after garbage collection by electronic devices and improving performance.
Patent Information
- Application Number
- CN202410176974.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-07
- Publication Date
- 2025-01-07
- 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 collection of hot spot information in electronic devices, based on the frequent use of memory objects, the memory objects are copied to the first subpartition of the continuous memory address during garbage collection, thereby improving the locality of memory objects and improving the performance of electronic devices. .
By concentrating frequently used memory objects in a continuous area of memory addresses, the locality of memory objects is improved, thereby improving the memory usage efficiency and performance of electronic devices.
Smart Images

Figure CN119271390A_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, an embodiment of the present application provides a garbage collection method, an electronic device and a storage medium. In the method, the electronic device uses memory objects with similar frequency, and the locations of these memory objects on the first partition are also close after GC processing, 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.
[0006] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[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 above 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 based on the hotspot information set; the hotspot information set includes multiple hotspot information, and the multiple hotspot information corresponds to the multiple memory objects one by one, and the hotspot information indicates the frequency of the target application using the memory object corresponding to the hotspot information. Among them, the above GC processing includes: if the memory object meets the preset conditions, the electronic device copies the memory object from the second partition to the first partition; wherein the first memory object is copied to the first sub-partition, and the second memory object is copied to the second sub-partition; the frequency of the target application using the first memory object is higher than the frequency of the target application using the second memory object. If the memory object does not meet the preset conditions, the electronic device deletes the memory object from the second partition. Furthermore, the fact that a memory object meets the preset conditions can be understood as the memory object passing the reachability test, and the fact that a memory object does not meet the preset conditions can be understood as the memory object failing the reachability test.
[0008] In the above method, after performing GC processing on the memory objects corresponding to the target application, the first memory object and the second memory object are in different partitions of the memory space of the electronic device, such as the first memory object is in the first sub-partition and the second memory object is in the second sub-partition; and the frequency of the target application using the first memory object is higher than the frequency of the target application using the second memory object. In other words, the memory objects that are frequently used by the target application are on 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 the memory objects that are frequently used by the target application 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.
[0009] In a possible design of the first aspect, the electronic device obtains a hotspot information set, including: the electronic device obtains an instruction for a target memory object, and obtains target hotspot information corresponding to the target memory object based on the instruction for the target memory object, where the target memory object is any one of a plurality of memory objects. Next, the electronic device writes the target hotspot information into the hotspot information set.
[0010] In this design, the electronic device can accurately obtain target hotspot information indicating the frequency of use of the target memory object by the target application according to the instruction for the target memory object.
[0011] In another possible design of the first aspect, the instruction for the target memory object includes the address of the target memory object. And the electronic device writes the target hotspot information into the hotspot information set, including: the electronic device extracts the address of the target memory object from the instruction for the target memory object. Next, the electronic device obtains the position of the target hotspot information in the hotspot information set according to the address of the target memory object. Afterwards, the electronic device writes the target hotspot information into the position of the target hotspot information in the hotspot information set.
[0012] In another possible design of the first aspect, the electronic device obtains the position of the target hotspot information in the hotspot information set according to the address of the target memory object, including: the electronic device obtains the offset according to the address of the target memory object. Afterwards, the electronic device obtains the address of the target hotspot information based on the offset and the address of the hotspot information set. And, the electronic device writes the target hotspot information to the position of the target hotspot information in the hotspot information set, including: the electronic device writes the target hotspot information to the address of the target hotspot information.
[0013] In this design, the electronic device can write the target hotspot information into the hotspot information set through the address of the target hotspot information.
[0014] In another possible design of the first aspect, the above-mentioned instruction for the target memory object includes: a read instruction for the target memory object and / or a fetch instruction for the target memory object. And, the above-mentioned electronic device obtains the target hotspot information corresponding to the target memory object based on the instruction for the target memory object, including: the electronic device obtains the target hotspot information corresponding to the target memory object based on the number of read instructions for the target memory object and the number of fetch instructions for the target memory object.
[0015] In this design, the electronic device can obtain hotspot information indicating the frequency of the target application using the memory object corresponding to the hotspot information according to the sum of the number of instruction fetches and read instructions.
[0016] In another possible design of the first aspect, after the electronic device performs GC processing on the memory object based on the hotspot information set, the method further includes: the electronic device clears the hotspot information included in the hotspot information set. Next, the electronic device updates the hotspot information included in the hotspot information set. Afterwards, the electronic device performs the next GC processing on the memory object based on the updated hotspot information set.
[0017] In this design, the electronic device can obtain the hotspot information of the electronic device during the non-GC period. And, during the GC period, the electronic device performs GC processing on the memory object according to the hotspot information obtained during the non-GC period corresponding to the GC period. In this way, the locality of the memory object can be further improved, so the efficiency of the electronic device in using the memory object can be further improved, and the performance of the electronic device can be improved. Among them, the correspondence between the above-mentioned GC period and non-GC period, and the GC period and non-GC period can be seen below, which will not be described in detail here.
[0018] In another possible design of the first aspect, the electronic device includes an interpreter, and the interpreter is used to interpret and execute a first code to be executed into a second code, the first code is different from the second code, for example, the first code may be a bytecode, and the second code may be a machine code; the first code includes instructions for a target memory object. The electronic device obtains instructions for the target memory object, and obtains target hotspot information corresponding to the target memory object based on the instructions for the target memory object, including: in the process of the interpreter interpreting and executing the first code into the second code, the interpreter of the electronic device obtains instructions for the target memory object, and obtains target hotspot information corresponding to the target memory object based on the instructions for the target memory object.
[0019] In this design, the interpreter can obtain the target hotspot information corresponding to the target memory object in the process of interpreting and executing the first code into the second code. Thus, the electronic device can obtain the target hotspot information efficiently.
[0020] In a possible design of the first aspect, the above-mentioned memory objects include new generation memory objects or old generation memory objects, such as old generation memory objects are memory objects that have undergone K rounds of GC processes, K is a positive integer; new generation memory objects are memory objects other than old generation memory objects. That is to say, 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. And, if the memory object meets the preset conditions, the electronic device copies the memory object from the second partition to the first partition, including: if 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. If the memory object does not meet the preset conditions, the electronic device deletes the memory object from the second partition, including: if 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 method also includes: the electronic device copies the old generation memory object from the second partition to the first sub-partition.
[0021] 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.
[0022] 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.
[0023] 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.
[0024] 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 disk of the electronic device or in the internal memory 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.
[0025] 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.
[0026] 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.
[0027] 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.
[0028] 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
[0029] Figure 1 A schematic diagram of a usage scenario of an electronic device provided in an embodiment of the present application;
[0030] Figure 2 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application;
[0031] Figure 3 A schematic diagram of a software architecture of an electronic device provided in an embodiment of the present application;
[0032] Figure 4 A schematic diagram of the location of a memory object provided in an embodiment of the present application;
[0033] Figure 5 A schematic diagram of a garbage collection method provided in an embodiment of the present application;
[0034] Figure 6 A schematic diagram of a process for generating a hotspot information set by an interpreter provided in an embodiment of the present application;
[0035] Figure 7 A flowchart of another garbage collection method provided in an embodiment of the present application;
[0036] Figure 8 Another schematic diagram of the location of a memory object provided in an embodiment of the present application;
[0037] Fig. 9 A schematic diagram of another garbage collection method provided in an embodiment of the present application;
[0038] Fig. 10A Another schematic diagram of the location of a memory object provided in an embodiment of the present application;
[0039] Fig. 10B Another schematic diagram regarding the location of a memory object provided for an embodiment of the present application. DETAILED DESCRIPTION
[0040] 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.
[0041] 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.
[0042] 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.
[0043] 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.
[0044] 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.
[0045] At present, how to improve the efficiency of using memory objects by electronic devices after performing GC processing is a problem to be solved.
[0046] 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.
[0047] 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.
[0048] 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.
[0049] 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.
[0050] 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.
[0051] 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.
[0052] For example, see Figure 1 The 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.
[0053] 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.
[0054] Next, the hardware structure of the electronic device provided in the embodiment of the present application is introduced.
[0055] For example, see Figure 2 , Figure 2 A schematic structural diagram of an electronic device 100 is shown.
[0056] 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.
[0057] 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.
[0058] 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.
[0059] 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.
[0060] 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.
[0061] 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.
[0062] 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.
[0063] 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.
[0064] 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.
[0065] 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.
[0066] Next, the software architecture of the electronic device 100 is introduced.
[0067] 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.
[0068] 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 3 As shown, the electronic device 100 may include an application layer, a framework layer, a local layer and a kernel layer.
[0069] 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 TMRuntime 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.
[0070] 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.
[0071] 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.
[0072] 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.
[0073] 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.
[0074] 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.
[0075] 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.
[0076] 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.
[0077] 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.
[0078] In response to the start-up instruction of the APP, the electronic device starts the virtual machine corresponding to the APP. After the virtual machine is running, the virtual machine will create multiple threads, and the threads use memory objects to implement the preset functions of the threads. Next, in the non-GC period, the interpreter obtains the hotspot information of the memory object, and the hotspot information represents the frequency of the memory object being used by the thread. Afterwards, in the GC period, the GC thread performs concurrentcopying GC on the memory object according to the hotspot information of the memory object. Among them, in the embodiment of the present application, the frequency of the memory object can be understood as the number of times the memory object is used in a non-GC period. For example, the more frequently the electronic device uses the memory object, the more times the electronic device uses the memory object in a non-GC period. For another example, the less frequently the memory object is used by the thread, the less times the memory object is used by the thread in a non-GC period.
[0079] 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.
[0080] 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.
[0081] 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.
[0082] 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.
[0083] 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.
[0084] 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.
[0085] After that, the GC thread deletes the memory objects in the first partition. After this process, since the memory objects frequently used by the electronic device are all located in the hot zone of the second partition, the locality of the memory objects can be improved.
[0086] 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.
[0087] 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.
[0088] 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.
[0089] 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.
[0090] 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.
[0091] 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.
[0092] For example, see Figure 5 The garbage collection method provided in the embodiment of the present application may include steps S500-S503.
[0093] S500. In response to a user's start operation on an application, a virtual machine of the application is started.
[0094] 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.
[0095] 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.
[0096] 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.
[0097] S501. The interpreter obtains a set of hotspot information of a memory object.
[0098] In some embodiments, the hotspot information set may be a bitmap data structure, which may be deployed in the memory space of the mobile phone, and the bitmap uses each bit to represent a certain state. The hotspot information set may include hotspot information, which may be used to characterize the frequency of use of memory objects.
[0099] In other embodiments, the hotspot information set may also be referred to as a hotspot bitmap, a hotspot flag set, a hotspot field set, etc. Also, in some other embodiments, the hotspot information set may also be in other forms, such as a file, which is not limited in the embodiments of the present application.
[0100] 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.
[0101] 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 corresponding to the memory object is 1; if a memory object is not frequently used by a thread during a non-GC period, the hotspot flag corresponding to the memory object is 0.
[0102] For example, in a non-GC period, if a certain memory object is used by a thread for more than or equal to M times, then the hotspot flag corresponding to the memory object is the first value; in a non-GC period, if a certain memory object is used by a thread for less than M times, then the hotspot flag corresponding to the memory object is the second value. Wherein, M is a positive integer. The values of the first value and the second value can be set according to actual usage requirements. For example, the first value can be 1, and the second value can be 0. The embodiments of the present application do not limit this.
[0103] 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.
[0104] 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.
[0105] 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.
[0106] As a possible implementation, the interpreter may generate a hotspot information set of the memory object based on fetch / write instructions for the memory object.
[0107] 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 generate a hot spot information set of the memory object. Specifically, this process can be achieved by inserting the first code.
[0108] For example, see Figure 6 , the interpreter can generate a hotspot information set of the memory object when the first code, that is, the Java code, runs to the instruction fetch or write instruction of the memory object, and the hotspot flag bit in the hotspot information set is 1. And, the interpreter interprets and executes the first code into the second code.
[0109] 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 corresponding to 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.
[0110] It is understandable that there may be multiple memory objects, so the mobile phone can record the hotspot information of multiple memory objects through one hotspot information set. Alternatively, the mobile phone can also record the hotspot information of one memory object through one hotspot information set.
[0111] For example, the memory objects include memory object A, memory object B, and memory object C. The mobile phone can record the hotspot information of memory object A, the hotspot information of memory object B, and the hotspot information of memory object C through hotspot information set Z. Alternatively, the mobile phone can also record the hotspot information of memory object A through hotspot information set A, and the hotspot information set B can record the hotspot information of memory object B and the hotspot information of memory object C. Alternatively, the mobile phone can record the hotspot information of memory object A through hotspot information set C, the hotspot information of memory object B through hotspot information set D, and the hotspot information of memory object C through hotspot information set E.
[0112] It should be understood that there may be more designs for the hotspot information set to record the hotspot information of the memory object, and the embodiments of the present application do not impose any limitation on this.
[0113] The following introduces the hotspot information set by taking the example of recording the hotspot information of multiple memory objects through one hotspot information set.
[0114] As a possible implementation, when the hotspot information set does not exist in the virtual machine, the interpreter creates the hotspot information set. For example, the interpreter creates the hotspot information set in the memory. It should be understood that after the virtual machine is started, the hotspot information set may not exist in the virtual machine, so the interpreter needs to create the hotspot information set; if the hotspot information set exists in the virtual machine, the interpreter can generate the hotspot information set of the memory object based on the fetch / write instruction for the memory object.
[0115] Exemplarily, the interpreter may obtain the maximum number of memory objects and create a hotspot information set based on the maximum number of memory objects. Considering that the number of memory objects may change during the operation of the virtual machine, the interpreter may create a hotspot information set based on the maximum number of memory objects, and the created hotspot information set includes hotspot flags corresponding to the maximum number of memory objects.
[0116] In this way, even if the number of memory objects changes, the hotspot information set still has a hotspot flag corresponding to the memory object. That is to say, for each memory object, there is a hotspot flag corresponding to the memory object in the hotspot information set. The description of the relationship between the hotspot flag in the hotspot information set and the memory object is described below, which will not be repeated here.
[0117] It should be noted that in the process of the interpreter creating a hotspot information set, the hotspot flag of the hotspot information set can be set to the second value. Among them, the above-mentioned maximum number of memory objects can be represented by the heap size parameter of the mobile phone. It should be understood that the heap size parameter is used to represent the maximum number of memory objects allowed to be used during the operation of the virtual machine.
[0118] As another possible implementation, after the interpreter creates a hotspot information set, that is, when the interpreter has a hotspot information set, the interpreter can write the first value into the hotspot flag corresponding to the memory object in the hotspot information set based on the fetch / write instruction for the memory object.
[0119] It should be understood that, from the above introduction, the fetch / write instruction for the memory object includes the address of the memory object. Therefore, the interpreter can perform address conversion through the address of the memory object, obtain the position of the hotspot flag corresponding to the memory object in the hotspot information set, and write the first value to the position.
[0120] Exemplarily, the process of performing address conversion on the address of a memory object may be performed by the following expression 1.
[0121] Hotspot flag address = hotspot information set address + memory object address / compression ratio expression 1
[0122] The memory object address / compression ratio may also be referred to as an offset. The hotspot flag address may represent the position of the hotspot flag corresponding to the memory object in the hotspot information set.
[0123] S502. In response to the GC instruction, the GC thread performs concurrentcopying GC on the memory object according to the hot spot information set.
[0124] In some embodiments, if the hotspot flag corresponding to the memory object is 1, the GC thread copies the memory object to the hot zone of the to partition; if the hotspot flag corresponding to the memory object is 0, the GC thread copies the memory object to the cold zone of the to partition.
[0125] 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.
[0126] 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.
[0127] For example, see Figure 7 The above step S502 may include steps S700-S704.
[0128] S700. In response to a GC instruction, the GC thread performs a reachability analysis on the memory object.
[0129] If the memory object fails the reachability analysis, step S701 is executed; if the memory object passes the reachability analysis, step S702 is executed.
[0130] S701. The GC thread deletes the memory object from the from partition.
[0131] S702. The GC thread obtains the hotspot flag corresponding to the memory object.
[0132] If the hotspot flag is the first value, execute step S703; if the hotspot flag is the second value, execute step S704.
[0133] S703. The GC thread copies the memory object to the hot zone of the to partition.
[0134] S704. The GC thread copies the memory object to the cold area of the to partition.
[0135] In some embodiments, after the above step S703, step S701 may be further executed. Alternatively, after executing steps S700-S704 for each memory object, the GC thread may also clear the from partition. In this way, the GC thread executes the concurrent copying GC process.
[0136] Thus, through the above steps S700-S704, 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 close, thereby improving the locality of the memory objects in the concurrent copying GC process. Then, the execution efficiency of the processor of the electronic device can be improved, and the performance of the electronic device can be improved.
[0137] Next, after step S502 or steps S700 - S704 , the GC thread executes step S503 .
[0138] S503. The GC thread clears the hotspot information collection.
[0139] It should be understood that the GC thread clears the hotspot information set, which can be understood as the GC thread setting each hotspot flag included in the hotspot information set to the second value. Thus, in the next non-GC period, the interpreter can write the hotspot flag corresponding to the memory object in the hotspot information set.
[0140] Next, the above steps S500-S503 are introduced in conjunction with the process of memory object change.
[0141] For example, see Figure 8 In response to the user's start operation on the application, the virtual machine of the application is started. After the virtual machine is started, the interpreter obtains the hotspot information set 800 based on the fetch / write instruction for the memory object, that is, the interpreter obtains the hotspot information set 800 of the memory object. Among them, the hotspot information set 800 includes N bits, and N is the above-mentioned maximum number of memory objects.
[0142] exist Figure 8 In the corresponding process, 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 second partition can be divided into the second partition hot zone and the second partition cold zone. It should be understood that in Figure 8 In the corresponding process, the first partition is the from partition and the second partition is the to partition.
[0143] For example, see Figure 8, memory objects 801a, 802a, 803a and 804a are all located in partition 1. The hotspot flag bit corresponding to memory object 801a is bit 00 of hotspot information set 800; the hotspot flag bit corresponding to memory object 802a is bit 01 of hotspot information set 800; the hotspot flag bit corresponding to memory object 803a is bit 02 of hotspot information set 800; and the hotspot flag bit corresponding to memory object 804a is bit 03 of hotspot information set 800.
[0144] Next, in response to the GC instruction, the GC thread performs concurrentcopying GC on the memory object according to the hotspot information set. That is, the GC thread executes the above steps S700-S704 on the memory object.
[0145] In this process, based on the fact that memory object 801a passes the reachability test and the hotspot flag corresponding to memory object 801a is 1, the GC thread copies memory object 801a from the from partition to the hot zone of the to partition, that is, the hot zone of the second partition, to obtain memory object 801b. The GC thread also deletes memory object 801a from the from partition.
[0146] In this process, since the memory object 802a fails the reachability test, the GC thread deletes the memory object 802a from the from partition, that is, from the first partition.
[0147] In this process, based on the fact that memory object 803a passes the reachability test and the hotspot flag corresponding to memory object 803a is 1, the GC thread copies memory object 803a from the from partition to the hot zone of the to partition, that is, the hot zone of the second partition, to obtain memory object 803b. The GC thread also deletes memory object 803a from the from partition.
[0148] In this process, based on the fact that memory object 804a passes the reachability test and the hotspot flag corresponding to memory object 804a is 0, memory object 804a is copied from the from partition to the hot zone of the to partition, that is, the hot zone of the second partition, to obtain memory object 804b. The GC thread deletes memory object 804a from the from partition.
[0149] In this way, the frequently used memory objects 801a and 803a can be gathered in the toth partition after GC. Thus, the locality of the memory objects can be improved, and the efficiency of the electronic device in using the memory objects can be improved.
[0150] Next, after executing the above steps S700-S704 for memory object 801a, memory object 802a, memory object 803a and memory object 804a, the GC thread clears the above hotspot information set 800 to obtain hotspot information set 805. In this way, in the subsequent process, the interpreter can write the hotspot flag corresponding to the memory object in hotspot information set 805.
[0151] In some other embodiments, during the above step S502, 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.
[0152] 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.
[0153] 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.
[0154] 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.
[0155] 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.
[0156] 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 set. If the memory object is a new memory object, the interpreter obtains its hotspot information set.
[0157] It is understandable that when the interpreter obtains the hotspot information set of the memory object, it will also consume the resources of the electronic device. Therefore, for the old memory object, not obtaining its hotspot information set can further save the resources of the electronic device.
[0158] In some implementations, the GC thread may perform CC generational GC on the memory object according to the hotspot flag corresponding to the memory object.
[0159] For example, see Fig. 9 Taking the hotspot flag bit being 1 indicating that the memory object is frequently used and the hotspot flag bit being 0 indicating that the memory object is not frequently used as an example, the process of CC generational GC of the memory object by the GC thread according to the hotspot flag bit corresponding to the memory object is introduced. Fig. 9 As shown, the process may include steps S900-S905.
[0160] S900.GC thread obtains the survival time of each memory object.
[0161] It should be understood that the above step S900 may also be expressed as the GC thread determining whether the memory object is an old memory object or a new memory object.
[0162] If the memory object is an old memory object, execute step S901; if the memory object is a new memory object, execute step S902.
[0163] S901. The GC thread copies the memory object to the hot zone of the to partition.
[0164] S902. The GC thread determines the reachability of the memory object.
[0165] If the memory object fails the reachability check, step S903 is executed; if the memory object passes the reachability check, step S904 is executed.
[0166] S903. The GC thread deletes the memory object from the from partition.
[0167] S904. The GC thread obtains the hotspot flag corresponding to the memory object.
[0168] If the hotspot flag corresponding to the memory object is 1, execute the above step S901; if the hotspot flag corresponding to the memory object is 0, execute step S905.
[0169] S905. The GC thread copies the memory object to the cold area of the to partition.
[0170] Optionally, in some examples, the GC thread may further perform the above step S903 after step S905 or step S901. Alternatively, after the GC thread performs steps S900-S905 on each memory object, the GC thread clears the from partition.
[0171] In some other examples, the ratio of the partitions occupied by cold areas and hot areas during CC generational GC can change during CC generational GC. For example, the more old objects there are during CC generational GC, the greater the ratio of hot areas. In other words, the more old generation memory objects there are, the greater the ratio of hot areas in the to partition. The fewer old generation memory objects there are, the smaller the ratio of hot areas in to. In this way, the phenomenon of hot areas being wasted due to being too large and the phenomenon of insufficient hot area space due to being too small can be alleviated.
[0172] Next, in combination with the change process of the memory objects and the hot spot information set, the process in which the GC thread can perform CC generational GC on the memory objects according to the hot spot flag bits corresponding to the memory objects is introduced.
[0173] For example, see Fig. 10A In response to the user's start operation on the application, the virtual machine of the application is started. After the virtual machine is started, the interpreter obtains the hotspot information set 1000 based on the fetch / write instruction for the memory object. In other words, the interpreter obtains the hotspot information set 1000 of the memory object. The hotspot information set 1000 includes N bits, where N is the maximum number of memory objects.
[0174] The memory space available to the virtual machine, that is, the memory space available to the threads of 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. 10A In the corresponding process, the proportion of the partition occupied by the hot zone remains unchanged.
[0175] For example, see Fig. 10A , memory objects 1001a, 1002a, 1003a and 1004a are all located in the first partition, and these memory objects are new memory objects. Among them, the hotspot flag corresponding to memory object 1001a is the 00th bit of hotspot information set 1000; the hotspot flag corresponding to memory object 1002a is the 01st bit of hotspot information set 1000; the hotspot flag corresponding to memory object 1003a is the 02nd bit of hotspot information set 1000; the hotspot flag corresponding to memory object 1004a is the 03rd bit of hotspot information set 1000. It should be understood that in Fig. 10A In the corresponding process, the first partition is the from partition and the second partition is the to partition.
[0176] Next, in response to the GC instruction, the GC thread performs CC generational GC on the memory object according to the hot spot information set. That is, the GC thread executes the above steps S900-S905 on the memory object.
[0177] In this process, based on the fact that memory object 1001a passes the reachability test and the hotspot flag corresponding to memory object 1001a is 1, the GC thread copies memory object 1001a from the from partition to the hot zone of the to partition, that is, the hot zone of the second partition, to obtain memory object 1001b. In addition, the GC thread deletes memory object 1001a from the from partition.
[0178] During this process, since the memory object 1002a fails the reachability test, the GC thread deletes the memory object 1002a from the from partition, that is, from the first partition.
[0179] In this process, based on the fact that memory object 1003a passes the reachability test and the hotspot flag corresponding to memory object 1003a is 1, the GC thread copies memory object 1003a from the from partition to the hot zone of the to partition, that is, the hot zone of the second partition, to obtain memory object 1003b. In addition, the GC thread deletes memory object 1003a from the from partition.
[0180] In this process, based on the fact that memory object 1004a passes the reachability test and the hotspot flag corresponding to memory object 1004a is 0, memory object 1004a is copied from the from partition to the hot zone of the to partition, that is, the hot zone of the second partition, to obtain memory object 1004b. In addition, the GC thread deletes memory object 1004a from the from partition.
[0181] In this way, the frequently used memory objects 1001a and 1003a can be gathered in the toth partition after GC, thereby improving the locality of the memory objects and improving the efficiency of the mobile phone in using the memory objects.
[0182] After the GC thread executes the above steps S900 to S905 for each memory object, the GC thread clears the memory objects in the from partition. Also, the GC thread clears the hotspot information set 1000 to obtain the hotspot information set 1010.
[0183] Afterwards, see Fig. 10B , memory object 1001c, memory object 1005c, memory object 1006c and memory object 1007c are all located in the from partition, and memory object 1001c is an old memory object, and memory object 1005c, memory object 1006c and memory object 1007c are new memory objects. The interpreter obtains hotspot information set 1020 based on the fetch / write instructions for the memory objects. In other words, the interpreter obtains hotspot information set 1020 of the memory objects.
[0184] Among them, memory object 1001c corresponds to the 00th bit of hot information set 1020, memory object 1005c corresponds to the 01st bit of hot information set 1020, memory object 1006c corresponds to the 02nd bit of hot information set 1020, and memory object 1007c corresponds to the 03rd bit of hot information set 1020.
[0185] Next, in response to the GC instruction, the GC thread performs CC generational GC on the memory object according to the hot spot information set. That is, the GC thread executes the above steps S900-S905 on the memory object.
[0186] In this process, since memory object 1001c is an old memory object, the GC thread copies memory object 1001c from the from partition to the hot zone of the to partition to obtain memory object 1001d. In addition, the GC thread deletes memory object 1001c from the from partition.
[0187] During this process, since the memory object 1005c fails the reachability test, the GC thread deletes the memory object 1005c from the from partition, that is, from the first partition.
[0188] During this process, since the memory object 1006c fails the reachability test, the GC thread deletes the memory object 1006c from the from partition, that is, from the first partition.
[0189] In this process, based on the fact that memory object 1007c passes the reachability test and the hotspot flag corresponding to memory object 1007c is 1, the GC thread copies memory object 1007c from the from partition to the hotspot of the to partition to obtain memory object 1007d. In addition, the GC thread deletes memory object 1007c from the from partition.
[0190] After the GC thread executes the above steps S900 to S905 for each memory object, the GC thread clears the memory objects in the from partition. Also, the GC thread clears the hotspot information set 1010 to obtain the hotspot information set 1030.
[0191] 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.
[0192] 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.
[0193] 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.
[0194] 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.
[0195] 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.
[0196] 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.
[0197] 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.
[0198] 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.
[0199] 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.
[0200] 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.
[0201] 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.
[0202] 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 and the second partition are different, 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 object corresponding to the target application based on the hotspot information set; the hotspot information set includes multiple hotspot information, the multiple hotspot information corresponds to multiple memory objects one by one, and the hotspot information indicates the frequency of the target application using the memory object corresponding to the hotspot information; The GC treatment includes: If the memory object meets the preset condition, the memory object is copied from the second partition to the first partition; wherein the first memory object is copied to the first sub-partition, and the second memory object is copied to the second sub-partition; and the frequency of use of the first memory object by the target application is higher than the frequency of use of the second memory object by the target application; 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 method further comprises: Obtaining an instruction for a target memory object, and obtaining target hotspot information corresponding to the target memory object based on the instruction for the target memory object, wherein the target memory object is any one of the multiple memory objects; The target hotspot information is written into the hotspot information set.
3. The method according to claim 2, characterized in that The instruction for the target memory object includes the address of the target memory object; and writing the target hotspot information into the hotspot information set includes: Extracting the address of the target memory object from the instruction for the target memory object; Obtaining the position of the target hotspot information in the hotspot information set according to the address of the target memory object; The target hotspot information is written into the position of the target hotspot information in the hotspot information set.
4. The method according to claim 3, characterized in that The obtaining the position of the target hotspot information in the hotspot information set according to the address of the target memory object includes: Obtaining an offset according to the address of the target memory object, and obtaining an address of the target hotspot information based on the offset and the address of the hotspot information set; The step of writing the target hotspot information into the position of the target hotspot information in the hotspot information set includes: The target hotspot information is written into the address of the target hotspot information.
5. The method according to any one of claims 2 to 4, characterized in that: The instructions for the target memory object include: a read instruction for the target memory object and / or a fetch instruction for the target memory object; The obtaining target hotspot information corresponding to the target memory object based on the instruction for the target memory object includes: The target hotspot information corresponding to the target memory object is obtained based on the number of read instructions for the target memory object and the number of fetch instructions for the target memory object.
6. The method according to any one of claims 2 to 5, characterized in that: After performing GC processing on the memory object corresponding to the target application based on the hotspot information set, the method further includes: Clear the hotspot information included in the hotspot information set; Updating the hotspot information included in the hotspot information set; The next GC process is performed on the memory object based on the updated hot spot information set.
7. The method according to claim 2, characterized in that The electronic device comprises an interpreter, the interpreter is used to interpret and execute a first code to be executed into a second code, the first code is different from the second code; the first code comprises the instruction for the target memory object; The step of acquiring an instruction for a target memory object and obtaining target hotspot information corresponding to the target memory object based on the instruction for the target memory object includes: In the process of interpreting and executing the first code into the second code, the interpreter obtains the instruction for the target memory object, and obtains the target hotspot information corresponding to the target memory object based on the instruction for the target memory object.
8. The method according to any one of claims 1 to 7, 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; If the memory object meets the preset condition, copying the memory object from the second partition to the first partition comprises: If the new generation memory object meets the preset condition, copying the new generation memory object from the second partition to the first partition; If the memory object does not meet the preset condition, deleting the memory object from the second partition includes: If the new generation memory object does not meet the preset condition, deleting the new generation memory object from the second partition; The GC treatment further comprises: The old generation memory objects are copied from the second partition to the first sub-partition.
9. The method according to claim 8, 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.
10. The method according to any one of claims 1 to 9, 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 disk of the electronic device or in the internal memory of the electronic device.
11. 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 described in any one of claims 1-10.
12. 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 10.
13. 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 executes the method according to any one of claims 1 to 10.
Citation Information
Patent Citations
Data storage method, device and system
CN110968253A
Data cache optimization method and device, nonvolatile storage medium and electronic equipment
CN114297099A
Object management method and device
CN116737350A
Method for controlling memory and electronic equipment
CN116820734A
Adaptive type-partitioned garbage collection
US7155467B1