Garbage collection method and device
By scanning and clearing garbage objects when an activity exits, and combining the cross-activity table to record cross-activity reference relationships, the time-consuming problem of long-lifecycle objects in ART is solved, the garbage collection efficiency and memory management efficiency are improved, the frequency of Full GC is reduced, and the user experience is improved.
Patent Information
- Application Number
- CN202510216072.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-26
- Publication Date
- 2025-09-05
- Estimated Expiration
- 2045-02-26
AI Technical Summary
In the existing technology, the garbage collector in the Android Runtime (ART) takes a long time to process long-lifecycle objects, resulting in frequent Full GC, which affects memory management efficiency and user experience.
By scanning and clearing garbage objects in the young generation area and the old generation area of the activity based on the reference relationship of the root object when the activity exits, and moving surviving objects to the old generation area, and combining the cross-activity table to record the cross-activity reference relationship, the frequency of Full GC and global traversal are reduced.
It improves the efficiency of garbage collection, reduces the frequency of Full GC startup, shortens application pause time, and improves memory management efficiency and user experience.
Smart Images

Figure CN119718679B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a garbage collection method and device. Background Art
[0002] Electronic devices typically use the garbage collector in the Android Runtime (ART) to manage the memory areas occupied by applications. Currently, the garbage collector in ART employs two garbage collection mechanisms: young garbage collection (Young GC) and full garbage collection (Full GC). Young GC reclaims short-lived garbage objects, while Full GC reclaims long-lived garbage objects through global lookups. Young GC is less time-consuming, while Full GC is more time-consuming.
[0003] As applications run longer, the memory area allocated by electronic devices for applications is used more and more, and Full GC is started more frequently and takes more time. Therefore, the overall efficiency of memory management of third-party applications is currently low. Summary of the Invention
[0004] Embodiments of the present invention provide a garbage collection method and apparatus for improving the overall efficiency of memory management of an electronic device.
[0005] The first aspect of the present application provides a garbage collection method, which can be applied to electronic devices. Unless otherwise specified, the "electronic device" in the implementation manner of the present application can refer to the electronic device itself, or a component in the electronic device (for example, a processor, a chip, or a chip system, etc.), or a logical module or software that can realize all or part of the functions of the electronic device.
[0006] The method includes: in response to the exit of the first activity among n activities, the electronic device can scan the young generation area in the memory area and the second area corresponding to the first activity starting from each root object according to the reference relationship of each root object, and obtain and clear garbage objects, where n is a natural number greater than 1. Among them, the memory area can be divided into a young generation area, an old generation area, and n areas corresponding to n activities. The young generation area is used to store objects when n activities are created, and the old generation area is used to store surviving objects after garbage collection. Among the n areas, at least one first area corresponding to a main page can be included, and the area corresponding to each activity overlaps with the old generation area and the young generation area respectively. That is, the area corresponding to each activity includes an active young generation area that overlaps with the young generation area, and an active old generation area that overlaps with the old generation area.
[0007] It can be understood that the memory area in the embodiment of the present application is the memory area occupied by the application. The above-mentioned garbage collection method can promptly clear the garbage objects in the area corresponding to the exited activity and the young generation area each time the activity exits. Compared with Full GC's global traversal and removal of garbage objects in the old generation area and the young generation area, this method only performs garbage cleaning on the young generation area and the active old generation area in the area corresponding to the exited activity, which takes less time. Moreover, in the existing garbage collection method, garbage objects with a long life cycle in the old generation area can only be cleaned up by FullGC, while this method can timely clear the garbage objects in the area corresponding to the exited activity and the area overlapping with the old generation area, which can reduce the startup frequency of Full GC and improve the overall garbage collection efficiency and memory management efficiency.
[0008] In a possible implementation of the first aspect above, the method further includes: obtaining surviving objects in the young generation area and the second area, and moving the surviving objects in the active young generation area and the active old generation area in the second area, and the active young generation area in the first area, to the active old generation area in the first area.
[0009] It can be understood that in the above-mentioned garbage collection method, after the first activity exits, it switches to the main page. After obtaining the surviving objects in the recycling area (such as the young generation area and the second area), the electronic device can move the surviving objects in the active young generation area and the active old generation area in the second area, and the active young generation area in the first area, to the active old generation area in the first area, and release the area corresponding to the surviving objects. Moreover, after these surviving objects become garbage objects, they can be cleared by Full GC; or they can be cleared by starting the above-mentioned garbage collection method after the main page exits. Compared with the garbage collection mechanism that only includes Young GC and Full GC, the above-mentioned garbage collection method can cooperate with Young GC and Full GC to jointly perform garbage collection, reduce the startup frequency of Full GC, and make the overall garbage collection efficiency higher.
[0010] In a possible implementation of the first aspect above, the root objects include: root objects with cross-activity reference relationships, root objects in the run stack, root objects in the native method stack, root objects in static variables, and root objects in the Android card table.
[0011] It is understood that the Android card table is the ART card table in the Android system. The root objects with cross-activity reference relationships can be the root objects with cross-activity reference relationships stored in the cross-activity table below. This is used to quickly find surviving objects with cross-activity reference relationships, improving the accuracy and efficiency of garbage collection after the first activity exits.
[0012] In a possible implementation of the first aspect above, live objects include objects that have a direct reference relationship and an indirect reference relationship with the root object; and garbage objects include objects that have neither a direct reference relationship nor an indirect reference relationship with the root object.
[0013] It is understood that live objects and garbage objects can be searched in the reclaim area based on reference relationships, starting from the root object. During the search, objects that are accessible from the root object and in the reclaim area are considered live objects. Objects that are not accessible from the root object and in the reclaim area are considered garbage objects.
[0014] In a possible implementation of the first aspect, the n regions corresponding to the n activities are determined based on the activities to which the objects belong and the regions occupied by the objects in the memory area.
[0015] It can be understood that in some embodiments, the memory area is divided into areas corresponding to each activity, and the area occupied by each object is marked according to the activity to which the object belongs. The area corresponding to each activity can be accurately divided in the memory area, so that the recycling area can be accurately determined.
[0016] In a possible implementation of the first aspect above, the n regions corresponding to the n activities are divided based on the activities to which the objects in the third region belong. The third region is a subset of the young generation region or the old generation region. The young generation region and the old generation region respectively include multiple third regions of the same size.
[0017] It can be understood that in some embodiments, the memory area is divided into an old generation area and a young generation area, and the old generation area and the young generation area each include multiple sub-areas of the same size, that is, the third area. According to the activities to which the objects stored in each third area belong, each third area is marked as the third area corresponding to each activity, and finally the memory area can be accurately marked as each area corresponding to each activity, so that the recycling area can be accurately determined. Moreover, compared with the method of marking each object, marking according to each third area has fewer marking times and occupies less memory, thereby achieving the purpose of reducing memory overhead.
[0018] In a possible implementation of the first aspect above, the method further includes: switching to a second activity among the n activities.
[0019] It is understood that in some embodiments, if there is a first activity and a main page, then when the first activity is exited, it will switch to the main page. If there is a first activity, a main page, and a second activity, when the first activity is exited, it can switch to the second activity.
[0020] In a possible implementation of the first aspect above, the method further includes: moving surviving objects in an active young generation area in a fourth area corresponding to the second activity to an active old generation area in the fourth area.
[0021] It's understandable that when the first activity exits and switches to the second activity, surviving objects in the active young generation area of the fourth region corresponding to the second activity can be moved to the active old generation area of the fourth region, freeing up the area corresponding to the surviving objects. Furthermore, after these surviving objects become garbage objects, they can be cleared through Full GC or by initiating the aforementioned garbage collection method after the second activity exits, resulting in higher overall garbage collection efficiency.
[0022] The second aspect of the present application provides an electronic device, including a processor and an interface circuit, the interface circuit being used to receive signals from other communication devices and transmit them to the processor or to send signals from the processor to other communication devices, and the processor implementing any one of the methods in the above-mentioned first aspect through logic circuits or executing code instructions.
[0023] A third aspect of the present application provides a program product, which includes instructions. When the instructions are executed on an electronic device, the electronic device implements any one of the methods in the first aspect.
[0024] A fourth aspect of the present application provides a communication device, which includes a module for executing any one of the methods in the first aspect.
[0025] The fifth aspect of the present application provides a computer-readable storage medium having a computer program or instruction stored thereon. When the computer program or instruction is executed by a communication device, any one of the methods in the first aspect described above is implemented.
[0026] The sixth aspect of the present application provides a chip, which includes a processor coupled to a memory and configured to execute a computer program or instruction stored in the memory to implement any one of the methods in the first aspect above.
[0027] The seventh aspect of the present application provides a system, which includes a memory for storing instructions executed by one or more processors, and a processor, which is one of the processors of the system, for calling instructions to execute any one of the methods in the above-mentioned first aspect.
[0028] It can be understood that the beneficial effects of the second to seventh aspects mentioned above refer to the first aspect and various possible implementations, and will not be elaborated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the technical solution of the present application, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the following drawings are only drawings of some embodiments of the present application. For ordinary technicians in this field, without paying any creative labor, other drawings that can also realize the technical solution of the present application can be obtained based on these drawings.
[0030] Figure 1 According to some embodiments of the present application, a schematic flow chart of a Full GC method is shown;
[0031] Figure 2A According to some embodiments of the present application, a schematic diagram of the unlocking interface of the mobile phone 100 is shown;
[0032] Figure 2B According to some embodiments of the present application, a schematic diagram of a main page of a shopping application is shown;
[0033] Figure 2C According to some embodiments of the present application, a schematic diagram of an activity interface of a supermarket is shown;
[0034] Figure 3A According to some embodiments of the present application, a flowchart of a garbage collection method is shown;
[0035] Figure 3B According to some embodiments of the present application, a flow chart of another garbage collection method is shown;
[0036] Figure 4 According to some embodiments of the present application, a schematic diagram of memory partitioning is shown;
[0037] Figure 5 According to some embodiments of the present application, a schematic diagram comparing the original Android allocation mechanism and the active object grouping mechanism is shown;
[0038] Figure 6 According to some embodiments of the present application, a schematic diagram of a garbage collection process based on an AGC mechanism is shown;
[0039] Figure 7 According to some embodiments of the present application, a flowchart of a garbage collection method based on the AGC mechanism is shown;
[0040] Figure 8 According to some embodiments of the present application, a schematic structural diagram of a communication device is shown;
[0041] Figure 9 According to some embodiments of the present application, a schematic structural diagram of an electronic device is shown;
[0042] Figure 10 According to some embodiments of the present application, a schematic diagram of a system software architecture is shown;
[0043] Figure 11 According to some embodiments of the present application, a schematic diagram of the hardware structure of a mobile phone is shown. DETAILED DESCRIPTION
[0044] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0045] The methods provided in the embodiments of the present application can be applied to any electronic device, including but not limited to mobile stations (MSs), mobile terminals (MTs), and the like. For example, the electronic device can be a mobile phone, a smart TV, a wearable device, a tablet computer, a virtual reality (VR) device, an augmented reality (AR) device, a terminal in industrial control, a terminal in self-driving, a terminal in remote medical surgery, a terminal in a smart grid, a terminal in transportation safety, a terminal in a smart city, a terminal in a smart home, and the like. The embodiments of the present application do not limit the specific form of the electronic device.
[0046] The "electronic device" in the embodiments of the present application may refer to the electronic device itself, or a component in the electronic device (for example, a processor, a chip, or a chip system, etc.), or a logic module or software that can implement all or part of the functions of the electronic device.
[0047] For ease of description, in the embodiments of this application, the electronic device will be described using a smartphone as an example. It is understandable that the technical solution of this application can also be applied to other electronic devices, which will not be described in detail here.
[0048] As described in the background technology, in the Android system, electronic devices usually manage memory areas through the garbage collector in ART. It can be understood that the memory area in the embodiment of the present application is the memory area occupied by the application. When the usage of the memory area reaches the upper limit, the garbage collector in ART will release the useless memory according to the garbage collection mechanism. The garbage collection mechanism is an automatic memory management mechanism. The garbage collection mechanism can be used to clear and recycle garbage objects in the memory area, release the corresponding area, and let other applications or objects use it. The garbage collection mechanism originated from the LISP language, and most high-level languages support the garbage collection mechanism, such as Java, .net, Go, etc.
[0049] Currently, the garbage collector in ART uses two garbage collection mechanisms: Young GC and Full GC. Young GC is used to quickly reclaim short-lived garbage objects and efficiently reclaim memory. Full GC is used to slowly and globally scan and reclaim long-lived garbage objects. Full GC can promptly locate and release all unused garbage objects in a memory area.
[0050] In some embodiments, ART divides the memory area into a Young Generation and an Old Generation. The Young GC initiation mechanism may include: when the memory capacity in the Young Generation reaches a first preset threshold, the ART garbage collector may use Young GC to collect garbage objects in the Young Generation. The Full GC initiation mechanism may include: when the memory capacity in both the Young and Old Generations reaches a second preset threshold, the ART garbage collector may use Full GC to scan all objects in both the Young and Old Generations, identify garbage objects, and free memory. The Young Generation includes newly allocated objects. After garbage collection in the Young Generation by Young or Full GC, surviving objects are moved to the Old Generation. Surviving objects include objects with direct and indirect references to root objects; garbage objects include objects with no direct or indirect references to root objects. Because most newly allocated objects have short lifecycles and become garbage objects quickly, there are fewer surviving objects in the Young Generation, and Young GC consumes less time. However, the objects in the old generation area have a long life cycle, and different objects become garbage objects at different times, making it difficult to predict when different objects will become garbage objects. Therefore, ART's garbage collector needs to use Full GC to scan all objects in the young generation area and the old generation area to find all garbage objects.
[0051] For example, in some embodiments, the process of Full GC executed by ART in the electronic device can be as follows: Figure 1 As shown, the following steps are included:
[0052] S101: Traverse the memory area allocated to the application and mark unreachable objects (ie, garbage objects).
[0053] It's understandable that marking unreachable objects, also known as unreachable objects, facilitates their subsequent removal. This step is called marking objects. In some possible implementations, ART traverses all objects in memory areas, including the young and old generations, to identify areas with low survival rates and then uses a coarse marking method to mark unreachable objects. Low survival rates refer to areas with a low occupancy rate of live objects.
[0054] Step S102: traverse the memory area again and delete unreachable objects.
[0055] In some possible implementations, when deleting unreachable objects, the garbage collector in ART will scan the memory area allocated to the application again, clean up the areas with low survival rates, delete the unreachable objects marked in the areas with low survival rates, and release their memory. It can be understood that when Full GC performs garbage collection using the above garbage collection method, the garbage collector needs to traverse all objects in the young generation area and the old generation area twice, and perform a global scan of the memory area twice, which takes a long time.
[0056] As the application's runtime increases, the memory area allocated by the electronic device for the application becomes increasingly utilized, and the number of long-lived objects increases. The number of objects in the young and old generation areas also continues to increase. These long-lived objects cannot be reclaimed by the Young GC and can only accumulate in the memory heap, ultimately requiring frequent Full GC triggering to reclaim garbage objects in the young and old generation areas. It's understandable that starting Full GC at an appropriate frequency can effectively reduce system memory pressure. However, as memory usage increases, global scan-based Full GC takes longer and longer. Frequently starting Full GC places an additional burden on the electronic device's CPU, causing frequent application pauses.
[0057] Therefore, the garbage collection mechanism adopted by the garbage collector in ART makes the overall efficiency of memory management low.
[0058] In view of this, the present application provides a garbage collection method for electronic devices. When an activity created by a third-party application exits, this method scans and clears the memory area occupied by the activity and the garbage objects in the young generation area based on the reference relationships between the root objects, freeing the area corresponding to the garbage objects.
[0059] Specifically, ART can divide the memory area into the young generation area, the old generation area, and the area corresponding to each activity. Among them, the young generation area is used to store newly allocated objects, and the old generation area is used to store surviving objects after garbage collection. The areas corresponding to each activity store the objects corresponding to each activity respectively. It can be understood that the area corresponding to each activity can include the area overlapping with the young generation area, called the active young generation area, and the area overlapping with the old generation area, called the active old generation area.
[0060] Furthermore, in some embodiments, ART may use a cross-activity table to record the cross-activity reference relationship of each object when a cross-activity reference relationship occurs for each object.
[0061] For example, if object M in the area corresponding to activity X points to an object in the area corresponding to activity Y, and activities X and Y are different, then object M's card in the cross-activity table is set to activity Y. For another example, if object M in the area corresponding to activity X points to an object in the area corresponding to activity Y, and activities X and Y are the same, then object M's card in the cross-activity table is set to 0. For another example, if object M in the area corresponding to activity X points to objects in the areas corresponding to at least two activities, and neither of these two activities is activity X, then object M's card in the cross-activity table is set to multiple activities. Furthermore, for some objects that do not have a reference relationship, their cards in the cross-activity table are also set to 0.
[0062] It is understood that in some embodiments, other forms of tables for recording cross-activity reference relationships of objects may be used, such as indexes, bitmaps, etc. In the embodiments of the present application, there is no specific limitation on the form of recording cross-activity reference relationships of objects.
[0063] If an activity exits, such as the first activity exits, ART can scan the recycling area, find the root objects needed for the recycling area, and then start looking for garbage objects and surviving objects from each root object, and then clear the garbage objects and move the surviving objects to the corresponding area. Among them, the recycling area includes the area corresponding to the exited activity and the young generation area, such as Figure 6 The root objects required for the reclaimed area include: root objects with cross-activity reference relationships in the cross-activity table (such as Figure 6 00 and object 01 in the ), the root object in the running stack, the root object in the local method stack, the root object in the static variable, and the root object in the Android card table. It can be understood that each object that can be accessed from the root object and is in the recycling area is a surviving object, such as Figure 6Survival object 1, surviving object 2, surviving object 3 and surviving object 4. If an object has no reference connection to the root object and is in the recycling area, the object is a garbage object.
[0064] The above garbage collection method promptly clears garbage objects from the areas occupied by the exiting activities and from the young generation area each time an activity exits. This method clears garbage objects from both the areas occupied by the exiting activities and the old generation area without initiating a Full GC. Compared to a Full GC that traverses and clears garbage from both the young and old generation areas, this method not only takes less time, but also reduces the frequency of Full GC initiations by promptly clearing garbage objects from some old generation areas. This improves both memory management efficiency and the user experience.
[0065] The following first describes some terms involved in this application.
[0066] 1. Android Runtime (ART): The managed runtime environment used by applications and some system services on the Android platform, responsible for executing and managing the application runtime environment. ART is a runtime library that provides core functionality such as memory management, thread management, and garbage collection. ART plays a critical role in the application lifecycle, ensuring that applications run correctly and efficiently on Android devices.
[0067] 2. Memory Area: In the embodiments of this application, this specifically refers to the heap memory used to store objects. This heap is a region of memory managed by the Java virtual machine and shared by all application threads. It is created when the virtual machine starts. When the virtual machine executes a program, it creates objects and allocates a block of memory for them in the memory area, where the objects are stored. Because the virtual machine continuously creates objects and allocates memory, if an object is no longer referenced, it becomes garbage and requires garbage collection. Otherwise, the memory will be consumed, affecting program execution.
[0068] 3. Application Activities: An important component of an application is a component that can contain a user interface (UI) and is typically used for user interaction. Each activity is responsible for handling specific user interactions. Each activity is implemented as a separate class and inherits from the Activity base class. The activity class will display the user interface composed of view controls and respond to events.
[0069] 4. Objects are the basic elements of application code, such as strings, functions, variables, and arrays. In object-oriented programming languages, objects are instances of a class. Objects have unique identifiers and contain properties and methods. Methods are the services an object provides, while properties are the properties of an object (for example, data structures, fields, etc.).
[0070] 5. Root object: refers to an object that can reference other objects but cannot be referenced by other objects.
[0071] 6. Surviving objects: These are objects that have a direct or indirect reference relationship with the root object, that is, every object that can be accessed from the root object. In a reference relationship, a first object pointing to a second object means that the first object references the second object, or that the second object is referenced by the first object.
[0072] 7. Garbage object: If an object has no reference to the root object, the object is a garbage object.
[0073] 8. Garbage Collection (GC): A memory management mechanism that uses a GC thread to reclaim garbage objects from a memory area (also known as the heap) to free up memory. In other words, GC is an automatic memory management mechanism. When dynamic memory on a computer is no longer needed, it should be released to free up storage space.
[0074] The following is based on Figures 2A to 7 Describe the technical solution of this application in detail.
[0075] Figures 2A to 2C A schematic diagram of a usage scenario of a third-party application is shown. Figure 2A Schematic diagram of the unlocking interface of the mobile phone 100 is shown. Figure 2A As shown, the unlocking interface 101 of the mobile phone 100 can display multiple third-party application icons, including: clock, calendar, file management, email, music card, video, sports health, weather, browser, smart life, settings, recorder, shopping, camera, address book, phone, information, etc. When the user needs to shop online, he can click the shopping application icon 102 in the unlocking interface 101. The mobile phone 100 can display the shopping application icon 102 on the display screen in response to the user's click operation. Figure 2B The main page 103 of the shopping application shown in FIG. When the user clicks any control in the main page 103 of the shopping application, such as the supermarket control 104, the mobile phone 100 can display the Figure 2C The activity interface 105 of the supermarket is shown.
[0076] As you can understand, an activity is an application component within the Android operating system. An application's interface consists of several activities. Each activity is assigned a window for drawing its user interface. This window houses views (the content displayed within the window) and display controls derived from views, such as buttons and labels. Users can perform various actions within the window. Different applications can have different activities. For example, a shopping application might have different activities than a memo application. All activities run within a system-created process. When ART executes an application, each activity creates a corresponding object and allocates memory for it within the application's memory area. For example, when the system creates the shopping application's homepage, it creates multiple objects corresponding to the homepage. Similarly, when the system creates a supermarket activity, it also creates multiple objects corresponding to the supermarket activity. Due to limited memory space, to ensure efficient use of memory, the garbage collector in ART reclaims objects and frees up the corresponding memory.
[0077] It can be understood that in the Android system, when each activity exits, many of the objects allocated to the activity in the foreground period will become garbage objects and need to be destroyed after the activity exits. To this end, the embodiment of the present application proposes an activity garbage collector, which is set in ART and can be used to efficiently recycle useless memory and improve the overall memory management efficiency of ART. The activity garbage collector includes: (1) an activity object group (AOG) module, which is used to divide the memory area into areas and identify the objects or areas occupied by the objects according to the activities to which the objects belong. (2) an activity garbage collection (AGC) module, which is a garbage collection mechanism that can efficiently recycle the areas of the young generation area and the old generation area marked as exited activities. (3) a cross activity table (CAT) module, which is used to mark the cross-activity references of each object, so that the cross-activity references can be quickly found through the marking during garbage collection. (4) a trigger mechanism module, which is a garbage collection mechanism that is actively triggered by an activity and can actively notify ART through the Android application framework layer (Framework) to start garbage collection when an activity exits, freeing up memory in advance and reducing memory pressure.
[0078] Based on the above active garbage collector, combined with Figures 3A to 11 , introduces the garbage collection method in the embodiment of this application.
[0079] First, according to Figure 3A , introduces a garbage collection method in an embodiment of the present application. The execution subject of the garbage collection method can be the mobile phone 100. Figure 3A As shown, a garbage collection method may include but is not limited to the following steps:
[0080] S301 , in response to the exit of the first activity among n activities, the ART in the mobile phone 100 scans the young generation area in the memory area and the second area corresponding to the first activity according to the reference relationship of each root object to obtain garbage objects.
[0081] Exemplarily, the memory area includes a young generation area, an old generation area, and n areas corresponding to n activities, where the n areas include at least a first area corresponding to a primary page, and each of the n areas includes an active young generation area that overlaps with the young generation area, and an active old generation area that overlaps with the old generation area. The young generation area is used to store objects created during the creation of n activities, and the old generation area is used to store surviving objects after garbage collection, where n is a natural number greater than 1.
[0082] S302 , the ART in the mobile phone 100 clears garbage objects.
[0083] It can be understood that Full GC can clean up long-lived garbage objects in the young and old generation areas. In existing garbage collection methods, long-lived garbage objects in the old generation area can only be cleaned up by Full GC. According to the aforementioned Full GC startup mechanism, when the storage capacity in the young and old generation areas reaches the second preset threshold, ART's garbage collector can use Full GC to scan and clear garbage objects in the young and old generation areas.
[0084] The above garbage collection method promptly cleans up garbage objects in the area corresponding to the exiting activity and in the young generation area each time an activity exits, which is less time-consuming than Full GC. Furthermore, without initiating Full GC, the above garbage collection method clears garbage objects in the old generation area of the activity corresponding to the exiting activity that overlaps with the old generation area. This increases the interval between the storage capacity in the young and old generation areas reaching a second preset threshold, thereby reducing the frequency of Full GC initiation, improving memory management efficiency, reducing application pause duration, and enhancing the user experience.
[0085] The following is based on Figure 3B , another garbage collection method in an embodiment of the present application is introduced. The execution subject of the garbage collection method can be the mobile phone 100.
[0086] like Figure 3B As shown, a garbage collection method may include but is not limited to the following steps:
[0087] S3001: Divide the memory area into a young generation area, an old generation area, and n areas corresponding to n activities.
[0088] It is understood that in some embodiments, in response to the start of the application, the ART in the mobile phone 100 can divide the memory area into a young generation area and an old generation area, and the young generation area and the old generation area can respectively include a plurality of third areas of the same size. Figure 4 As shown, the Young Generation area and the Old Generation area are each composed of several third regions, and each third region is of equal size. The third region of the Young Generation area, called the Young Region, is used to store objects newly allocated by the mobile phone 100 for applications. The third region of the Old Generation area, called the Old Region, is used to store objects that are still in use after garbage collection, that is, surviving objects.
[0089] Furthermore, the active object grouping module in ART can group and mark each third area in the memory area according to the activities to which the stored objects belong, and obtain the active object grouping mark of each third area. Based on the active object grouping mark of each third area, the memory area is divided into n areas corresponding to n activities, where n is a natural number greater than 1. The n areas include at least one first area corresponding to a main page, and each of the n areas includes an active young generation area that overlaps with the young generation area, and an active old generation area that overlaps with the old generation area.
[0090] In some embodiments, after the third-party application is started, when ART assigns an object to each third region, each third region may be marked based on the activity to which the assigned object belongs according to the activity object grouping module.
[0091] Figure 5 A comparison diagram of the original Android allocation mechanism and the active object grouping mechanism is shown. Figure 5 As shown, both the young and old generations in the memory area include several third regions, such as third region 1, third region 2, third region 3, third region 4, third region 5, third region 6, and third region 7. Each third region is of equal size. ART can allocate objects contiguously in each third region.
[0092] When the application switches from the first activity to the second activity, according to the original Android allocation mechanism, each object is stored in the young generation area and the old generation area respectively.
[0093] According to the active object grouping mechanism, ART allocates objects belonging to the same activity in the young generation area and the old generation area to the same third area, and marks each third area according to the activity to which the stored objects belong. Figure 5 For example, the third area 1 of the young generation area stores objects of the first activity, and the active objects of the third area 1 are grouped and marked as the first activity; the third area 2 of the old generation area stores objects corresponding to the first activity, and the active objects of the third area 2 are grouped and marked as the first activity; the third area 3 of the old generation area stores objects corresponding to the first activity, and the active objects of the third area 3 are grouped and marked as the first activity; the third area 4 of the young generation area stores objects of the second activity, and the active objects of the sub-area 4 are grouped and marked as the second activity; the third area 5 of the young generation area stores objects of the first activity, and the active objects of the third area 5 are grouped and marked as the first activity; the third area 6 of the old generation area stores objects corresponding to the first activity, and the active objects of the third area 6 are grouped and marked as the first activity; the third area 7 of the old generation area stores objects corresponding to the first activity, and the active objects of the third area 7 are grouped and marked as the first activity, and so on.
[0094] It can be understood that by grouping and marking the third regions according to the activities to which the objects stored in the third regions belong, the memory region can be accurately divided into regions corresponding to the activities, thereby accurately determining the reclaimed region.
[0095] In some embodiments, after a third-party application is launched, when ART allocates objects to a memory area, the area occupied by each object in the memory area may be marked according to the activity to which each object belongs, thereby obtaining an activity object grouping tag for the area occupied by each object. Based on the activity object grouping tag for the area occupied by each object, the memory area is divided into n areas corresponding to n activities, where n is a natural number greater than 1.
[0096] It can be understood that grouping and marking the areas occupied by objects in the memory area according to the activities to which the stored objects belong can also accurately divide the areas corresponding to each activity, thereby accurately determining the reclaimed areas. Marking the third areas in the young and old generation areas according to the activities to which the objects stored therein belong requires fewer markings, occupies less memory, and requires less memory overhead, compared to marking the areas occupied by objects in the memory area according to the activities to which the objects belong.
[0097] S3002: Mark the cross-activity reference relationship of each object in the memory area, and store the mark of the cross-activity reference relationship in a cross-activity table.
[0098] It is understandable that ART can dynamically mark the cross-activity reference relationship of each object allocated to the application. In some embodiments, ART can write the cross-activity reference relationship into the memory occupied by each object when a cross-activity reference relationship appears, mark the cross-activity reference relationship during the writing process, and then store the cross-activity reference relationship in the cross-activity table. In some embodiments, ART can also mark and store the cross-activity reference relationship of each allocated object according to a preset time or condition. In the embodiments of the present application, there is no specific limitation on the frequency and time of marking and storing the cross-activity reference relationship of each object.
[0099] Specifically, the cross-activity table may include multiple cards, each corresponding to a predetermined size area in the memory area, such as a 1KB area, and each card records the cross-activity reference relationship of an object.
[0100] For example, if in the third area of the active object group marked as Activity X, object M points to an object in the third area of the active object group marked as Activity Y, and Activity X and Activity Y are different, then the card corresponding to object M is set to Activity Y. For another example, if in the third area of the active object group marked as Activity X, object M points to an object in the third area of the active object group marked as Activity Y, and Activity X and Activity Y are the same, then the card corresponding to object M is set to 0. For another example, if in the third area of the active object group marked as Activity X, object M points to objects in at least two third areas, and at least two third areas are not marked as Activity X, then the card corresponding to object M is set to Multiple Activities, that is, Multiple Activities. In addition, for some objects that do not have a reference relationship, their corresponding cards are also set to 0.
[0101] It can be understood that the cross-activity table borrows ART's write barrier to record reference updates and expands the Android card table, that is, the ART card table in the Android system. The original ART card table only records the reference relationship between objects in the old generation area pointing to objects in the young generation area, while the cross-activity table records the cross-activity reference relationship between objects. Therefore, the cross-activity table does not require additional memory overhead and introduces new write barriers, and will not cause additional overhead to program operation. In addition, through the cross-activity table, cross-activity reference relationships can be quickly found, and the auxiliary AGC only needs to quickly find garbage objects in the recycling area and avoid global scans similar to Full GC. It can be understood that in some embodiments, other forms of tables that record the cross-activity reference relationships of each object can also be used, such as indexes, bitmaps (BitMap), etc. In the embodiments of the present application, there is no specific limitation on the form of recording the cross-activity reference relationships of each object.
[0102] S3003: When the first activity exits, garbage collection is started, and the root objects required for the recycling area are found according to the cross-activity reference relationship recorded in the cross-activity table and other reference relationships.
[0103] It can be understood that based on the trigger mechanism module in the activity garbage collector, the Android system framework layer can pass the life cycle of activity switching to ART to help ART determine the activity that the current application exits and the activity that is switched. When any activity exits, such as when the first activity exits, the Android system framework layer can actively notify ART to start garbage collection. ART will start the activity garbage collector to perform garbage collection on the recycling area. Among them, the recycling area includes the young generation area and all third areas where the activity objects are grouped and marked as the first activity, that is, the young generation area and the second area corresponding to the first activity.
[0104] It is understandable that the existing garbage collection mechanisms of Young GC and Full GC will continue to be retained to adapt to different garbage collection scenarios.
[0105] Figure 6 Figure 2 shows the garbage collection process based on the AGC mechanism. Figure 6 As shown, the memory area may include: an area marked as the first activity, an area marked as the second activity, and an area marked as the main page. When the first activity exits, it can be switched to the second activity. It is understandable that Figure 6 The areas corresponding to the activities in the memory area are only examples. In some embodiments, the memory area may include areas corresponding to more activities, or areas corresponding to fewer activities. In the embodiment of the present application, the memory area may include at least a second area in which activity objects are grouped and marked as the first activity, and a first area in which activity objects are grouped and marked as the main page, where the first activity is the exited activity. That is, when the first activity is exited, it can be switched to the main page.
[0106] In order to avoid the need for a global search similar to Full GC, ART first finds the root objects required by AGC based on the cross-activity reference relationships recorded in the cross-activity table and other reference relationships. These root objects include: root objects with cross-activity reference relationships in the cross-activity table, root objects in the run stack, root objects in the native method stack, root objects in static variables, and root objects in the Android card table. Among them, the root objects with cross-activity reference relationships in the cross-activity table can include: Figure 6 In the example, Object 00 points to live object 1, and Object 01 points to live object 4. The reclaimed area includes the young generation area and a second area where live objects are grouped and marked as first live. As can be seen, the cross-live table helps the AGC quickly find garbage objects in the reclaimed area, avoiding the full GC global scan.
[0107] S3004: Perform a local search in the recycling area based on each root object to find surviving objects, move the surviving objects to the corresponding area, and clear the garbage objects.
[0108] Specifically, in step S3004, a local search is performed in the recycling area according to each root object to find the surviving object, move the surviving object to the corresponding area, and clear the garbage object. Figure 7 shown. Figure 7 The garbage collection method based on the AGC mechanism may include but is not limited to the following steps:
[0109] S701: Obtain an object graph based on all root objects, all objects in the reclaimed area, and reference relationships.
[0110] It can be understood that all root objects found by ART include: root objects with cross-activity reference relationships in the cross-activity table, root objects in the run stack, root objects in the native method stack, root objects in static variables, and root objects in the Android card table. These root objects, the young generation area, and all objects and reference relationships in the second area of the activity group marked as the first activity can form an object graph.
[0111] S702: Mark all root objects as visited.
[0112] S703: Traverse the object graph starting from the root object to find all surviving objects.
[0113] As can be understood, the object graph is traversed from the root object to find all reachable objects, i.e., surviving objects. During the traversal process, the visited objects are marked as visited. As can be understood, surviving objects are objects that are reachable and in the reclaimed area.
[0114] S704: Move the surviving object to the corresponding area according to the active object grouping mark of the third area where the surviving object is located.
[0115] For example, if the object grouping in the third area where the surviving object is located is marked as the first activity, then the surviving object is moved to the active old generation area in the area of the main page that overlaps with the old generation area. If the object grouping in the third area where the surviving object is located is marked as the main page, then the surviving object is moved to the active old generation area in the area of the main page that overlaps with the old generation area. If the object grouping in the sub-area where the surviving object is located is marked as other activities, then the reachable object is moved to the active old generation area in the area where the object grouping is marked as other activities that overlaps with the old generation area. For example, when the first activity exits and switches to the second activity, you can do the following: Figure 6As shown, the surviving objects in the active young generation area in the fourth area corresponding to the second activity, such as surviving object 4, are moved to the active old generation area in the fourth area. It can be understood that objects moved to the old generation area can be reclaimed by Full GC when they become garbage objects.
[0116] S705, clear garbage objects.
[0117] It can be understood that after the traversal is completed, all visited objects are marked as visited, and unvisited objects are unreachable, that is, garbage objects. After clearing the garbage objects and completing the movement of the surviving objects, the memory corresponding to the surviving objects and garbage objects is released.
[0118] As you can understand, the above garbage collection method, based on the characteristic of applications consisting of multiple activities, improves the ART object allocation mechanism to efficiently group objects according to the application activity at the time of object allocation. This helps the garbage collection mechanism quickly find objects created by the activity. After the activity exits, garbage collection is used to clean up the group corresponding to the exiting activity. With almost no additional overhead for Full GC and Young GC, AGC proactively, efficiently, and promptly cleans up long-lived objects, reducing the frequency of Full GC initiations and improving the overall efficiency of ART garbage collection.
[0119] This garbage collection method initiates garbage collection upon activity exit, promptly clearing garbage objects belonging to the exiting activity and those in the young generation. This reduces time consumption, avoids frequent Full GC initiations, reduces application pause time, and improves memory management efficiency and user experience. Furthermore, by using the cross-activity table to locate all root objects in the collection area, it improves garbage collection accuracy and the efficiency of finding surviving objects.
[0120] It can be understood that the AGC mechanism finds live objects by performing a depth-first search on objects and references in the reclaimed area, while unaccessed objects are identified as garbage objects. It can be divided into the following two steps: Step (A): In order to avoid the global search like Full GC, the AGC mechanism can find the reclaimed area (including Figure 6The root objects required for the young and middle generation areas and the area corresponding to the first activity to exit) include root objects with cross-activity reference relationships in the cross-activity table, root objects in the running stack, root objects in the local method stack, root objects in static variables, and root objects in the Android card table. Step (B): Perform a local depth-first search in the reclaimed area to find the surviving objects and move them to the corresponding memory area. The surviving objects in the second area corresponding to the first activity to exit are moved to the active old generation area of the first area corresponding to the main page. The surviving objects in the first area corresponding to the main page are moved to the active old generation area of the first area corresponding to the main page. The surviving objects in the area corresponding to the activities that have not exited are moved to the active old generation area of the area corresponding to the activities that have not exited. When these surviving objects become useless, they are recycled using Full GC. After the movement of the surviving objects is completed, the corresponding memory is released.
[0121] It can be understood that when an activity exits, the above-mentioned garbage collection method actively triggers garbage collection, and promptly recycles the large number of garbage objects generated by the activity exit, effectively reducing the memory pressure of the system. Since garbage collection runs through the entire life cycle of the program, the garbage collection performance has a great impact on the business. Currently, the garbage collection performance is mainly reflected in the throughput. The throughput is the ratio of the total amount of released memory to the time spent on garbage collection. The higher the throughput, the better the garbage collection performance. Table 1 shows the throughput of Android's original garbage collection mechanism and the throughput of the garbage collection mechanism of the embodiment of the present application.
[0122] Table 1:
[0123]
[0124] It is understood that the existing Android garbage collection mechanism in Table 1 includes Young GC and Full GC, while the garbage collection mechanism in the embodiment of the present application includes AGC, Young GC, and Full GC. As can be seen from Table 1, the garbage collection mechanism in the embodiment of the present application significantly improves the throughput of various applications, for example, from 22% to 149%.
[0125] Table 2 shows the time consumption of AGC, Young GC, and Full GC.
[0126] Table 2:
[0127]
[0128] As can be seen from Table 2, AGC takes longer than Young GC, but significantly less time than Full GC. By combining AGC with Young GC and Full GC for garbage collection and reducing the frequency of Full GC startup, the overall garbage collection efficiency can be greatly improved.
[0129] Table 3 shows the Full GC startup interval in the original Android system and the Full GC startup interval in the embodiment of the present application.
[0130] Table 3:
[0131]
[0132] It can be understood from Table 3 that the AGC mechanism in the embodiment of the present application prolongs the start interval of Full GC, reduces the impact on the user's foreground use, and can greatly improve the overall garbage collection efficiency.
[0133] In order to better implement the above-mentioned solutions of the embodiments of the present application, relevant devices for implementing the above-mentioned solutions are also provided below.
[0134] See also Figure 8 , is a structural diagram of a communication device provided in an embodiment of the present application, which is used to implement the functions of the electronic device in the above-mentioned method embodiment. In some possible implementations, the communication device 800 provided in the embodiment of the present application may include a storage module 801 (sometimes also referred to as a storage unit) for storing any data, computer instructions and / or computer programs that may be involved in the various embodiments of the present application; and a processing module 802 (sometimes also referred to as a processing unit) for, in response to the exit of the first activity among n activities, scanning the young generation area and the second area corresponding to the first activity in the memory area according to the reference relationship of each root object, obtaining garbage objects, and clearing the garbage objects, wherein the memory area includes a young generation area, an old generation area, and n areas corresponding to n activities, the n areas including at least a first area corresponding to a main page, each of the n areas including an active young generation area overlapping with the young generation area and an active old generation area overlapping with the old generation area, the young generation area is used to store objects when the n activities are created, and the old generation area is used to store surviving objects after garbage collection, and n is a natural number greater than 1.
[0135] The device may also include a transceiver module (sometimes also referred to as a transceiver unit). The transceiver module is capable of both transmitting and receiving functions. When a transceiver module implements the transmitting function, it may be referred to as a transmitting module (sometimes also referred to as a transmitting unit); when a transceiver module implements the receiving function, it may be referred to as a receiving module (sometimes also referred to as a receiving unit). The transmitting module and the receiving module may be the same functional module, referred to as the transceiver module, which is capable of both transmitting and receiving functions; alternatively, the transmitting module and the receiving module may be different functional modules, with the transceiver module being a collective term for these functional modules.
[0136] For more detailed operation of the storage module 801 and the processing module 802, please refer to the description in the above method embodiment, which will not be repeated here.
[0137] It should be noted that the physical device corresponding to the storage module 801 in the present device may be a memory, the physical device corresponding to the processing module 802 may be a processor, and the physical device corresponding to the transceiver module may be a transceiver.
[0138] It should be noted that the information interaction, execution process, etc. between the modules of the above-mentioned device are based on the same concept as the method embodiment of the present application, and the technical effects they bring are the same as those of the method embodiment of the present application. For specific contents, please refer to the description in the method embodiment shown above in this application, and no further details will be given here.
[0139] See also Figure 9 , is a structural diagram of an electronic device 900 provided in an embodiment of the present application.
[0140] like Figure 9 As shown, the electronic device includes a processor 910 and an interface circuit 920. The processor 910 and the interface circuit 920 are coupled to each other. It is understandable that the interface circuit 920 can be a transceiver or an input / output interface. Optionally, the electronic device 900 may further include a memory 930 for storing instructions executed by the processor 910, such as garbage collection instructions, or for storing input data required by the processor 910 to execute instructions or for storing data generated after the processor 910 executes instructions, such as grouping tags of active objects in each third area of the memory area, cross-activity reference relationships between objects, etc.
[0141] When the electronic device 900 is used to implement the above-mentioned garbage collection method and other embodiments, the processor 910 is used to implement the functions of the above-mentioned processing module 802, and the interface circuit 920 is used to implement the functions of the above-mentioned transceiver module.
[0142] When the electronic device is a chip applied to a terminal, the terminal chip implements the functions of the electronic device in the above method embodiment.
[0143] It can be understood that an operating system is running in the mobile phone 100. The embodiment of the present application takes the layered architecture Android system as an example to illustrate the software structure of the mobile phone 100. Figure 10 This is a schematic diagram of the system software architecture of mobile phone 100. The software structure utilizes a layered architecture, which divides the software into several layers, each with distinct roles and responsibilities. Layers communicate with each other via software interfaces. In some embodiments, the Android system is divided into five layers: from top to bottom: the application layer, the application framework layer (Framework), the Android runtime (ART) and system libraries, the hardware abstraction layer (HAL), and the kernel layer (Kernel).
[0144] The application layer may include a series of application packages, such as camera, gallery, calendar, call, map, navigation, wireless local area network (WLAN), Bluetooth, music, video, and short message applications.
[0145] The application framework layer provides an application programming interface (API) and programming framework for applications in the application layer. The application framework layer includes predefined functions. For example, the application framework layer may include a window manager, content provider, telephony manager, resource manager, notification manager, and view system.
[0146] The window manager is used to manage window programs. The window manager can obtain the size of the display screen and set the windows corresponding to each display interface based on the window properties. The content provider is used to store and obtain data and make this data accessible to applications. The stored data may include video data, image data, audio data, etc. The phone manager is used to provide communication functions for the mobile phone 100. For example, it manages call status (including call connection, hang up, etc.). The resource manager provides various resources to applications, such as localized strings, icons, images, layout files, video files, etc. The notification manager enables applications to display notification information in the status bar of the screen, which can be used to convey messages to users. The view system includes visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. The display interface can be composed of one or more views.
[0147] ART includes a core library and a virtual machine. ART is responsible for scheduling and management of the Android system. The garbage collector in the embodiment of this application is set in ART. The core library consists of two parts: one part is the function that needs to be called by the Java language, and the other part is the Android core library.
[0148] The application layer and application framework layer run in a virtual machine. The virtual machine executes Java files in the application layer and application framework layer as binary files. The virtual machine manages object lifecycles, stack management, thread management, security and exception management, and garbage collection.
[0149] The system library can include multiple functional modules, such as surface manager, media library, three-dimensional (3D) graphics processing library (such as OpenGL ES), two-dimensional (2D) graphics engine, etc.
[0150] The surface manager manages the display subsystem of electronic devices 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 static image files. The media library supports a variety of audio and video encoding formats. The 3D graphics processing library implements 3D graphics drawing, image rendering, compositing, and layer processing. The 2D graphics engine is a drawing engine for 2D graphics.
[0151] The hardware abstraction layer (HAL) encapsulates hardware drivers and provides a common interface for the framework layer to call drivers. The HAL includes the touch panel (TP) HAL, display HAL, and sensor HAL.
[0152] 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.
[0153] Figure 11 According to an embodiment of the present application, a schematic diagram of the hardware structure of a mobile phone 100 is shown. The mobile phone 100 can be the electronic device mentioned in the above embodiment.
[0154] The mobile phone 100 may include a processor 110, an external memory interface 120, an internal memory 121, an interface module 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.
[0155] It should be understood that the structures illustrated in the embodiments of the present application do not constitute a specific limitation on the mobile phone 100. In other embodiments of the present application, the mobile phone 100 may include more or fewer components than shown, or may combine or separate certain components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0156] Processor 110 may include one or more processing units.
[0157] The charging management module 140 is configured to receive charging input from a charger. The power management module 141 is configured to connect the battery 142 , the charging management module 140 and the processor 110 .
[0158] The wireless communication functions of mobile phone 100 are implemented through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, a modem processor, and a baseband processor. Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Mobile communication module 150 can provide wireless communication solutions for mobile phone 100, including 2G / 3G / 4G / 5G. Wireless communication module 160 can provide wireless communication solutions for mobile phone 100, including wireless local area network (WLAN).
[0159] In some embodiments, the antenna 1 of the mobile phone 100 is coupled to the mobile communication module 150, and the antenna 2 is coupled to the wireless communication module 160, so that the mobile phone 100 can communicate with the network and other devices through wireless communication technology.
[0160] Mobile phone 100 implements its display function through a graphics processing unit (GPU), display 194, and an application processor. The GPU is a microprocessor for image processing and connects display 194 and the application processor. Display 194 is used to display images, videos, etc. Display 194 includes a display panel. Camera 193 is used to capture still images or videos.
[0161] 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 mobile phone 100. The internal memory 121 can be used to store computer executable program code, and the executable program code includes instructions. The internal memory 121 may include a program storage area and a data storage area. For example, the program storage area can be used to store computer instructions and / or computer programs corresponding to the above-mentioned garbage collection methods, algorithms, etc. The data storage area can be used to store the active object grouping tags of each third area in the above-mentioned memory area, the cross-activity reference relationship of each object, etc.
[0162] The mobile phone 100 can implement audio functions such as music playback and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.
[0163] The pressure sensor 180A senses pressure signals and converts them into electrical signals. The gyroscope sensor 180B can be used to determine the motion of the mobile phone 100. The air pressure sensor 180C measures air pressure. The magnetic sensor 180D includes a Hall effect sensor. The acceleration sensor 180E detects the magnitude of acceleration of the mobile phone 100 in various directions (typically three axes). The distance sensor 180F measures distance. The proximity sensor 180G may include, for example, a light-emitting diode (LED) and a photodetector. The ambient light sensor 180L senses ambient light brightness. The fingerprint sensor 180H collects fingerprints. The temperature sensor 180J detects temperature. The touch sensor 180K, also known as a "touch device," is also called a "touch control device." The buttons 190 include a power button and a volume button. The motor 191 can generate vibration prompts. The indicator 192 can be an indicator light that can indicate charging status, battery level changes, messages, missed calls, notifications, etc. The SIM card interface 195 connects to a SIM card.
[0164] It should be understood that the structure of the electronic device shown in the embodiments of the present application does not constitute a specific limitation on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than shown, or some components may be combined or separated, or the components may be arranged differently. The components shown in the illustrations may be implemented in hardware, software, or a combination of software and hardware.
[0165] The various embodiments of the mechanisms disclosed in this application can be implemented in hardware, software, firmware, or a combination of these implementation methods. The embodiments of this application can be implemented as a computer program or program code executed on a programmable system, which includes at least one processor, a storage system (including volatile and non-volatile memory and / or storage elements), at least one input device, and at least one output device.
[0166] Program code can be implemented with a high-level programming language or an object-oriented programming language to communicate with the processing system. Where necessary, program code can also be implemented in assembly language or machine language. In fact, the mechanism described in this application is not limited to the scope of any particular programming language. In either case, the language can be a compiled language or an interpreted language.
[0167] In some cases, the disclosed embodiments may be implemented in hardware, firmware, software, or any combination thereof. The disclosed embodiments may also be implemented as instructions carried by or stored on one or more transitory or non-transitory machine-readable (e.g., computer-readable) storage media, which can be read and executed by one or more processors. For example, the instructions may be distributed over a network or via other computer-readable media. Thus, a machine-readable medium may include any mechanism for storing or transmitting information in a form readable by a machine (e.g., a computer), including but not limited to floppy disks, optical disks, optical discs, magneto-optical disks, read-only memory (ROM), random access memory (RAM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), magnetic or optical cards, flash memory, or tangible machine-readable storage for transmitting information via the Internet using electrical, optical, acoustic, or other propagated signals (e.g., carrier waves, infrared signals, digital signals, etc.). Accordingly, machine-readable media includes any type of machine-readable media suitable for storing or transmitting electronic instructions or information in a form readable by a machine, eg, a computer.
[0168] Embodiments of the present application also provide a system that may include at least one processor that executes instructions stored in a non-transitory computer-readable medium, such as a memory. The processor may be any conventional processor, such as a commercially available central processing unit (CPU). Alternatively, the processor may be a dedicated device such as an application-specific integrated circuit (ASIC) or other hardware-based processor.
[0169] An embodiment of the present application further provides a communication device, which includes a module for executing the methods in the above embodiments.
[0170] An embodiment of the present application further provides a chip, which includes a processor coupled to a memory and configured to execute computer programs or instructions stored in the memory to implement the methods in the above embodiments.
[0171] In addition, an embodiment of the present application further provides a computer-readable storage medium, on which instructions are stored. When the instructions are executed on an electronic device, the electronic device executes the methods in the above aspects.
[0172] In addition, an embodiment of the present application further provides a computer program product, which includes: computer program code, which enables the computer to execute the methods in the above aspects when the computer program code is run on a computer.
[0173] In the accompanying drawings, some structural or method features may be shown in a particular arrangement and / or order. However, it should be understood that such a particular arrangement and / or order may not be required. Rather, in some embodiments, these features may be arranged in a manner and / or order different from that shown in the illustrative drawings. In addition, the inclusion of a structural or method feature in a particular figure does not imply that such feature is required in all embodiments, and in some embodiments, such features may not be included or may be combined with other features.
[0174] It should be noted that the units / modules mentioned in the various device embodiments of the present application are all logical units / modules. Physically, a logical unit / module can be a physical unit / module, or a part of a physical unit / module, or can be implemented as a combination of multiple physical units / modules. The physical implementation of these logical units / modules themselves is not the most important. The combination of functions implemented by these logical units / modules is the key to solving the technical problems raised by this application. In addition, in order to highlight the innovative part of this application, the above-mentioned device embodiments of this application do not introduce units / modules that are not closely related to solving the technical problems raised by this application. This does not mean that other units / modules do not exist in the above-mentioned device embodiments.
[0175] While the present invention has been shown and described with reference to certain preferred embodiments thereof, it will be understood by those skilled in the art that various changes in form and details may be made therein without departing from the scope of the invention.
Claims
1. A garbage collection method, characterized in that: include: In response to the exit of the first activity among n activities, scanning the young generation areas in the n areas corresponding to the n activities, the young generation areas in the first area corresponding to the main page, and the old generation areas in the second area corresponding to the first activity in the memory area according to the reference relationship of each root object, to obtain garbage objects, wherein the memory area includes the young generation areas in the n areas and the old generation areas in the n areas, and the young generation areas and the old generation areas in the first area corresponding to the main page, wherein the area where each area in the n areas overlaps with the young generation area is an active young generation area, and the area where each area in the n areas overlaps with the old generation area is an active old generation area, the young generation area is used to store objects when the n activities are created, and the old generation area is used to store surviving objects after garbage collection, and n is a natural number greater than 1; Clear the garbage object.
2. The method according to claim 1, characterized in that The method further comprises: Obtaining surviving objects in the young generation area and the second area; The surviving objects in the active young generation area and the active old generation area in the second region and the active young generation area in the first region are moved to the active old generation area in the first region.
3. The method according to claim 2, characterized in that The root objects include: root objects with cross-activity reference relationships, root objects in the run stack, root objects in the native method stack, root objects in static variables, and root objects in the Android card table.
4. The method according to claim 2, characterized in that The surviving objects include objects that have direct reference relationships and indirect reference relationships with the root object; The garbage objects include objects that have no direct reference relationship or indirect reference relationship with the root object.
5. The method according to claim 1, characterized in that The n regions corresponding to the n activities are determined based on the activities to which each object belongs and the regions occupied by each object in the memory area.
6. The method according to claim 1, characterized in that The n regions corresponding to the n activities are divided based on the activities to which the objects in the third region belong. The third region is a subset of the young generation region or the old generation region. The young generation region and the old generation region respectively include multiple third regions of the same size.
7. The method according to claim 1, characterized in that The method further comprises: Switch to a second activity among the n activities.
8. The method according to claim 7, characterized in that The method further comprises: Move surviving objects in the active young generation area of the fourth region corresponding to the second activity to the active old generation area of the fourth region.
9. An electronic device, characterized in that: The method comprises a processor and an interface circuit, wherein the interface circuit is used to receive signals from other communication devices and transmit them to the processor or send signals from the processor to other communication devices, and the processor implements the method according to any one of claims 1 to 8 through a logic circuit or executing code instructions.
10. A program product, characterized in that The program product includes instructions, and when the instructions are executed, the method according to any one of claims 1 to 8 is implemented.
11. A communication device, characterized in that: Comprising means for executing the method according to any one of claims 1 to 8.
12. A computer-readable storage medium, characterized in that The storage medium stores a computer program or instruction. When the computer program or instruction is executed by the communication device, the method according to any one of claims 1 to 8 is implemented.
13. A chip, characterized in that: The chip comprises a processor coupled to a memory and configured to execute a computer program or instruction stored in the memory, so that the chip implements the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
System internal memory control method and system thereof
CN105573811A
Concurrent remembered set scanning
US12147336B1