Method, device and computer storage medium for memory garbage collection
By identifying garbage RC rings in IoT devices through marked nodes and reference count values, and employing subtraction and recovery operations, efficient recycling of garbage RC rings is achieved, solving the problem of memory resource waste in existing technologies, simplifying marking rules, and improving real-time performance.
Patent Information
- Application Number
- CN201710306751.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2017-05-04
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2037-05-04
AI Technical Summary
In existing technologies, reference counting cannot reclaim circular reference (RC) nodes that occur in IoT devices, resulting in wasted memory resources.
By marking nodes and their reference counts, it is determined whether a marked node is located in the garbage RC ring. After determination, nodes in the garbage RC ring are recycled. Subtraction and restoration operations are used to simplify the marking rules.
It effectively reclaims nodes in the garbage RC ring, avoids wasting memory resources, simplifies the marking rules, reduces the algorithm's memory consumption, and improves real-time performance.
Smart Images

Figure CN108804337B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a method, apparatus, and computer storage medium for memory garbage collection. Background Technology
[0002] In recent years, with the rapid development of Internet of Things (IoT) technology, many types of IoT devices have emerged. Due to the limited memory resources of IoT devices (FLASH less than 128KB, RAM less than 64KB), an effective way to easily deploy applications or services onto IoT devices is to support the JavaScript language. However, JS generates a large number of garbage objects during runtime, requiring an automatic garbage collection (GC) mechanism.
[0003] Existing technologies use reference counting to reclaim objects in IoT devices that are no longer used by applications, thereby achieving automatic reclamation of memory resources. Reference counting maintains a reference count for each node (also known as an internal storage object, or simply an "object"). When a new reference points to a node, its reference count is incremented by 1; when an application pointing to a node is destroyed, its reference count is decremented by 1. When the count reaches zero, the memory resources occupied by that node are reclaimed.
[0004] However, reference counting cannot reclaim nodes that have circular references (RC), such as... Figure 1 As shown, R1, R2, and R3 form an RC ring (the ref in the diagram represents the reference count value of the corresponding node). R1 has external references and its reference count value is 2, while the reference count values of R2 and R3 are 1. Figure 2 As shown, when the external references disappear, the reference counts of R1, R2, and R3 cannot be cleared because there are mutual references between nodes within the ring. This results in some memory not being reclaimed, causing a waste of memory resources. Summary of the Invention
[0005] This application provides a method, apparatus, and computer storage medium for memory garbage collection, which can realize the recycling of garbage RC rings.
[0006] Firstly, a method for garbage collection of memory is provided, the method comprising: determining whether a marked node is located in a garbage circular reference (RC ring) based on a marked node of an application and the reference count value of the marked node, wherein no node in the garbage RC ring has external references; and when the marked node is located in the garbage RC ring, reclaiming the node in the garbage RC ring.
[0007] In this application, it is possible to determine whether a marker node is located within a waste RC ring by using the marker node and its reference count value, and if the marker node is located within the waste RC ring, the waste RC ring can be recycled.
[0008] In conjunction with the first aspect, in some implementations of the first aspect, determining whether a marked node is located in a garbage circular reference (RC ring) based on the application's marked node and the reference count value of the marked node includes: decrementing the reference count value of the current node's child nodes by 1; if a first target child node with a reference count value of 0 appears among the current node's child nodes, then the first target child node is taken as the current node, and this step is repeated until the target condition is met; if the first target child node does not appear among the current node's child nodes, then this step is exited, wherein the marked node is the initial value of the current node, and the target condition is that the marked node appears among the first target child nodes of the current node; if the target condition is met, it is determined that the marked node is located in the garbage RC ring.
[0009] The process of decrementing the reference count of the current node's child nodes by 1, and then, if a first target child node appears among the current child node's child nodes, making that first target child node the current node and continuing to decrement the reference count of the current node by 1, can be called a subtraction operation. This subtraction operation makes it relatively easy to determine whether a marked node is located in a garbage RC ring, so that once it is determined that the marked node is in the garbage RC ring, the nodes in the garbage RC ring can be reclaimed.
[0010] In conjunction with the first aspect, in some implementations of the first aspect, the method further includes: determining that the marked node is located in a non-garbage RC ring if the target condition is not met; and restoring the reference count values of the current node and its child nodes.
[0011] When a marked node is not in a garbage RC ring, the reference count of the non-garbage RC ring can be restored by restoring the current node and its reference count value, thereby ensuring the correctness of the reference relationships of other non-garbage RC ring nodes after the garbage collection operation is performed.
[0012] In conjunction with the first aspect, in some implementations of the first aspect, restoring the reference count values of the current node and its child nodes includes: incrementing the reference count value of the current node's child nodes by 1; if a second target child node with a reference count value of 1 appears among the child nodes of the current node, then the second target child node is used as the current node, and this step is re-executed; if the second target child node does not appear among the child nodes of the current node, then this step is exited.
[0013] In conjunction with the first aspect, in some implementations of the first aspect, the method further includes: determining a set of marked nodes, wherein the set of marked nodes contains multiple nodes, and the marked node is any one of the multiple nodes.
[0014] In a second aspect, an apparatus for memory garbage collection is provided, the apparatus including a module for performing the methods of the first aspect or its various implementations.
[0015] Thirdly, an apparatus for memory garbage collection is provided, the apparatus comprising: a storage medium and a central processing unit, the storage medium being a non-volatile storage medium storing a computer-executable program, the central processing unit being connected to the non-volatile storage medium and executing the computer-executable program to implement the method of the first aspect or its various implementations.
[0016] Fourthly, a computer-readable medium is provided that stores program code for execution by a computer, the program code including instructions for performing the methods of the first aspect or its various implementations.
[0017] It should be understood that the technical solutions provided in the second to fourth aspects of this invention are consistent with those provided in the first aspect in terms of technical means and beneficial effects, and will not be described again. Attached Figure Description
[0018] Figure 1 This is a schematic diagram of an RC ring.
[0019] Figure 2 This is a schematic diagram of an RC ring.
[0020] Figure 3 This is a schematic flowchart of a memory garbage collection method according to an embodiment of this application.
[0021] Figure 4 This is a schematic diagram of an RC ring.
[0022] Figure 5 This is a schematic diagram of an RC ring.
[0023] Figure 6This is a schematic diagram of an RC ring.
[0024] Figure 7 This is a schematic diagram of an RC ring.
[0025] Figure 8 This is a schematic diagram of an RC ring.
[0026] Figure 9 This is a schematic diagram of an RC ring.
[0027] Figure 10 This is a schematic diagram of an RC ring.
[0028] Figure 11 This is a schematic diagram of an RC ring.
[0029] Figure 12 This is a schematic flowchart of a memory garbage collection method according to an embodiment of this application.
[0030] Figure 13 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0031] Figure 14 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0032] Figure 15 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0033] Figure 16 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0034] Figure 17 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0035] Figure 18 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0036] Figure 19 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0037] Figure 20 This is a schematic diagram of multiple nodes in an embodiment of this application.
[0038] Figure 21 This is a schematic block diagram of a memory garbage collection apparatus according to an embodiment of this application.
[0039] Figure 22 This is a schematic block diagram of a memory garbage collection apparatus according to an embodiment of this application. Detailed Implementation
[0040] The technical solutions in this application will now be described with reference to the accompanying drawings.
[0041] Figure 3This is a schematic flowchart of a memory garbage collection method according to an embodiment of this application. Figure 3 The methods include:
[0042] 110. Based on the application's marked nodes and the reference count values of the marked nodes, determine whether the marked nodes are located in the garbage RC ring, where nodes in the garbage RC ring have no external references.
[0043] For example, such as Figure 4 As shown, nodes A and B reference each other, and there are no other external references pointing to nodes A and B. Therefore, A and B form a garbage RC ring.
[0044] In addition, such as Figure 5 As shown, nodes A, B, and C reference each other, and there are no external references pointing to these three nodes. Therefore, nodes A, B, and C also form a garbage RC ring.
[0045] like Figure 6 As shown, nodes A, B, and C reference each other, and there are external references pointing to A. Therefore, nodes A, B, and C form a non-garbage RC ring.
[0046] The aforementioned garbage RC rings can be considered as memory nodes unrelated to application execution, while non-garbage RC rings can be considered as memory nodes related to application execution.
[0047] Alternatively, marking nodes in the application can be achieved by calling built-in functions. For example, in the JavaScript virtual machine, the built-in function `SetCycleHeader` can be added to mark a specific node. During application runtime, this built-in function is executed, setting the `is_root` flag in the memory header of the node in the RC ring to `true`, and adding the node to the marked node set (also known as a circular linked list set), so that it can be determined whether each node in the marked node set is located in the RC ring.
[0048] 120. If the marked node is located in the garbage RC ring, reclaim the nodes in the garbage RC ring.
[0049] In this application, it is possible to determine whether a marker node is located within a waste RC ring by using the marker node and its reference count value, and if the marker node is located within the waste RC ring, the waste RC ring can be recycled.
[0050] The aforementioned marked nodes can be obtained by marking nodes in potential RC rings. Compared with the existing technology of reclaiming garbage RC rings by marking strong references, weak references, and unowned references, this method greatly simplifies the marking rules and makes it easier to implement. Specifically, the existing technology needs to consider different situations to set weak references or unowned references, and the rules are relatively cumbersome and strict. Incorrect settings may lead to serious memory problems. However, in this application, it is only necessary to mark nodes in suspected RC rings. Missing or incorrect marking will not affect the reclamation of memory garbage.
[0051] Optionally, the aforementioned marked nodes can be nodes obtained by the user marking nodes in the application that may form RC loops. Specifically, the user can mark nodes that may be located in RC loops during the program development phase and add the marked nodes to a marked node set (this marked node set can contain multiple marked nodes). Next, using... Figure 3 The method processes each marked node to determine whether each marked node is located in the garbage RC ring.
[0052] It should be understood that by marking any node in the RC ring as a marked node, it is possible to traverse to all other nodes in the RC ring from that marked node.
[0053] The program developers mark the nodes in the possible RC loops to obtain marked nodes. This can reduce the detection range of RC loop nodes, reduce the memory consumption of the algorithm, and improve real-time performance.
[0054] Optionally, Figure 3 The method further includes: determining a set of marked nodes, wherein the set of marked nodes contains multiple nodes, and the marked node is any one of the multiple nodes. It should be understood that after marking the nodes in a possible RC ring, multiple marked nodes can be obtained, and combining the multiple marked nodes together yields the set of marked nodes.
[0055] Optionally, based on the application's marked node and the reference count value of the marked node, it is determined whether the marked node is located in the garbage RC ring. Specifically, this includes: decrementing the reference count value of the current node's child nodes by 1; if a first target child node with a reference count value of 0 appears among the current node's child nodes, then the first target child node is taken as the current node, and this step is repeated until the target condition is met; if no first target child node appears among the current node's child nodes, then this step is exited. Here, the marked node is the initial value of the current node, and the target condition is that a marked node appears among the first target child nodes of the current node; if the target condition is met, it is determined that the marked node is located in the garbage RC ring.
[0056] The process of decrementing the reference count of the current node's child nodes by 1, and then, if a first target child node appears among the current child node's child nodes, making that first target child node the current node and continuing to decrement the reference count of the current node by 1, can be called a decrement operation. This decrement operation makes it easier to determine whether a marked node is located in a garbage RC ring, so that once it is determined that the marked node is in the garbage RC ring, the nodes in the garbage RC ring can be reclaimed.
[0057] To better understand the subtraction operation, the principle of subtraction will be explained below with pseudocode.
[0058] The relevant pseudocode for the subtraction operation is as follows:
[0059]
[0060]
[0061] Specifically, when processing the above-mentioned marked nodes using the subtraction operation, the set of marked nodes (cycle_roots) is first traversed, and then the reference count (ref) of each marked node (root) and its referenced child nodes is decremented (Decrease(root)).
[0062] When traversing the set of marked nodes, if the reference count of a marked node is 0, then it is removed from the set of marked nodes and the next marked node in the set is processed; if the reference count of a marked node is not 0, then the child nodes of the marked node are processed and the marked node is set as a node that has undergone the subtraction operation (root.is_decreased=true).
[0063] When processing the child nodes of a marked node, if the ref of a child node is not equal to 0 after setting the ref of the marked node to ref-1, then the traversal of the branch of that child node stops. If the ref of a child node is 0 after setting the ref of the marked node to ref-1, and the child node is not a node that has already undergone the subtraction operation, then the direct child nodes of that child node need to be further decremented by 1. If the ref of a child node is 0 after setting the ref of the marked node to ref-1, but the child node is a node that has already undergone the subtraction operation, then the traversal of the branch of that child node stops.
[0064] The following is combined Figures 7 to 9 The process of subtraction is explained in detail.
[0065] like Figure 7As shown, A is a marker node. The specific process for determining whether A is located in the waste RC ring is as follows:
[0066] Decrement the reference count of node B, a child of node A, by 1, making node B's reference count 0. Since B's reference count becomes 0 after decrementing, next decrement the reference count of node C, a child of node B, by 1, making C's reference count 0. Then decrement the reference count of node A, a child of node C, by 1, making A's reference count 0. After these subtraction operations, the reference counts of these three nodes are as follows: Figure 8 As shown. By Figure 8 As we can see, after subtracting the reference counts generated by mutual references between A, B, and C, the reference counts of all three nodes are 0. Therefore, A is located in the RC ring, and this RC ring is a garbage RC ring. Next, we can reclaim the garbage RC ring containing A.
[0067] like Figure 9 As shown, A is a marker node. The specific process for determining whether A is located in the waste RC ring is as follows:
[0068] Decrementing the reference count of child node B of node A by 1 changes B's reference count from 2 to 1. Since A has only one child node B, and B's reference count is not equal to 0 after the subtraction operation, no further subtraction operation is performed on B's child nodes. After the subtraction operation, the reference counts of the three nodes are as follows: Figure 10 As shown, Figure 10 As shown, A, B, and C form an RC ring. However, because B has external references, this RC ring is not a garbage RC ring. In other words, node A is not located in a garbage RC ring, but in a non-garbage RC ring. Therefore, the non-garbage RC ring containing A cannot be reclaimed.
[0069] By performing a subtraction operation, the reference count of the nodes in the garbage RC ring is cleared to zero. In other words, after the subtraction operation, the reference count of all nodes in the garbage RC ring will be 0, and then the nodes in the garbage RC ring can be reclaimed.
[0070] It should be understood that when marking nodes, at least one node should be marked for each RC ring. Marking multiple nodes in an RC ring will not affect memory garbage collection. However, if no nodes in an RC ring are marked, that RC ring cannot be collected, leading to memory leaks. Furthermore, to improve the efficiency of garbage collection, if the RC ring is known, only one node in that RC ring can be marked. Conversely, if it's uncertain whether a node is in the RC ring, that node should also be marked. Mistakenly marking a node as part of the RC ring will not affect memory garbage collection.
[0071] For example, if a marked node is a mislabeled non-garbage RC node, and the reference count of the marked node has become 0 before the above subtraction operation is performed on it, then it can be deleted from the marked node set and the marked node can be reclaimed using the normal RC mechanism in the prior art (when the reference count of a node becomes 0, the node is reclaimed).
[0072] It should be understood that in this application, after subtracting the reference count value of a node in the RC ring where the marked node is located, if the reference count value of that node is 0, Figure 3 The method does not immediately reclaim the node, but instead reclaims all nodes in the garbage RC ring after determining that the marked node is in the garbage RC ring, or restores the reference count value of the marked node and its child nodes that was subtracted due to the subtraction operation when it is determined that the marked node is not in the garbage RC ring.
[0073] Optionally, in some embodiments, Figure 3 The method also includes: determining that the marked node is located in a non-garbage RC ring if the above target conditions are not met; and restoring the reference count values of the current node and its child nodes.
[0074] When a marked node is not in a garbage RC ring, the reference count of the non-garbage RC ring can be restored by restoring the current node and its reference count value, thereby ensuring the correctness of the reference relationships of other non-garbage RC ring nodes after the garbage collection operation is performed.
[0075] For example, in the case of Figure 9 After performing a subtraction operation on the marked node and its child nodes, the reference count values of each node are as follows: Figure 10 As shown, after decrementing the reference count of A's child node B by 1, B's reference count is no longer 0. Therefore, it is finally determined that A is not located in the RC ring. Next, it is necessary to restore B's reference count to 2 before the subtraction operation.
[0076] Optionally, the reference count values of the current node and its child nodes are restored. Specifically, this includes: incrementing the reference count value of the current node's child nodes by 1; if a second target child node with a reference count value of 1 appears among the current node's child nodes, then the second target child node is used as the current node, and this step is re-executed; if no second target child node appears among the current node's child nodes, then this step is exited.
[0077] Optionally, after determining that a marked node is in a garbage RC ring, the garbage RC ring can be added to the garbage node set. Then, similar processing can be performed on other marked nodes, adding the garbage RC rings of other nodes to the garbage node set, and then recycling the nodes in the garbage node set.
[0078] It should be understood that in this application, all the garbage RC rings where the marked nodes are located can be added to the garbage node set before the nodes in the garbage node set are recycled, or the nodes in the garbage RC ring can be recycled directly after the garbage RC ring where a marked node is located is determined.
[0079] For example, such as Figure 10 As shown, after the subtraction operation, the reference count of node B returned to 1 from 2. Next, the reference count of node B needs to be restored. Figure 11 As shown, the specific process of restoring the reference count value of node B is as follows: Starting from the marked node A, increment the reference count value of A's child node B by 1, so that the reference count value of B becomes 2. Since the reference count value of B is 1, the process of restoring the reference count value ends.
[0080] It should be understood that the above-mentioned incrementing of the reference count of the current node's child nodes to restore the node's reference count can be called a restore operation. It should also be understood that, to avoid performing repeated restore operations, the need_restore flag can be set during the restore operation; after performing a restore operation on a node, the need_restore flag of that node is set to true.
[0081] To better understand the restore operation, the principle of the subtraction operation will be explained below with pseudocode.
[0082] The pseudocode related to the recovery operation is as follows:
[0083]
[0084] Specifically, when processing the marked nodes using the recovery operation, the first step is to traverse the set of marked nodes (cycle_roots), and then perform the recovery operation on the reference count of each node with a non-zero reference count and its child nodes.
[0085] When performing the recovery operation, after setting the ref of the child nodes of the marked node (whose reference count is not 0) to ref+1, if the ref of the child node is not 1, then the traversal of the branch of the child node is stopped; after setting the ref of the child nodes of the marked node to ref+1, if the ref of the child node is 1, that is, changed from 0 to 1, then the child nodes of the child node are further incremented by 1.
[0086] It should be understood that the recovery operation is equivalent to the inverse operation of the subtraction operation. When the marked node is located in the garbage RC ring, after performing the subtraction operation on the garbage RC ring, no recovery operation is performed on the garbage RC ring. However, when the marked node is located in the non-garbage RC ring, after performing the subtraction operation on the non-garbage RC ring, a recovery operation is still required on the non-garbage RC ring to restore the reference count value of the nodes in the non-garbage RC ring to the value before the subtraction operation, so as to ensure the correctness of the reference relationship of the non-garbage RC ring after the recycling operation.
[0087] Once it's determined that the marked node is in the garbage RC ring, the nodes in the garbage RC ring can be reclaimed. The principle of the reclamation operation is explained below with pseudocode.
[0088] The pseudocode related to the recycling operation is as follows:
[0089]
[0090] After traversing the set of marked nodes and performing subtraction and restoration operations, only the refs of garbage RC ring nodes (including the marked nodes used to mark the garbage RC ring) are cleared to zero. To prevent duplicate collections caused by multiple nodes within the same RC ring and circular references within the ring itself, an `is_collected` flag is set. This flag is set to true when collecting a node, and false before collection to allow it to be added to the garbage node set. Nodes with ref of 0 and their child nodes with ref of 0 are collected, released, and removed from the marked node set, completing this garbage RC ring collection. The remaining nodes in the marked node set, except for the `is_root` flag, are then cleared, awaiting the next garbage RC ring collection algorithm call.
[0091] Optionally, four flag bits can be set in the reserved bits of the memory header of each node for use during garbage collection. The meaning of each flag bit of the node is as follows:
[0092]
[0093] The following is combined Figures 12 to 20 The memory garbage collection method of the embodiments of this application will be described in detail.
[0094] Figure 12 This illustrates the entire process of garbage collection in memory. The process specifically includes:
[0095] 210. Mark the nodes in the application to obtain marked nodes.
[0096] 220. Determine the set of marked nodes {R1, R2, R3}.
[0097] exist Figure 13 In the diagram, R1, R2, and R3 are potential RC (Recursive Concurrent) nodes added to the marked node, and AE are other nodes that have reference relationships with the marked node during recursive traversal. Circular reference relationships include [R1, B], [R1, C, B], and [R2, R3]. Assume the subtraction operation is performed in the order R1-R2-R3.
[0098] 230. Perform a subtraction operation:
[0099] The specific subtraction operation involves performing subtraction operations on R1, R2, and R3 in sequence.
[0100] Performing a subtraction operation on R1 and its child nodes specifically includes:
[0101] (1) Decrement the reference counts of child nodes A, B, and C of R1 by 1 to obtain... Figure 14 The results shown;
[0102] (2) Since the reference count of C becomes 0, the reference counts of C's child nodes B and R2 are decremented by 1, resulting in... Figure 15 The results shown;
[0103] (3) Since the reference count of B becomes 0, the reference count of B's child node R1 is decremented by 1, resulting in... Figure 16 The results are shown.
[0104] Since subtraction has already been performed on R1, the subtraction operation on node R1 and its child nodes is terminated.
[0105] Performing a subtraction operation on R2 and its child nodes specifically includes:
[0106] (4) Decrement the reference count of R3, a child node of R2, by 1 to obtain... Figure 17 The results shown;
[0107] (5) Since the reference count of R3 becomes 0, the reference counts of R3's child nodes R2, D, and E are decremented by 1, resulting in... Figure 18 The results are shown.
[0108] Since subtraction has already been performed on R2, the subtraction operation on node R2 and its child nodes is terminated.
[0109] Performing a subtraction operation on R3 and its child nodes specifically includes:
[0110] Since a subtraction operation has already been performed on R3 in step 220, and the reference count of R3 is 0, no further subtraction operation will be performed on R3 and its child nodes. R3 will be removed from the set of marked nodes, thus completing this subtraction operation.
[0111] 240. Traverse the set of marked nodes and perform recovery operations on the marked nodes and their child nodes whose reference counts are not 0.
[0112] Specifically, after the subtraction operation, the marked nodes in the marked node set are traversed. Since the reference count values of R1 and R3 become 0 and the reference count value of R2 becomes 1, it is only necessary to set the need_restore flag of R2 to true. In this way, the restoration operation only needs to be performed on node R2 whose need_restore flag is true.
[0113] The specific process for restoring R2 is as follows:
[0114] (6) Increment the reference count of R3, the child node of R2, by 1 to obtain... Figure 19 The results shown;
[0115] (7) Increment the reference count of R3's child nodes R2, D, and E by 1 to obtain... Figure 20 The results are shown.
[0116] Since the reference count of R2 is not 1 after being incremented by 1, the recovery operation for R2 and its child nodes is completed.
[0117] 250. Collect garbage RC rings.
[0118] 260. Recycle the nodes in the waste RC ring.
[0119] like Figure 20As shown, after the subtraction and recovery operations, the reference count of the marked node R1 becomes 0. Therefore, R1 and its child nodes B and C with a reference count of 0 are collected. However, since child node A still has external references, its reference count is not set to 0. Thus, the nodes in the garbage ring [R1, C, B] can be collected and released. In the RC ring [R2, R3], since R2 still has external references, its reference count is not set to zero. Therefore, it is a non-garbage RC ring and will be checked again later.
[0120] The above text combined Figures 3 to 20 The memory garbage collection method of the embodiments of this application is described in detail below. Figures 21 to 22 The apparatus for memory garbage collection according to embodiments of this application is described. It should be understood that... Figure 21 and Figure 22 The device described above can implement all the steps of the memory garbage collection method described above. For the sake of brevity, repeated descriptions will be omitted below.
[0121] Figure 21 This is a schematic block diagram of a memory garbage collection apparatus according to an embodiment of this application. Figure 21 The device 300 can perform the above. Figure 3 A method for garbage collection of memory. The device 300 includes:
[0122] The determining module 310 is used to determine whether the marked node is located in a garbage circular reference (RC ring) based on the marked node of the application and the reference count value of the marked node, wherein the nodes in the garbage RC ring have no external references;
[0123] The recycling module 320 is used to recycle nodes in the waste RC ring when the marked node is located in the waste RC ring.
[0124] Optionally, as an embodiment, the determining module 310 is specifically used to: decrement the reference count of the child nodes of the current node by 1; if a first target child node with a reference count of 0 appears among the child nodes of the current node, then the first target child node is taken as the current node, and this step is repeated until the target condition is met; if the first target child node does not appear among the child nodes of the current node, then this step is exited, wherein the marked node is the initial value of the current node, and the target condition is that the marked node appears among the first target child nodes of the current node; if the target condition is met, the marked node is determined to be located in the garbage RC ring.
[0125] Optionally, as an embodiment, the determining module 310 is further configured to: determine that the marked node is located in a non-garbage RC ring if the target condition is not met;
[0126] The device 300 further includes:
[0127] The recovery module 320 is used to recover the reference count values of the current node and its child nodes.
[0128] Optionally, as an embodiment, the recovery module 320 is specifically used to: increment the reference count of the child nodes of the current node by 1; if a second target child node with a reference count of 1 appears among the child nodes of the current node, then the second target child node is used as the current node and the current step is re-executed; if the second target child node does not appear among the child nodes of the current node, then the current step is exited.
[0129] Optionally, as an embodiment, the determining module 310 is further configured to determine a set of marked nodes, wherein the set of marked nodes contains multiple nodes, and the marked node is any one of the multiple nodes.
[0130] Figure 22 This is a schematic block diagram of a memory garbage collection apparatus according to an embodiment of this application. Figure 22 The device 400 can perform the above. Figure 3 A method for garbage collection of memory. The device 400 includes:
[0131] Memory 410 is used to store programs;
[0132] The processor 420 is configured to execute a program stored in the memory 410. When the program is executed, the processor 420 is specifically configured to: determine whether the marked node is located in a garbage circular reference (RC ring) based on the marked node of the application and the reference count value of the marked node, wherein the nodes in the garbage RC ring have no external references; and reclaim the nodes in the garbage RC ring when the marked node is located in the garbage RC ring.
[0133] Optionally, as an embodiment, the processor 420 is specifically configured to: decrement the reference count of the child nodes of the current node by 1; if a first target child node with a reference count of 0 appears among the child nodes of the current node, then the first target child node is used as the current node, and this step is re-executed until the target condition is met; if the first target child node does not appear among the child nodes of the current node, then this step is exited, wherein the marked node is the initial value of the current node, and the target condition is that the marked node appears among the first target child nodes of the current node; if the target condition is met, the marked node is determined to be located in the garbage RC ring.
[0134] Optionally, as an embodiment, the processor 420 is further configured to: determine that the marked node is located in a non-garbage RC ring if the target condition is not met; and restore the reference count values of the current node and its child nodes.
[0135] Optionally, as an embodiment, the processor 420 is specifically configured to: increment the reference count of the child nodes of the current node by 1; if a second target child node with a reference count of 1 appears among the child nodes of the current node, then the second target child node is used as the current node and the current step is re-executed; if the second target child node does not appear among the child nodes of the current node, then the current step is exited.
[0136] Optionally, as an embodiment, the processor 420 is further configured to: determine a set of marked nodes, wherein the set of marked nodes contains a plurality of nodes, and the marked node is any one of the plurality of nodes.
[0137] Optionally, the aforementioned devices 300 and 400 may specifically be IoT devices or other devices with limited memory resources.
[0138] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0139] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0140] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of 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 system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0141] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0142] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0143] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) 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.
[0144] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A method for garbage collection of memory, characterized in that, include: Decrement the reference count of the current node's child nodes by 1. If the current node's child nodes contain a first target child node with a reference count of 0, then use the first target child node as the current node and repeat this step until the target condition is met. If the current node's child nodes do not contain the first target child node, then exit this step. Here, the marked node is a node obtained by marking nodes in the application that may form a circular reference (RC) ring. The marked node is the initial value of the current node, and the target condition is that the marked node appears in the first target child node of the current node. Under the condition that the target condition is met, it is determined that the marked node is located in the garbage RC ring, and none of the nodes in the garbage RC ring have external references; When the marked node is located in the waste RC ring, the node in the waste RC ring is recycled.
2. The method as described in claim 1, characterized in that, The method further includes: If the target condition is not met, the marked node is determined to be located in a non-garbage RC ring; Restore the reference count values of the current node and its child nodes.
3. The method as described in claim 2, characterized in that, The process of restoring the reference count values of the current node and its child nodes includes: Increment the reference count of the child nodes of the current node by 1. If a second target child node with a reference count of 1 appears among the child nodes of the current node, then the second target child node is used as the current node and this step is repeated. If the second target child node does not appear among the child nodes of the current node, then exit this step.
4. The method according to any one of claims 1-3, characterized in that, The method further includes: Determine a set of marked nodes, wherein the set of marked nodes contains multiple nodes, and the marked node is any one of the multiple nodes.
5. A device for memory garbage collection, characterized in that, include: The determination module is used to decrement the reference count of the child nodes of the current node by 1. If the first target child node with a reference count of 0 appears among the child nodes of the current node, the first target child node is taken as the current node and this step is repeated until the target condition is met. If the first target child node does not appear among the child nodes of the current node, this step is exited. Here, the marked node is a node obtained by marking nodes in the application that may form a circular reference (RC) ring. The marked node is the initial value of the current node, and the target condition is that the marked node appears in the first target child node of the current node. Under the condition that the target condition is met, it is determined that the marked node is located in the garbage RC ring, and none of the nodes in the garbage RC ring have external references; The recycling module is used to recycle nodes in the waste RC ring when the marked node is located in the waste RC ring.
6. The apparatus as claimed in claim 5, characterized in that, The determining module is also used for: If the target condition is not met, the marked node is determined to be located in a non-garbage RC ring; The device further includes: The recovery module is used to restore the reference count values of the current node and its child nodes.
7. The apparatus as claimed in claim 6, characterized in that, The recovery module is specifically used for: Increment the reference count of the child nodes of the current node by 1. If a second target child node with a reference count of 1 appears among the child nodes of the current node, then the second target child node is used as the current node and this step is repeated. If the second target child node does not appear among the child nodes of the current node, then exit this step.
8. The apparatus as described in any one of claims 5-7, characterized in that, The determining module is further configured to determine a set of marked nodes, wherein the set of marked nodes contains multiple nodes, and the marked node is any one of the multiple nodes.
9. A device for memory garbage collection, characterized in that, include: A processor and a memory, the memory being used to store a computer-executable program, the processor being used to read the computer-executable program in the memory and implement the method as described in any one of claims 1-4.
10. A computer storage medium, characterized in that, The computer storage medium stores a computer-executable program, which, when executed by a computer, causes the computer to perform the method as described in any one of claims 1-4.
Citation Information
Patent Citations
Garbage recycling method for Java virtual machine and monitoring system thereof
CN102929699A
Managing object lifetime in a cyclic graph
US9613073B2
System and method of computer automatic memory management
CN101046755A
Garbage object detecting method and device
CN103226476A