Garbage collection method, electronic device, and storage medium
By copying memory objects to contiguous regions based on their usage frequency during garbage collection, the problem of low memory utilization in electronic devices is solved, thus improving device performance.
Patent Information
- Application Number
- CN202410176974.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-07
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2044-02-07
AI Technical Summary
In the prior art, after electronic devices perform garbage collection, the utilization efficiency of memory objects is low, which affects device performance.
By copying frequently used memory objects to contiguous areas of memory space based on their frequency of use during garbage collection, memory locality is improved, thereby increasing memory utilization efficiency.
By grouping frequently used memory objects into contiguous regions, the locality of memory objects is improved, thereby enhancing the performance and memory utilization efficiency of electronic devices.
Smart Images

Figure CN119271390B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of terminal technology, and in particular to a waste recycling method, electronic device, and storage medium. Background Technology
[0002] Garbage collection (GC) is a memory management mechanism. It can be understood as the process of deleting memory objects when they are no longer needed, thereby freeing up the memory space they occupy. This storage resource management mechanism is called garbage collection.
[0003] Currently, the memory space of electronic devices can be divided into a first partition and a second partition. Memory objects reside in the second partition. During garbage collection (GC) processing, the electronic device copies memory objects that meet preset conditions from the second partition to the first partition and then clears the second partition. In this way, memory objects that do not meet the preset conditions are deleted, i.e., garbage memory objects are removed.
[0004] At present, how to improve the efficiency of electronic devices in using memory objects after performing garbage collection (GC) is a problem that needs to be solved. Summary of the Invention
[0005] In view of this, embodiments of this application provide a garbage collection method, an electronic device, and a storage medium. In this method, memory objects used by the electronic device with similar frequency of use are located close to each other on the first partition after GC processing. This can improve the locality of memory objects, thereby improving the efficiency of the electronic device in using memory objects and improving the performance of the electronic device.
[0006] To achieve the above objectives, the embodiments of this application adopt the following technical solutions:
[0007] Firstly, this application provides a garbage collection method applicable to electronic devices such as mobile phones and tablets. The memory space of these devices includes a first partition and a second partition. The first partition includes a first subpartition and a second subpartition. The memory addresses of the first subpartition are contiguous, and the first and second partitions are different. The first subpartition and the second subpartition are also different. The method includes: after a target application starts, if the memory size occupied by the target application is greater than or equal to a memory threshold, the electronic device performs garbage collection (GC) on the memory objects corresponding to the target application based on a hotspot information set. The hotspot information set includes multiple hotspot information items, each corresponding to a specific memory object. The hotspot information indicates the frequency with which the target application uses the memory objects corresponding to the hotspot information. The GC process includes: if a memory object meets a preset condition, 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 subpartition, and the second memory object is copied to the second subpartition; the frequency with which the target application uses the first memory object is higher than the frequency with which the target application uses the second memory object. If the memory object does not meet the preset condition, the electronic device deletes the memory object from the second partition. Furthermore, a memory object meeting the preset conditions can be understood as the memory object passing the reachability test, while a memory object not meeting the preset conditions can be understood as the memory object failing the reachability test.
[0008] In the above method, after performing garbage collection (GC) on the memory objects corresponding to the target application, the first and second memory objects reside in different partitions of the electronic device's memory space. For example, the first memory object may be in the first sub-partition, and the second memory object in the second sub-partition. Furthermore, the target application uses the first memory object more frequently than the second. In other words, the memory objects used more frequently by the target application are located in the first sub-partition. Since the memory addresses in the first sub-partition are contiguous, the memory objects within it are located close to each other. Therefore, this method can concentrate the memory objects used more frequently by the target application in the first sub-partition, thereby improving the locality of memory objects and thus increasing the efficiency and performance of the electronic device in using memory objects.
[0009] In one possible design of the first aspect, the electronic device acquires the hotspot information set by: acquiring an instruction for a target memory object; 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 multiple memory objects; and then writing the target hotspot information into the hotspot information set.
[0010] In this design, electronic devices can accurately obtain target hotspot information indicating the frequency with which a target application uses a target memory object, based on instructions directed at the target memory object.
[0011] In another possible design of the first aspect, the aforementioned instructions targeting the target memory object include the address of the target memory object. Furthermore, the electronic device writing the target hotspot information into a hotspot information set includes: the electronic device extracting the address of the target memory object from the instructions targeting the target memory object; next, the electronic device obtaining the position of the target hotspot information in the hotspot information set based on the address of the target memory object; and then, the electronic device writing 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 based on the address of the target memory object, including: the electronic device obtains an offset based on the address of the target memory object. Then, the electronic device obtains the address of the target hotspot information based on the offset and the address of the hotspot information set. Finally, 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, electronic devices can write target hotspot information into a hotspot information set using the address of the target hotspot information.
[0014] In another possible design of the first aspect, the aforementioned instructions targeting the target memory object include: read instructions targeting the target memory object and / or fetch instructions targeting the target memory object. Furthermore, the electronic device obtains target hotspot information corresponding to the target memory object based on the instructions targeting 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 and the number of fetch instructions targeting the target memory object.
[0015] In this design, the electronic device can obtain hotspot information, which indicates the frequency of memory objects used by the target application, based on the sum of the number of fetch and read instructions.
[0016] In another possible design of the first aspect, after the electronic device performs GC processing on memory objects based on the hotspot information set, the method further includes: the electronic device clearing the hotspot information included in the hotspot information set. Next, the electronic device updates the hotspot information included in the hotspot information set. Then, the electronic device performs the next GC processing on the memory objects based on the updated hotspot information set.
[0017] In this design, the electronic device can acquire hotspot information during non-GC periods. Then, during GC periods, the electronic device performs GC processing on memory objects based on the hotspot information acquired during the corresponding non-GC periods. This further improves the locality of memory objects, thereby enhancing the efficiency of the electronic device in using memory objects and improving its performance. Details regarding GC and non-GC periods, and their correspondence, will be provided below and will not be elaborated upon here.
[0018] In another possible design of the first aspect, the electronic device includes an interpreter for interpreting and executing first code to be executed into second code, the first code being different from the second code; for example, the first code may be bytecode, and the second code may be machine code. The first code includes instructions targeting a target memory object. The electronic device acquires the instructions targeting the target memory object and obtains target hotspot information corresponding to the target memory object based on the instructions, including: during the process of the interpreter interpreting and executing the first code into second code, the interpreter of the electronic device acquires the instructions targeting the target memory object and obtains target hotspot information corresponding to the target memory object based on the instructions targeting the target memory object.
[0019] In this design, the interpreter can obtain target hotspot information corresponding to the target memory object while interpreting and executing the first code into the second code. Therefore, electronic devices can efficiently obtain target hotspot information.
[0020] In one possible design of the first aspect, the aforementioned memory objects include either young generation memory objects or old generation memory objects. For example, old generation memory objects are memory objects that have undergone K rounds of GC, where K is a positive integer; young generation memory objects are memory objects other than old generation memory objects. That is, the aforementioned memory objects can be divided into young generation memory objects or old generation memory objects based on their lifespan. Furthermore, the above-mentioned method of copying memory objects from the second partition to the first partition if they meet preset conditions includes: if young generation memory objects meet preset conditions, the electronic device copies young generation memory objects from the second partition to the first partition. The above-mentioned method of deleting memory objects from the second partition if they do not meet preset conditions includes: if young generation memory objects do not meet preset conditions, the electronic device deletes young generation memory objects from the second partition. The above method also includes: the electronic device copies old generation memory objects from the second partition to the first sub-partition.
[0021] In this design, considering that the electronic device's determination of whether a memory object meets the preset conditions also consumes the device's resources, for old generation memory objects, the electronic device can skip the condition check and directly copy the old generation memory to the first sub-partition. This saves the resource consumption of the electronic device in determining whether memory objects meet the preset conditions, and further improves the efficiency of the electronic device in performing GC processing.
[0022] In another possible design of the first aspect, the proportion of the first sub-partition in the first partition is positively correlated with the number of old generation memory objects.
[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 larger the proportion of the first subpartition in the first partition. Conversely, the fewer old generation memory objects there are, the smaller the proportion of the first subpartition in the first partition. This mitigates the problem of wasted space due to an excessively large first subpartition, and the problem of insufficient space due to an excessively small first subpartition.
[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 on the disk of the electronic device or in the internal memory of the electronic device. In this design, the electronic device can use virtual memory technology to write the second subpartition to the disk of the electronic device, thereby saving space in the memory storage of the electronic device.
[0025] In a second aspect, an electronic device is provided, comprising a processor and a memory; the processor and the memory are coupled; the memory is used to store computer program code; the computer program code includes computer instructions, which, when executed by the processor, cause the electronic device to perform the methods provided by the first aspect and any possible design of the first aspect.
[0026] Thirdly, a computer-readable storage medium is provided, comprising computer instructions that, when executed on an electronic device, cause the electronic device to perform the methods provided by the first aspect and any possible design of the first aspect.
[0027] Fourthly, a computer program product is provided, which includes instructions that, when the computer program product is run on an electronic device, cause the electronic device to perform the methods provided by the first aspect and any possible design of the first aspect.
[0028] The technical effects of any of the design methods in the second to fourth aspects can be found in the technical effects of different design methods in the first aspect, and will not be repeated here. Attached Figure Description
[0029] Figure 1 This is a schematic diagram illustrating a usage scenario of an electronic device provided in an embodiment of this application;
[0030] Figure 2 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of this application;
[0031] Figure 3 A schematic diagram of the software architecture of an electronic device provided in an embodiment of this application;
[0032] Figure 4 A schematic diagram of the location of a memory object provided in an embodiment of this application;
[0033] Figure 5 A schematic flowchart of a waste recycling method provided in an embodiment of this application;
[0034] Figure 6 A schematic diagram illustrating the process by which the interpreter provides a set of hotspot information in the embodiments of this application;
[0035] Figure 7 A schematic flowchart illustrating yet another waste recycling method provided in this application embodiment;
[0036] Figure 8 This is yet another schematic diagram illustrating the location of a memory object, provided as an embodiment of this application.
[0037] Figure 9 A schematic flowchart illustrating another waste recycling method provided in this application embodiment;
[0038] Figure 10A Another schematic diagram regarding the location of memory objects provided for embodiments of this application;
[0039] Figure 10B This is yet another schematic diagram of the location of a memory object provided in an embodiment of this application. Detailed Implementation
[0040] The technical solutions of the embodiments of this application will be described below with reference to the accompanying drawings. In the description of this application, unless otherwise stated, " / " indicates that the objects before and after are in an "or" relationship. For example, A / B can represent A or B. "And / or" in this application is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, and B alone, where A and B can be singular or plural. Furthermore, in the description of the embodiments of this application, unless otherwise stated, "multiple" refers to two or more. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, and c can be single or multiple. Furthermore, to facilitate a clear description of the technical solutions in the embodiments of this application, the terms "first" and "second" are used in the embodiments of this application to distinguish identical or similar items with substantially the same function and effect. Those skilled in the art will understand that the terms "first" and "second" do not limit the quantity or execution order, and the terms "first" and "second" are not necessarily different.
[0041] In this application, the terms "exemplary" or "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design described as "exemplary" or "for example" in this application should not be construed as being better or more advantageous than other embodiments or designs. Specifically, the use of terms such as "exemplary" or "for example" is intended to present the relevant concepts in a specific manner to facilitate understanding.
[0042] The technical solutions disclosed in this application involve the collection, storage, use, processing, transmission, provision, and disclosure of users' personal information, all of which comply with relevant laws and regulations and do not violate public order and good morals.
[0043] Garbage collection (GC), also known as memory garbage collection, can be understood as the process of deleting memory objects when they are no longer needed, in order to release the memory space occupied by these objects. This storage resource management mechanism is called garbage collection.
[0044] In some schemes, electronic devices can perform garbage collection using 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 memory objects from the from partition to the to partition based on the reachability of the memory objects. After the memory objects in the from partition are copied, the memory objects in the from partition are cleared; this achieves garbage collection.
[0045] At present, how to improve the efficiency of electronic devices in using memory objects after performing GC processing is a problem that needs to be solved.
[0046] In view of this, embodiments of this application provide a garbage collection method in which an electronic device obtains hotspot information of memory objects, which can characterize the frequency with which the electronic device uses the memory objects. Then, during concurrent copying GC, the electronic device copies the memory objects to the location corresponding to the hotspot information in the to partition based on the hotspot information. The process of concurrent copying GC and the concept of the to partition are explained later and will not be repeated here.
[0047] By employing the methods described above, memory objects with similar hotspot information—that is, memory objects used with similar frequency by the electronic device—are located close to each other on the to partition. This improves the locality of memory objects during concurrent copying GC, thereby enhancing the efficiency of memory object usage by the electronic device and improving its performance.
[0048] Locality, also known as the principle of locality, includes spatial locality and temporal locality. For memory objects, spatial locality can be understood as follows: if a memory object is used by the processor, then memory objects near it are likely to be used by the processor in the future. Similarly, temporal locality can be understood as follows: if a memory object is used by the processor, then it is likely to be used by the processor in the future. Based on the above analysis of spatial and temporal locality, for two memory objects, if their locations are relatively close, the higher their usage frequency, the better their locality. Furthermore, for two memory objects, if their usage frequency is high, the closer their locations, the better their locality. Therefore, the more frequently used memory objects are concentrated in a given area, the better their locality.
[0049] It should be understood that memory objects near a certain memory object can be interpreted as having similar memory addresses.
[0050] Furthermore, because the processor preloads other memory objects near a given memory object when using that object, improving memory object locality can enhance the efficiency of memory object usage in electronic devices, thereby improving the performance of those devices.
[0051] For example, a processor is currently using memory object A, and later it needs to use memory object B. If memory object B and memory object A have similar memory addresses, the processor will preload memory object B when using memory object A. Therefore, the processor's execution efficiency will be higher when using memory object B subsequently. However, if memory object B and memory object A have different memory addresses, the processor cannot preload memory object B when using memory object A. Therefore, the processor will need to load memory object B when using it subsequently, resulting in lower execution efficiency. Thus, the better the locality of memory objects, the higher the processor's efficiency in using memory objects, and the better the performance of electronic devices.
[0052] For example, see Figure 1 The technical solutions provided in this application embodiment can be applied to the process of a user running an application (APP) through an electronic device 100.
[0053] The aforementioned electronic devices can be mobile phones, tablets, smart screens, augmented reality (AR) / virtual reality (VR) devices, laptops, ultra-mobile personal computers (UMPCs), netbooks, personal digital assistants (PDAs), and other electronic devices with memory. They can also be wearable devices with memory, such as smartwatches, smart bracelets, electronic watches, and fitness trackers.
[0054] Next, the hardware structure of the electronic device provided in the embodiments of this application will be described.
[0055] For example, see Figure 2 , Figure 2 A schematic diagram of the structure of the electronic device 100 is shown.
[0056] Electronic device 100 may include processor 110, external memory interface 120, internal memory 121, universal serial bus (USB) interface 130, etc.
[0057] It is understood that the structures illustrated in the embodiments of the present invention do not constitute a specific limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0058] Processor 110 may include one or more processing units, such as: application processor (AP), modem processor, graphics processing unit (GPU), image signal processor (ISP), controller, memory, video codec, digital signal processor (DSP), baseband processor, and / or neural network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.
[0059] The controller can be the nerve center and command center of the electronic device 100. The controller can generate operation control signals according to the instruction opcode and timing signals to complete the control of fetching and executing instructions.
[0060] The processor 110 may also include a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory can store instructions or data that the processor 110 has just used or that are used repeatedly. If the processor 110 needs to use the instruction or data again, it can retrieve it directly from the memory. This avoids repeated accesses, 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. Interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0062] USB port 130 is a USB standard compliant interface, specifically a Mini USB port, Micro USB port, USB Type-C port, etc. USB port 130 can be used to connect a charger to charge electronic device 100, and can also be used for data transfer between electronic device 100 and peripheral devices. It can also be used to connect headphones for audio playback. This interface can also be used to connect other electronic devices, such as AR devices.
[0063] It is understood that the interface connection relationships between the modules illustrated in the embodiments of the present invention are merely illustrative and do not constitute a structural limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may also employ different interface connection methods or combinations of multiple interface connection methods as described in the above embodiments.
[0064] The external storage 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 storage interface 120 to perform data storage functions. For example, music, video, and other files can be saved on the external memory card.
[0065] Internal memory 121 can be used to store computer executable program code, which includes instructions. Processor 110 executes various functional applications and data processing of electronic device 100 by running the instructions stored in internal memory 121. Internal memory 121 may include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback, image playback, etc.), etc. The data storage area may store data created during the use of electronic device 100 (such as audio data, phonebook, etc.). Furthermore, internal memory 121 may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc.
[0066] Next, the software architecture of electronic device 100 will be introduced.
[0067] The software system of electronic device 100 can adopt a layered architecture, event-driven architecture, microkernel architecture, microservice architecture, or cloud architecture. This embodiment of the invention uses a layered architecture of Android. TM Taking the system as an example, the software structure of electronic device 100 is illustrated.
[0068] Figure 3 This is a schematic diagram of the software architecture of an electronic device 100 according to an embodiment of the present invention. A layered architecture divides the software into several (architectural) layers, each with a clear role and division of labor. Layers communicate with each other through software interfaces. In some embodiments, Android can be used... TM The system is divided into four layers, from top to bottom: the application layer (also known as the application (APP) layer), the application framework layer (also known as the framework (FWK)), the native layer, and the 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] Understandable Figure 3 The software architecture shown does not constitute a specific limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may include more or fewer architectural layers than shown, or combine certain architectural layers, or split certain architectural layers, etc. For Android... TM The system can also be divided into other hierarchical structures, such as: application layer (or simply application layer), application framework layer, system libraries, and Android. TMRuntime layer, kernel layer, etc. Furthermore, different operating systems can have different layer divisions; technical personnel can divide the operating system into different layers based on actual usage scenarios.
[0070] The application layer can include a series of application packages. For example... Figure 3 As shown, the application package can include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, and video.
[0071] The application framework layer provides application programming interfaces (APIs) and a programming framework for applications in the application layer. The application framework layer includes predefined functions. For example, the application layer may include system services and media services.
[0072] A system server is a process that provides many subsystem services. Each subsystem service runs as a thread, waiting for requests from applications, processing the requests, and then returning the results to the applications. These subsystem services include, for example, the Window Manager Service (WMS), the Notification Manager Service (NMS), the Activity Manager Service (AMS), and the Input Manager Service (IMS).
[0073] The native layer, also known as the system runtime library layer, can contain multiple functional modules. Examples include: surface manager, media libraries, 3D graphics processing libraries (e.g., OpenGL ES), 2D graphics engines (e.g., SGL), core libraries, and so on.
[0074] The Surface Manager manages the display subsystem and provides fusion of 2D and 3D layers for multiple applications. The Media Library supports playback and recording of various common audio and video formats, as well as still image files. It supports multiple audio and video encoding formats, such as MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG. The 3D Graphics Processing Library implements 3D graphics drawing, image rendering, compositing, and layer processing. The 2D Graphics Engine is the drawing engine for 2D graphics. The core library consists of two parts: one part contains the functionalities called by the Java language, and the other part is the Android core library.
[0075] Additionally, the local layer can also include Android. TM runtime (Android) TM Android runtime (ART) is responsible for Android. TM System scheduling and management. In some examples, ART can also be referred to as Android. TM The runtime virtual machine, or simply the virtual machine, can include a GC thread and an interpreter. The interpreter is used to interpret and execute Java files into binary files. The GC thread is used to perform garbage collection.
[0076] The kernel driver layer, also known as the kernel layer, is the layer between hardware and software. The kernel layer includes at least display drivers, camera drivers, audio drivers, and sensor drivers.
[0077] The garbage collection method provided in this application embodiment will be described below in the software architecture of electronic device 100, combined with the process of electronic device 100 running APP.
[0078] In response to the app's launch instruction, the electronic device starts the virtual machine corresponding to the app. After the virtual machine starts running, it creates multiple threads, which use memory objects to implement their preset functions. Next, during non-GC periods, the interpreter obtains hotspot information of the memory objects, which represents the frequency with which the memory objects are used by threads. Then, during GC periods, the GC thread performs concurrent copying GC on the memory objects based on the hotspot information. In this embodiment, the frequency of memory object usage can be understood as the number of times a memory object is used during a non-GC period. For example, the higher the frequency of memory object usage by the electronic device, the more times the electronic device uses the memory object during a non-GC period. Conversely, the lower the frequency of memory object usage by threads, the fewer times the memory object is used during a non-GC period.
[0079] For example, see Figure 4 , Figure 4 This illustrates the process of the CG thread performing concurrent copying GC on memory objects based on their hotspot information. The memory space available to the virtual machine, that is, the memory space available to threads created by the virtual machine, can be divided into Partition 1 and Partition 2, and memory objects reside on these partitions. Furthermore, Partition 1 can be further divided into Partition 1 hot zone and Partition 1 cold zone; Partition 2 is also divided into Partition 2 hot zone and Partition 2 cold zone. For example, Partition 1 includes memory objects 301a, 302a, 303a, and 304a.
[0080] Next, if the GC thread receives a GC instruction, it performs reachability analysis on memory objects 301a, 302a, 303a, and 304a. If a memory object passes the reachability analysis, it is copied to the second partition; otherwise, it is not copied. Simultaneously, when the GC thread copies memory objects to the second partition, if hotspot information indicates that the electronic device uses the memory object frequently, it is copied to a hot area of the second partition; conversely, if hotspot information indicates that the electronic device uses the memory object infrequently, it is copied to a cold area of the second partition. For example, copying memory object 301a to a hot area of the second partition yields memory object 301b, and copying memory object 302a to a hot area of the second partition yields memory object 302b. Similarly, copying memory object 304a to a cold area of the second partition yields memory object 304b.
[0081] It should be noted that the period from when the GC thread receives the GC instruction until the first partition mentioned above is cleared can be called the GC period. Furthermore, the non-GC period can be understood as the time period during virtual machine operation other than the GC period.
[0082] Furthermore, during GC periods, the "from" partition and "to" partition are relative concepts. For example, in the above... Figure 4 During the illustrated GC period, the GC thread copies memory objects from partition 1 to partition 2. Therefore, during this GC period, partition 1 can also be referred to as the "from" partition, and partition 2 can also be referred to as the "to" partition. Furthermore, if in the next GC period the GC thread copies memory objects from partition 2 to partition 1, then during this GC period, partition 2 can also be referred to as the "from" partition, and partition 1 can also be referred to as the "to" partition. In other embodiments, the "to" partition can also be referred to as the first partition, the "from" partition can also be referred to as the second partition, the hot area of the "to" partition can also be referred to as the first subpartition, and the cold area of the "to" partition can also be referred to as the second subpartition.
[0083] Since the memory objects in the *from* partition are selectively copied to the *to* partition during the above process, those memory objects copied from the *from* partition to the *to* partition can be considered the same memory object in both the *from* and *to* partitions. For example, memory objects 301a and 301b can be considered the same memory object. Similarly, memory objects 304a and 304b can be considered the same memory object.
[0084] The term "frequent use of memory object by the electronic device" can be understood as the frequency of the electronic device's use of the memory object being higher than or equal to a threshold value. For example, within a certain time period (e.g., non-GC period), the number of times the electronic device uses the memory object is greater than or equal to the threshold value. Conversely, "infrequent use of memory object by the electronic device" can be understood as the frequency of the electronic device's use of the memory object being lower than a threshold value. For example, within a certain time period (e.g., non-GC period), the number of times the electronic device uses the memory object is less than the threshold value. The threshold value can be 1 time / time period, 3 times / time period, 4 times / time period, etc. The frequency threshold value can be 1 time, 3 times, 4 times, etc. Specifically, the threshold value and the frequency threshold value can be set according to actual usage needs, and this application embodiment does not limit this.
[0085] Afterwards, the GC thread deletes memory objects in partition 1. Following this process, frequently used memory objects are located in the hot zone of partition 2. Therefore, the locality of memory objects is improved.
[0086] It should be pointed out that, in Figure 4 In the corresponding process, for memory objects 301a and 301b, since the electronic device uses memory object 301a more or less than the threshold number of times, after the electronic device performs a concurrent copying GC, that is, after the electronic device performs GC processing on the memory object, memory object 301b is located in the hot zone of partition 2. And, in Figure 4 During the corresponding process, for memory objects 304a and 304b, since the electronic device uses memory object 304a less often than the threshold, after the electronic device performs the concurrent copying GC process, that is, after the electronic device performs GC processing on the memory object, memory object 304b is located in the cold area of partition 2.
[0087] It should be noted that, Figure 4 During the corresponding process, for memory objects 301b and 304b, the electronic device uses memory object 301a more frequently than memory object 304a. Therefore, after the electronic device performs GC processing on the memory objects, memory object 301b is located in the hot zone of the second partition, and 304b is located in the cold zone of the second partition.
[0088] It should be understood that the aforementioned GC indication can be generated by the GC thread based on 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, the GC thread generates a GC indication when the available memory of the phone is less than or equal to memory threshold A. Or, for another example, the memory monitoring module sends a GC indication to the GC thread when the memory occupied by the application is greater than or equal to memory threshold B. Here, memory thresholds A and B can both be 32MB, 128MB, etc. In some embodiments, memory thresholds A and B can change according to the application's running status during application operation. Specifically, they can be set according to actual usage needs.
[0089] This understanding shows that electronic devices respond to GC instructions and perform GC processing; 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 are many other ways to generate GC indicators, and this application embodiment does not limit this. Furthermore, the process of GC thread performing reachability analysis can be found in related technologies, such as reachingability analysis through reference counting. Another example is reachingability analysis by determining whether a memory object is referenced through its root node. In other examples, the reachingability analysis process can have many more implementations, and this application embodiment does not impose any limitations on this. In other embodiments, reachingability analysis can also be referred to as reachingability judgment, reachingability test, etc. And in other embodiments, a memory object passing reachingability analysis can be described as meeting preset conditions, and a memory object failing reachingability analysis can be described as not meeting preset conditions.
[0091] Below, we will use the above... Figure 1 In the illustrated use case, the electronic device is a mobile phone, and the mobile phone has the above-mentioned features. Figure 2 The hardware structure shown, and Figure 3 Taking the illustrated software architecture as an example, the technical solutions provided in the embodiments of this application will be introduced.
[0092] For example, see Figure 5 The waste recycling method provided in this application embodiment may include steps S500-S503.
[0093] S500. In response to the user's startup operation on the application, the virtual machine of the application starts.
[0094] It should be understood that the aforementioned applications can be third-party applications or system applications. Furthermore, the user's operation to launch the application can be a click on the application icon or a voice command. Specifically, the user's operation to launch the application can be designed according to actual usage needs, and this application embodiment does not limit this.
[0095] It should be noted that the virtual machine's GC thread and the virtual machine's interpreter are also in the startup state after the virtual machine starts.
[0096] In some embodiments, in response to a user's launch operation on an application, the phone generates a launch instruction for the application. Then, in response to the launch instruction, the application's virtual machine starts.
[0097] S501. The interpreter obtains a set of hotspot information for memory objects.
[0098] In some embodiments, the hotspot information set can be a bitmap data structure, which can be deployed in the phone's memory space. The bitmap uses each bit to represent a certain state. The hotspot information set can include hotspot information, which can 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. Furthermore, in some other embodiments, the aforementioned hotspot information set may also be in other forms, such as a file; this application does not limit this.
[0100] In some implementations, considering that certain memory objects may have a relatively long lifespan, such as surviving multiple GC and non-GC periods, and that the frequency of use of these memory objects may differ across these multiple GC and non-GC periods, hotspot information can also be time-sensitive, such as being updated according to GC periods. That is, in these implementations, hotspot information can be used to characterize the frequency of memory object use within a non-GC period.
[0101] For example, hotspot information may include a hotspot flag. If a memory object is frequently used by threads during non-GC periods, the hotspot flag corresponding to that memory object is 1; if a memory object is not frequently used by threads during non-GC periods, the hotspot flag corresponding to that memory object is 0.
[0102] For example, during non-GC periods, if a memory object is used by a thread more than or equal to M times, then the hotspot flag corresponding to that memory object is set to the first value; during non-GC periods, if a memory object is used by a thread less than M times, then the hotspot flag corresponding to that memory object is set to the second value. Here, M is a positive integer. The values of the first and second values can be set according to actual usage requirements. For example, the first value can be 1, and the second value can be 0; this embodiment of the application does not limit this.
[0103] For example, hotspot information may also include the number of times a memory object is used by threads during a non-GC period, which will be referred to as the usage count below. Here, the use of a memory object by a thread can be understood as a thread performing a read (fetch) operation on the memory object or a thread performing a write (storage) operation on the memory object.
[0104] For example, during a non-GC period, if a memory object is read once by a thread, then the memory object is used 1 time; during a non-GC period, if a memory object is fetched 4 times and written 2 times by a thread, then the memory object is used 6 times.
[0105] In the following embodiments of this application, the technical solution provided by the embodiments of this application will be described using the hotspot information being a hotspot flag bit as an example.
[0106] As one possible implementation, the interpreter can generate a set of hotspot information for memory objects based on fetch / write instructions for memory objects.
[0107] It should be understood that since the function of an interpreter is to execute a Java file into a binary file, that is, the interpreter is used to interpret and execute the first piece of code to be executed into second code. For example, it converts the first piece of code into the second piece of code; where the first piece of code can be Java code, such as bytecode, and the second piece of code can be native code, such as machine code. Therefore, when the Java code runs to a fetch / write instruction on a memory object, the interpreter can generate a set of hotspot information for that memory object. Specifically, this process can be achieved by instrumenting the first piece of code.
[0108] For example, see Figure 6 When the interpreter executes the first code (i.e., the Java code) to a fetch or write instruction to a memory object, it generates a hotspot information set for that memory object, where the hotspot flag is set to 1. The interpreter then interprets and executes the first code as the second code.
[0109] For example, if the instructions iget-object, aget-object, sget-object, iput-object, aput-object, and sput-object appear in the first code, the hotspot flag corresponding to the memory object will be 1. Here, iget, aget, sget, iput, aput, and sput all represent instruction names, and object represents the address of the memory object pointed to by that instruction.
[0110] Understandably, there can be multiple memory objects; therefore, a mobile phone can record the hotspot information of multiple memory objects using a hotspot information set. Alternatively, a mobile phone can also record the hotspot information of a single memory object using a hotspot information set.
[0111] For example, 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, memory object B, and memory object C through hotspot information set Z. Alternatively, the mobile phone can record the hotspot information of memory object A through hotspot information set A, and record the hotspot information of memory object B and memory object C through hotspot information set B. Or, the mobile phone can record the hotspot information of memory object A through hotspot information set C, record the hotspot information of memory object B through hotspot information set D, and record the hotspot information of memory object C through hotspot information set E.
[0112] It should be understood that there are many other ways to record hot information of memory objects in the hot information set, and this application embodiment does not impose any restrictions on this.
[0113] The following section will introduce hotspot information sets by taking the example of recording hotspot information of multiple memory objects through a hotspot information set.
[0114] As one possible implementation, if a hotspot information set does not exist in the virtual machine, the interpreter creates one. For example, the interpreter creates a hotspot information set in memory. It should be understood that after the virtual machine starts, a hotspot information set may not exist in the virtual machine, therefore the interpreter needs to create one; if a hotspot information set exists in the virtual machine, the interpreter can generate a hotspot information set for memory objects based on fetch / write instructions for memory objects.
[0115] For example, the interpreter can obtain the maximum number of memory objects and create a hotspot information set based on this number. Considering that the number of memory objects may change during the virtual machine's operation, the interpreter can create the hotspot information set based on the maximum number of memory objects, and this 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 will still contain a hotspot flag corresponding to that memory object. That is, for each memory object, the hotspot information set contains a corresponding hotspot flag. A detailed description of the relationship between the hotspot flags in the hotspot information set and the memory objects will be provided below and will not be repeated here.
[0117] It should be noted that during the process of the interpreter creating the hotspot information set, the hotspot flag of the hotspot information set can be set to the second value. The maximum number of memory objects mentioned above can be represented by the phone's heap size parameter. 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 virtual machine's operation.
[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 bit corresponding to the memory object in the hotspot information set based on the fetch / write instructions for the memory object.
[0119] It should be understood that, as described above, the fetch / write instructions for memory objects include the address of the memory object. Therefore, the interpreter can perform address translation using the address of the memory object to obtain the position of the hotspot flag bit corresponding to the memory object in the hotspot information set, and write the first value to that position.
[0120] For example, the process of address translation of a memory object can be performed using 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 can also be referred to as the offset. The hotspot flag address can represent the position of the hotspot flag corresponding to the memory object in the hotspot information set.
[0123] S502. In response to a GC instruction, the GC thread performs concurrent copying GC on memory objects based on a hotspot information set.
[0124] In some embodiments, if the hot spot flag corresponding to the memory object is 1, the GC thread copies the memory object to the hot area of the to partition; if the hot spot flag corresponding to the memory object is 0, the GC thread copies the memory object to the cold area of the to partition.
[0125] It should be noted that the physical address of the hot zone is contiguous, and it can be deployed in the phone's internal memory. The physical address of the cold zone can also be contiguous, and it can be deployed in the phone's RAM or external storage, such as a hard disk. Specifically, for the cold and hot zones in the first partition, the ratio of their occupancy in the first partition can be 5:5, or it can be 3:7, 4:6, etc. For the cold and hot zones in the second partition, the ratio of their occupancy in the second partition can be the same as or different from that in the first partition; this embodiment does not impose such limitations.
[0126] It should be understood that for cold storage areas, mobile phones can use virtual memory technology to set the cold storage areas on the phone's hard drive, thereby saving space in the phone's internal storage.
[0127] For example, see Figure 7 Step S502 above may include steps S700-S704.
[0128] S700. In response to GC instructions, the GC thread performs reachability analysis on memory objects.
[0129] If the memory object fails the reachability analysis, proceed to step S701; if the memory object passes the reachability analysis, proceed to step S702.
[0130] The S701.GC thread removes the memory object from the from partition.
[0131] The S702.GC thread retrieves the hotspot flag corresponding to the memory object.
[0132] If the hotspot flag is value 1, proceed to step S703; if the hotspot flag is value 2, proceed to step S704.
[0133] The S703.GC thread copies the memory object to the hot sector of the to partition.
[0134] The S704.GC thread copies the memory object to the cold sector of the to partition.
[0135] In some embodiments, step S701 may be executed after step S703 described above. Alternatively, after steps S700-S704 have been executed for each memory object, the GC thread may also clear the from partition. In this way, the GC thread performs the concurrent copying GC process.
[0136] Therefore, through steps S700-S704, frequently used memory objects can be copied to the hot area of the to partition. This ensures that frequently used memory objects are located close together on the to partition, thereby improving the locality of memory objects during the concurrent copying GC process. This, in turn, improves the processor efficiency and overall performance of electronic devices.
[0137] Next, after step S502, or steps S700-S704, the GC thread executes step S503.
[0138] S503.GC thread clears the hotspot information set.
[0139] It should be understood that when the GC thread clears the hotspot information set, it can be interpreted as the GC thread setting each hotspot flag in the hotspot information set to its second value. Therefore, in the next non-GC period, the interpreter can use the hotspot flag corresponding to the memory object written to the hotspot information set.
[0140] The following section will describe steps S500-S503 in conjunction with the process of memory object changes.
[0141] For example, see Figure 8 In response to the user's application launch command, the application's virtual machine starts. After the virtual machine starts, the interpreter obtains a hotspot information set 800 based on fetch / write instructions for memory objects; that is, the interpreter obtains a hotspot information set 800 for memory objects. The hotspot information set 800 includes N bits, where N is the maximum number of memory objects mentioned above.
[0142] exist Figure 8 In this 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 Partition 1 and Partition 2, and memory objects reside on these partitions. Furthermore, Partition 2 can be further divided into a hot zone and a 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 8Memory objects 801a, 802a, 803a, and 804a are all located in partition 1. Specifically, the hotspot flag for memory object 801a is bit 00 of hotspot information set 800; the hotspot flag for memory object 802a is bit 01 of hotspot information set 800; the hotspot flag for memory object 803a is bit 02 of hotspot information set 800; and the hotspot flag for memory object 804a is bit 03 of hotspot information set 800.
[0144] Next, in response to the GC instruction, the GC thread performs concurrent copying GC on the memory objects based on the hotspot information set. That is, the GC thread executes steps S700-S704 above on the memory objects.
[0145] During this process, based on the fact that memory object 801a passes the reachability test and its corresponding hotspot flag is 1, the GC thread copies memory object 801a from the from partition to the hotspot of the to partition, which is the hotspot of the second partition, thus obtaining memory object 801b. The GC thread then deletes memory object 801a from the from partition.
[0146] During this process, since memory object 802a failed the reachability test, the GC thread will delete memory object 802a from the from partition, that is, from the first partition.
[0147] During this process, based on the fact that memory object 803a passes the reachability test and its corresponding hotspot flag is 1, the GC thread copies memory object 803a from the from partition to the hotspot of the to partition, which is the hotspot of the second partition, thus obtaining memory object 803b. The GC thread then deletes memory object 803a from the from partition.
[0148] During this process, based on the fact that memory object 804a passes the reachability test and its corresponding hotspot flag is 0, memory object 804a is copied from the from partition to the hotspot of the to partition, which is the hotspot of the second partition, resulting in memory object 804b. The GC thread then deletes memory object 804a from the from partition.
[0149] In this way, frequently used memory objects 801a and 803a can be grouped together in the to partition after GC. This improves the locality of memory objects and increases the efficiency of electronic devices in using memory objects.
[0150] Next, after performing steps S700-S704 on memory objects 801a, 802a, 803a, and 804a, the GC thread clears the hotspot information set 800, resulting in hotspot information set 805. In subsequent processes, the interpreter can then write the hotspot flag corresponding to the memory object into hotspot information set 805.
[0151] In some other embodiments, during step S502 above, the GC thread may also adopt one or more of the following strategies during the concurrent copying GC process: mobility partitioning strategy, object size partitioning strategy, and lifespan strategy, to improve the GC efficiency of concurrent copying GC and increase GC benefits.
[0152] As one possible implementation, the GC thread can improve the efficiency of concurrent copying GC and increase GC benefits through a lifespan strategy. In other words, the GC thread can use a parallel copying generational garbage collection (CCgenerational GC) method for garbage collection.
[0153] In the generational garbage collection (CC) method, the GC thread can classify memory objects into new and old generation memory objects based on their lifespan. For example, if a memory object has survived K rounds of GC, it is an old memory object; if it has not survived K rounds of GC, it is a new memory object, where K is a positive integer. Taking K=1 as an example, after the first round of GC, memory object A survives, meaning it passes the reachability test and is copied from the from partition to the to partition during the first round of GC. In the second round of GC, since memory object A has survived one round of GC, it is an old memory object. If memory object B did not exist in the first round of GC but exists in the second round, meaning it did not survive one round of GC, then it is a new memory object.
[0154] For old memory objects, the GC thread does not check their reachability and directly copies them from the from partition to the to partition; for new memory objects, the GC thread checks the reachability of the new memory objects and copies them from the from partition to the to partition based on the reachability of the new memory objects.
[0155] Understandably, in the CC generational GC method, for old memory objects, since the GC thread does not perform reachability checks, the overhead of performing reachability checks on old memory objects can be saved compared to the concurrent copying GC process. This improves the efficiency of concurrent copying GC, thus increasing GC benefits. In other words, when the interpreter obtains hotspot information for memory objects, it can also obtain hotspot information for newly created memory objects.
[0156] As a possible example, during step S501 above, the interpreter can also obtain the lifetime 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] Understandably, the interpreter consumes electronic device resources when retrieving the hotspot information set of memory objects. Therefore, not retrieving the hotspot information set for old memory objects can further conserve electronic device resources.
[0158] In some implementations, the GC thread can perform CC generational GC on memory objects based on the hotspot flags corresponding to those objects.
[0159] For example, see Figure 9 Taking the example of a hotspot flag of 1 indicating frequent use of memory objects and a hotspot flag of 0 indicating infrequent use of memory objects, this section describes the process by which the GC thread performs CC generational GC on memory objects based on their corresponding hotspot flags. Figure 9 As shown, the process may include steps S900-S905.
[0160] The S900.GC thread retrieves the lifetime of each memory object.
[0161] It should be understood that the above step S900 can also be described 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, then proceed to step S901; if the memory object is a new memory object, then proceed to step S902.
[0163] The S901.GC thread copies the memory object to the hot sector of the to partition.
[0164] The S902.GC thread performs a reachability check on the memory object.
[0165] If the memory object fails the reachability test, proceed to step S903; if the memory object passes the reachability test, proceed to step S904.
[0166] The S903.GC thread removes the memory object from the from partition.
[0167] The S904.GC thread retrieves the hotspot flag corresponding to the memory object.
[0168] If the hotspot flag corresponding to the memory object is 1, then execute step S901 above; if the hotspot flag corresponding to the memory object is 0, then execute step S905.
[0169] The S905.GC thread copies the memory object to the cold sector of the to partition.
[0170] Optionally, in some examples, the GC thread may also execute step S903 after step S905 or step S901. Alternatively, after the GC thread has executed steps S900-S905 for each memory object, the GC thread may clear the from partition.
[0171] In other examples, the ratio of cold and hot zones in a CC generational GC can vary throughout the GC process. For instance, the more old objects there are during a CC generational GC, the larger the proportion occupied by hot zones. In other words, the more old generation memory objects there are, the larger the proportion of hot zones in the `to` partition. Conversely, the fewer old generation memory objects there are, the smaller the proportion of hot zones in the `to` partition. This mitigates the problem of wasted hot zones due to excessively large hot zones, and the problem of insufficient hot zone space due to excessively small hot zones.
[0172] The following section describes the process by which the GC thread performs CC generational GC on memory objects based on the hotspot flags corresponding to those objects, in conjunction with the changes in memory objects and hotspot information sets.
[0173] For example, see Figure 10A In response to the user's startup command for the application, the application's virtual machine starts. After the virtual machine starts, the interpreter obtains a hotspot information set 1000 based on fetch / write instructions for memory objects. In other words, the interpreter obtains a hotspot information set 1000 for memory objects. This hotspot information set 1000 includes N bits, where N is the maximum number of memory objects mentioned above.
[0174] The memory space usable by the virtual machine, that is, the memory space usable by the virtual machine's threads, can be divided into Partition 1 and Partition 2, and memory objects reside on these partitions. Furthermore, Partition 1 can be further divided into a hot zone and a cold zone; Partition 2 is also divided into a hot zone and a cold zone. Figure 10A During the corresponding process, the proportion of the zone occupied by the hot zone remains unchanged.
[0175] For example, see Figure 10A Memory objects 1001a, 1002a, 1003a, and 1004a are all located in partition 1, and these memory objects are newly created memory objects. Specifically, the hotspot flag for memory object 1001a is bit 00 of hotspot information set 1000; the hotspot flag for memory object 1002a is bit 01 of hotspot information set 1000; the hotspot flag for memory object 1003a is bit 02 of hotspot information set 1000; and the hotspot flag for memory object 1004a is bit 03 of hotspot information set 1000. It should be understood that... Figure 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 CCgenerational GC on the memory objects based on the hotspot information set. That is, the GC thread executes the above steps S900-S905 on the memory objects.
[0177] During this process, based on the fact that memory object 1001a passes the reachability test and its corresponding hotspot flag is 1, the GC thread copies memory object 1001a from the from partition to the hotspot of the to partition, which is the hotspot of the second partition, thus obtaining memory object 1001b. Then, the GC thread deletes memory object 1001a from the from partition.
[0178] During this process, since memory object 1002a failed the reachability test, the GC thread will delete memory object 1002a from the from partition, that is, from the first partition.
[0179] During this process, based on the fact that memory object 1003a passes the reachability test and its corresponding hotspot flag is 1, the GC thread copies memory object 1003a from the from partition to the hotspot of the to partition, which is the hotspot of the second partition, resulting in memory object 1003b. Then, the GC thread deletes memory object 1003a from the from partition.
[0180] During this process, based on the fact that memory object 1004a passes the reachability test and its corresponding hotspot flag is 0, memory object 1004a is copied from the from partition to the hotspot of the to partition, which is the hotspot of the second partition, resulting in memory object 1004b. Then, the GC thread deletes memory object 1004a from the from partition.
[0181] In this way, frequently used memory objects 1001a and 1003a can be grouped together in the to partition after GC. This improves the locality of memory objects and increases the efficiency of the phone in using memory objects.
[0182] After the GC thread performs steps S900-S905 on each memory object, it clears the memory objects in the from partition. Additionally, the GC thread clears hotspot information set 1000, resulting in hotspot information set 1010.
[0183] See afterward. Figure 10B Memory objects 1001c, 1005c, 1006c, and 1007c are all located in the from partition. Memory object 1001c is an old memory object, while memory objects 1005c, 1006c, and 1007c are new memory objects. The interpreter obtains hotspot information set 1020 based on fetch / write instructions for the memory objects. In other words, the interpreter obtains hotspot information set 1020 for memory objects.
[0184] Among them, memory object 1001c corresponds to the 00th bit of hotspot information set 1020, memory object 1005c corresponds to the 01st bit of hotspot information set 1020, memory object 1006c corresponds to the 02nd bit of hotspot information set 1020, and memory object 1007c corresponds to the 03rd bit of hotspot information set 1020.
[0185] Next, in response to the GC instruction, the GC thread performs CCgenerational GC on the memory objects based on the hotspot information set. That is, the GC thread executes the above steps S900-S905 on the memory objects.
[0186] During 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 sector of the to partition, obtaining memory object 1001d. Then, the GC thread deletes memory object 1001c from the from partition.
[0187] During this process, since memory object 1005c failed the reachability test, the GC thread will delete memory object 1005c from the from partition, that is, from the first partition.
[0188] During this process, since memory object 1006c failed the reachability test, the GC thread will delete memory object 1006c from the from partition, that is, from the first partition.
[0189] During this process, based on the fact that memory object 1007c passes the reachability test and its corresponding hotspot flag is 1, the GC thread copies memory object 1007c from the from partition to the hotspot of the to partition, resulting in memory object 1007d. Then, the GC thread deletes memory object 1007c from the from partition.
[0190] After the GC thread performs steps S900-S905 on each memory object, it clears the memory objects in the from partition. Additionally, the GC thread clears hotspot information set 1010, resulting in 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 individual consent has been obtained, including but not limited to notifying and reminding users to read the relevant user agreement (notification) and sign the agreement (authorization) which includes authorization of relevant user information before users use the function.
[0192] Based on the algorithmic steps of the various examples described in the embodiments disclosed herein, this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by 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 conjunction with the embodiments, but such implementations should not be considered beyond the scope of this application.
[0193] This embodiment can divide the electronic device into functional modules according to the above method example. For example, each function can be divided into its own functional modules, or two or more functions can be integrated into one processing module. The integrated modules can be implemented in hardware. It should be noted that the module division in this embodiment is illustrative and only represents one logical functional division. In actual implementation, there may be other division methods.
[0194] This application also provides a computer-readable storage medium storing computer program code. When the processor executes the computer program code, the electronic device or wearable device performs the relevant method steps in the above method embodiments.
[0195] This application also provides a computer program product that, when run on an electronic device or wearable device, causes the electronic device or wearable device to execute the relevant method steps described in the above method embodiments.
[0196] The electronic devices, wearable devices, computer-readable storage media, or computer program products provided in this application are all used to perform the corresponding methods provided above. Therefore, the beneficial effects they can achieve can be referred to the beneficial effects in the corresponding methods provided above, and will not be repeated here.
[0197] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual applications, the above 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 this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0199] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0200] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0201] If the integrated unit is implemented as 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 embodiments of this application, in essence, or the part that contributes, or all or part of the technical solution, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0202] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A waste recycling method, characterized in that, The method is applied to an electronic device, which includes an interpreter for interpreting and executing first code to be executed into second code, the first code being different from the second code; the first code includes instructions for a corresponding target memory object, the target memory object being one of multiple memory objects corresponding to a target application; the memory space of the electronic device includes a first partition and a second partition, the first partition including a first sub-partition and a second sub-partition, the memory addresses of the first sub-partition being contiguous, the first partition and the second partition being different, and the first sub-partition being different from the second sub-partition; the method includes: After the target application starts, during the process of interpreting the first code into the second code, the interpreter obtains instructions for the target memory object, and updates the target hotspot information corresponding to the target memory object based on the target memory object parsed from the instructions. The hotspot information set includes multiple hotspot information, which correspond one-to-one with multiple memory objects. The hotspot information indicates the frequency with which the target application uses the memory object corresponding to the hotspot information, and the target hotspot information corresponds to the target memory object. If the memory size occupied by the target application is greater than or equal to the memory threshold, garbage collection (GC) is performed on the memory objects corresponding to the target application based on the hotspot information set; the memory objects include young generation memory objects or old generation memory objects; the old generation memory objects are memory objects that have undergone K rounds of GC processing, where K is a positive integer, and the young generation memory objects are memory objects other than old generation memory objects; The GC process includes: If the memory object meets the preset conditions, 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; the target application uses the first memory object more frequently than the target application uses the second memory object more frequently. The step of copying the memory object from the second partition to the first partition if the memory object meets the preset conditions includes: If the young generation memory object meets the preset conditions, then the young generation memory object is copied from the second partition to the first partition; The step of deleting the memory object from the second partition if the memory object does not meet the preset conditions includes: If the young generation memory object does not meet the preset conditions, the young generation memory object will be deleted from the second partition; The old generation memory object is copied from the second partition to the first subpartition, and the old generation memory object is deleted from the second partition.
2. The method according to claim 1, characterized in that, The instructions targeting the target memory object include the address of the target memory object; updating the target hotspot information corresponding to the target memory object includes: Extract the address of the target memory object from the instructions targeting the target memory object; The location of the target hotspot information in the hotspot information set is obtained based on the address of the target memory object; Write the target hotspot information into the position of the target hotspot information in the hotspot information set.
3. The method according to claim 2, characterized in that, The step of obtaining the position of the target hotspot information in the hotspot information set based on the address of the target memory object includes: The offset is obtained based on the address of the target memory object, and the address of the target hotspot information is obtained based on the offset and the address of the hotspot information set; The step of writing the target hotspot information to the position of the target hotspot information in the hotspot information set includes: Write the target hotspot information into the address of the target hotspot information.
4. The method according to any one of claims 1-3, characterized in that, The instructions targeting the target memory object include: read instructions targeting the target memory object and / or fetch instructions targeting the target memory object; Before updating the target hotspot information corresponding to the target memory object, the method further includes: The target hotspot information corresponding to the target memory object is obtained based on the number of read instructions and the number of fetch instructions for the target memory object.
5. The method according to any one of claims 1-3, characterized in that, After performing garbage collection (GC) on the memory objects 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; Update the hotspot information set to include the hotspot information; The next GC process is performed on the memory object based on the updated hotspot information set.
6. The method according to claim 1, characterized in that, The proportion of the first sub-partition in the first partition is positively correlated with the number of old generation memory objects.
7. The method according to any one of claims 1-3, 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.
8. 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, which, when executed by the processor, cause the electronic device to perform the method as described in any one of claims 1-7.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes computer instructions that, when executed on an electronic device, cause the electronic device to perform the method as described in any one of claims 1-7.
10. A computer program product, characterized in that, The computer program product includes instructions that, when the computer program product is run on an electronic device, cause the electronic device to perform the method as described in any one of claims 1-7.
Citation Information
Patent Citations
Object management method and device
CN116737350A
Method for controlling memory and electronic equipment
CN116820734A