Cyclic reference detection method and device
By constructing an object reference relationship diagram and using strong connectivity component detection technology, the accuracy and efficiency of circular reference detection in the existing technology are solved, and a detection method for accurately identifying the huge circular reference relationship during the program operation stage is realized.
Patent Information
- Application Number
- CN202311525372.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-15
- Publication Date
- 2025-05-23
AI Technical Summary
When detecting circular references in the prior art, static code analysis tools are not suitable for dynamic code and program operation stages. The methods of the dedicated library have problems such as insecure detection results, inaccurateness, and inability to detect large circular reference relationships.
By constructing an object reference relationship diagram, using strong connectivity component detection technology, a huge circular reference relationship is identified during the program operation, improving the accuracy of the detection results.
It realizes the detection of circular references of program code into strong connected component detection that converts the detection of object reference relationship diagram, effectively improving the accuracy and efficiency of detection results.
Smart Images

Figure CN120029874A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for detecting circular references. Background Art
[0002] In the application development scenario, memory limit overrun is an important cause of application crashes. For object-oriented programming, if two objects strongly reference each other, a circular reference will be formed, and the garbage collection mechanism will not be able to recycle these objects, which will cause memory leaks, and eventually lead to memory limit overruns and application crashes. To this end, it is necessary to detect circular references in the program and identify the reference relationship between objects. Currently, the commonly used detection methods mainly include using the static code analysis tool that comes with the integrated development tool to perform static detection of circular references on program code, or using a dedicated library for detecting circular references to detect circular references on specified objects during the program running phase.
[0003] In the process of implementing the present invention, the inventors found that the prior art has the following problems:
[0004] The method of static code analysis tools is not suitable for detecting dynamic code and program running stages; although the method of dedicated libraries is suitable for dynamic detection, there are problems such as multiple detection results for the same program code are not unique, the detection results are inaccurate, and it is impossible to detect large circular reference relationships in the program. Therefore, these two methods cannot meet the usage requirements. Summary of the invention
[0005] In view of this, an embodiment of the present invention provides a method and device for detecting circular references, which realizes the conversion of the detection of circular references in program code into the detection of strongly connected components of an object reference relationship graph. During the program running process, the extremely large circular reference relationship is detected through the object reference relationship graph, which effectively improves the accuracy of the detection results.
[0006] In response to receiving a detection request for a program code circular reference, determining an object to be detected;
[0007] Parsing the object to be detected and constructing an object reference relationship graph;
[0008] According to the program code where the object to be detected is executed, the nodes in the object reference relationship graph are accessed and marked, and the strongly connected components are determined according to the result of the marking process, so as to obtain the circular reference corresponding to the detection request.
[0009] Optionally, determining the object to be detected includes: determining a method for acquiring the object to be detected according to the detection request; when the acquisition method is directional scanning, acquiring the object to be detected from a specified storage medium; when the acquisition method is global scanning, acquiring a memory snapshot of the program when the detection request is received, parsing the memory snapshot, and obtaining the object to be detected.
[0010] Optionally, after determining the objects to be detected, the method further includes: filtering the objects to be detected according to a preset screening condition, and filtering out objects that will not generate circular references in the objects to be detected.
[0011] Optionally, the object to be detected is parsed to construct an object reference relationship graph, including: determining the type of the object to be detected; parsing the object to be detected based on the type to obtain a strong reference object of the object to be detected; determining the strong reference relationship between the object to be detected and the strong reference object of the object to be detected based on the object to be detected and the strong reference object of the object to be detected to obtain an object reference relationship graph.
[0012] Optionally, according to the type, the object to be detected is parsed to obtain a strong reference object of the object to be detected, including: when the type of the object to be detected is direct holding, obtaining the strong reference object of the object to be detected according to the reference table of the object to be detected; when the type of the object to be detected is a structure type, obtaining the strong reference object of the object to be detected through the object pointer of the object to be detected; when the type of the object to be detected is a collection type, traversing and obtaining the strong reference object of each element in the object to be detected, the strong references of each element constitute the strong reference object of the object to be detected.
[0013] Optionally, according to the program code where the object to be detected is executed, the nodes in the object reference relationship graph are accessed and marked, including: establishing an access record for storing access nodes; by executing the program code, the nodes in the object reference relationship graph are accessed, the accessed nodes are recorded in the access record, and according to the access record, the accessed nodes are marked.
[0014] Optionally, by executing the program code, node access is performed on the nodes in the object reference relationship graph, the visited nodes are recorded in the access record, and according to the access record, the visited nodes are marked, including: according to the program code and a preset recursive depth, the root node in the object reference relationship graph is checked, the root node is marked using a preset backtracking value and a preset index value, and the root node is added to the access record; by executing the program code, the adjacent nodes of the root node in the object reference relationship graph are traversed and accessed; according to the access record, it is determined whether to record the visited adjacent nodes in the access record, and the backtracking value and index value of the adjacent nodes are determined, and the adjacent nodes are marked using the backtracking value and index value of the adjacent nodes, so as to determine the strongly connected components according to the backtracking value and the index value.
[0015] Optionally, when the number of objects to be detected exceeds a preset concurrency threshold, the method further includes: adding multiple objects to be detected to a pre-established parallel queue; assigning a corresponding sub-thread to each object to be detected in the parallel queue according to a preset number of sub-threads, and generating a detection task for each object to be detected; and executing the detection task of each object to be detected in parallel through each sub-thread.
[0016] Optionally, the execution of the detection task of the object to be detected includes updating the access record used to record the access node; before executing the detection tasks of each object to be detected in parallel through each sub-thread, the method also includes: locking the access record so that only one sub-thread's detection task has the authority to update the access record within a time period.
[0017] According to a second aspect of an embodiment of the present invention, a circular reference detection device is provided, comprising:
[0018] A module for determining an object to be detected, configured to determine an object to be detected in response to receiving a detection request for a circular reference of a program code;
[0019] A reference relationship graph construction module is used to parse the object to be detected and construct an object reference relationship graph;
[0020] The circular reference acquisition module is used to perform node access and marking processing on the nodes in the object reference relationship graph according to the program code where the object to be detected is executed, determine the strongly connected components according to the result of the marking processing, and then obtain the circular reference corresponding to the detection request.
[0021] According to a third aspect of an embodiment of the present invention, there is provided an electronic device for circular reference, including:
[0022] one or more processors;
[0023] a storage device for storing one or more programs,
[0024] When the one or more programs are executed by the one or more processors, the one or more processors implement the method provided in the first aspect of the embodiment of the present invention.
[0025] According to a fourth aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method provided by the first aspect of the embodiment of the present invention is implemented.
[0026] An embodiment of the invention has the following advantages or beneficial effects: by responding to a detection request for circular references in program code, an object to be detected is determined; the object to be detected is parsed and an object reference relationship graph is constructed; according to the execution of the program code where the object to be detected is located, nodes in the object reference relationship graph are accessed and marked, and strongly connected components are determined according to the results of the marking process, thereby obtaining a technical solution for circular references corresponding to the detection request, thereby realizing the conversion of the detection of circular references in program code into the detection of strongly connected components in the object reference relationship graph, and detecting extremely large circular reference relationships through the object reference relationship graph during program running, thereby effectively improving the accuracy of the detection results. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0028] Figure 1 is a schematic diagram of the main process of a method for detecting circular references according to an embodiment of the present invention;
[0029] Figure 2 is a detailed flowchart of a method for detecting circular references according to an embodiment of the present invention;
[0030] Figure 3 It is a schematic diagram of the overall flow of the method for detecting circular references according to an embodiment of the present invention;
[0031] Figure 4 is a schematic diagram of main modules of a circular reference detection device according to an embodiment of the present invention;
[0032] Figure 5 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0033] Figure 6 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0034] It should be noted that the acquisition, storage and application of user personal information involved in the technical solution of the present disclosure are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.
[0035] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0036] Among the existing circular reference methods, the method of static code analysis tools is not suitable for detecting dynamic code and program running stages; although the method of dedicated libraries is suitable for dynamic detection, there are problems such as multiple detection results for the same program code are not unique, the detection results are inaccurate, and it is impossible to detect large circular reference relationships in the program. Therefore, these two methods cannot meet the usage requirements.
[0037] In order to solve the above problems existing in the prior art, the present invention proposes a method for detecting circular references. Based on the determined objects to be detected, an object reference relationship graph is constructed. During the running phase of the program, the circular references of the program code are obtained by searching for strongly connected components in the object reference relationship graph. This realizes the conversion of the detection of circular references of the program code into the detection of strongly connected components of the object reference relationship graph. During the running of the program, the extremely large circular reference relationship is detected through the object reference relationship graph, which effectively improves the accuracy of the detection result.
[0038] In the introduction to the embodiments of the present invention, the terms and their meanings are as follows:
[0039] Strong reference: A strong reference means that object A holds a pointer to object B, and does not allow object B to be released when object A exists. Strong references can protect the referenced object from being recycled by the system, but if used improperly, it may cause memory to not be released, resulting in memory leaks, especially in the case of circular references;
[0040] Circular reference: A circular reference refers to two or more objects that strongly reference each other to form a closed loop, causing their reference counts to not drop to 0 correctly and cannot be correctly recycled by the system. In a circular reference, all objects are strongly referenced to each other;
[0041] Large reference loop: In an object reference graph, there may be multiple reference loops, and sub-loops with common nodes can be regarded as part of a larger reference loop. When a reference loop cannot be expanded any further, it becomes a large reference loop. There may be multiple large reference loops in an object reference graph, and they cannot be connected in both directions.
[0042] Strong connectivity: used to describe the connectivity between nodes in a directed graph. In a directed graph, if there is a directed path from node A to node B, and there is also a directed path from node B to node A, then we say that node A and node B are strongly connected. In other words, the two nodes can reach each other without any one-way barriers.
[0043] Strongly connected component: A strongly connected component refers to a maximal strongly connected subgraph in a directed graph. It has the following property: for any two vertices, there is a directed path from one vertex to the other. Here, it can be understood as a set of objects in an object network where any two objects can reach each other through a series of reference relationships, and each of them can directly or indirectly reference all other objects, forming a closed loop;
[0044] Memory snapshot: Instantly capture the memory state of an application at runtime and record the memory state of the entire application in a static binary form, including data, code, stack, global variables, etc. Memory snapshots can be used in many scenarios, such as debugging, performance analysis, security auditing, code coverage testing, etc.
[0045] Figure 1 is a schematic diagram of the main process of the method for detecting circular references according to an embodiment of the present invention. Figure 1 As shown, the circular reference detection method of the embodiment of the present invention includes the following steps S101 to S103.
[0046] Step S101 : in response to receiving a detection request for a program code circular reference, determining an object to be detected.
[0047] Specifically, in the field of programming, circular references are a common problem, especially when using object-oriented programming languages, where two or more objects strongly reference each other to form a closed loop, thus forming a circular reference. In order to avoid problems such as memory overrun and application crash caused by the circular reference relationship between objects during the running of the application, it is necessary to detect the circular reference relationship between objects in the program code. According to the received detection request for circular reference of the program code, the target program code to be detected is determined, and the object to be detected is determined from the target program code.
[0048] According to one embodiment of the present invention, determining the object to be detected includes: determining a method for acquiring the object to be detected according to the detection request; when the acquisition method is directional scanning, acquiring the object to be detected from a specified storage medium; when the acquisition method is global scanning, acquiring a memory snapshot of the program when the detection request is received, parsing the memory snapshot, and obtaining the object to be detected.
[0049] Specifically, the embodiment of the present invention provides two methods for acquiring the object to be detected according to the detection requirements, and attaches relevant information of the acquisition method when issuing a circular reference detection request to the detection system. When the detection system receives the detection request of the program code circular reference, it determines whether the acquisition method of the object to be detected is directional scanning or global scanning according to the relevant information of the acquisition method attached in the detection request. In the case where the acquisition method is directional scanning, the object to be detected is acquired from the specified storage medium according to the relevant information in the detection request, that is, the requester of the detection request specifies the object to be detected. In the case where the acquisition method is global scanning, all registered classes in the memory and all object areas in the memory stack are acquired by using the memory snapshot acquisition tool provided in the dedicated library to obtain a memory snapshot, and then all objects in the memory snapshot are parsed and symbolized by using a dedicated function to obtain the object to be detected, that is, the global scanning method can detect all objects involved in the current running state of the program code, which belongs to global detection.
[0050] According to another embodiment of the present invention, after determining the objects to be detected, the method further includes: filtering the objects to be detected according to preset screening conditions, and filtering out objects that will not generate circular references in the objects to be detected.
[0051] Specifically, considering that not all of the objects to be detected obtained above will generate circular references, some objects that will not generate circular references can be filtered out according to the type of the object. Of course, objects that will not generate circular references can also be filtered out according to the filtering rules customized by the requester of the detection request, or objects to be detected that are not of interest can be filtered out. This can also effectively alleviate the subsequent detection workload of circular references, improve detection efficiency, and save system resources.
[0052] Step S102: parse the object to be detected and construct an object reference relationship graph.
[0053] Specifically, based on the above-determined object to be detected, the embodiment of the present invention abstracts the detection of circular references as the problem of finding strongly connected components in a directed graph. For the directed graph of object reference relationships, considering that the strong reference relationship between objects may cause a circular reference problem, it is set to use the nodes in the graph to represent the objects, and the connecting edges between the nodes represent the strong reference relationship between the objects. For example, if object A holds object B, then there is an edge from node A to node B. In the embodiment of the present invention, the object to be detected is used as the root node, the reference relationship in the object to be detected is parsed, all adjacent child nodes of the root node are obtained, and the connection relationship between the root node and the child nodes is determined to obtain an object reference relationship graph.
[0054] According to one embodiment of the present invention, the object to be detected is parsed and an object reference relationship graph is constructed, including: determining the type of the object to be detected; parsing the object to be detected based on the type to obtain a strong reference object of the object to be detected; determining the strong reference relationship between the object to be detected and the object to be detected based on the object to be detected and the strong reference object of the object to be detected to obtain an object reference relationship graph.
[0055] Specifically, considering that different types of objects have different parsing methods, it is first necessary to determine the type of the object to be detected determined above, and according to the specific object type, use the corresponding parsing method to parse the object to be detected to obtain a strong reference object that has a strong reference relationship with the object to be detected; based on the object to be detected and its corresponding strong reference object, combined with the program code where they are located, determine the strong reference relationship between the object to be detected and the strong reference object; finally, use the object to be detected and the strong reference object as nodes of the object reference relationship graph, and use the strong reference relationship between them as the connecting edge between the nodes of the object reference relationship graph to construct an object application relationship graph.
[0056] According to another embodiment of the present invention, the object to be detected is parsed according to the type to obtain a strong reference object of the object to be detected, including: when the type of the object to be detected is direct holding, obtaining the strong reference object of the object to be detected according to the reference table of the object to be detected; when the type of the object to be detected is a structure type, obtaining the strong reference object of the object to be detected through the object pointer of the object to be detected; when the type of the object to be detected is a collection type, traversing and obtaining the strong reference object of each element in the object to be detected, the strong references of each element constitute the strong reference object of the object to be detected.
[0057] Specifically, for the core language Objective-C (an object-oriented programming language that extends C) developed by iOS (an input output system mobile operating system), the types of objects to be detected generally include: directly held types, structure types, and collection types. For directly held types, that is, when other objects are directly held through attributes or member variables, all other objects held by the object to be detected are obtained through the reference table of the object to be detected, including strong references and weak references; these held objects are filtered, objects held by weak references are filtered out, and objects held by strong references are retained. These objects held by strong references are strong reference objects of the object to be detected. For structure types, in iOS development, they correspond to block (closure) types. Blocks will hold objects used in code blocks. When a block of an object holds other objects, the object will also indirectly hold these objects. Through related functions, the number of variable pointers held by the block can be obtained. Since the entire structure of the block is continuously distributed in memory, all object pointers held by the block can be obtained by using the address of the block as the base address for memory offset, thereby obtaining all other objects held by the object. Similarly, these held objects can also be filtered, objects held by weak references are filtered out, and objects held by strong references are retained. For collection types, the objects to be detected of the collection type are traversed and visited, and conditional statements and type checks are used to determine whether the reference type of the current element is a strong reference. The objects held by the strong reference are added to the result collection to obtain the strong reference object of the object to be detected.
[0058] In addition, considering that the strongly referenced objects of the parsed objects to be detected may contain some types that will no longer strongly reference other objects, such as simple data types that only store strings or numbers, or common functional types, etc., the strongly referenced objects can be further filtered, specifically by adding a filter list.
[0059] Based on the object reference relationship graph constructed above, an adjacency list structure is used to abstractly describe the constructed object reference relationship graph. For example, the object name in the object reference relationship graph is described using objectName: NSString. In this way, a linguistic description of the object reference relationship graph is obtained to facilitate the subsequent determination of strongly connected components.
[0060] Step S103: perform node access and labeling processing on the nodes in the object reference relationship graph according to the program code where the object to be detected is executed, determine the strongly connected components according to the labeling processing results, and then obtain the circular reference corresponding to the detection request.
[0061] Specifically, during the execution of the program code, based on the depth-first search algorithm, starting from the root node in the object reference relationship graph, the accessed object is searched for the corresponding node in the object reference relationship graph, the node is accessed, and a marking process is performed on the accessed node to identify the access information of the node. According to the marking marks of each node, the strong connectivity between the nodes in the object reference relationship graph can be determined to obtain a strongly connected component. According to the definition of strongly connected components in graph theory, combined with the definition of circular references in the program field, it can be known that when the number of nodes in the strongly connected component is greater than 1, it can be considered that a circular reference is detected, so finally, according to the number of nodes included in the strongly connected component, it is determined whether the strongly connected component corresponds to a circular reference.
[0062] According to one embodiment of the present invention, node access and labeling processing are performed on the nodes in the object reference relationship graph based on the program code in which the object to be detected is executed, including: establishing an access record for storing access nodes; performing node access on the nodes in the object reference relationship graph by executing the program code, recording the accessed nodes in the access record, and labeling the accessed nodes based on the access record.
[0063] Specifically, an access record for storing access nodes shared by the circular reference detection system is established, and the access record may include information such as the accessed node identifier and the sequence number of the accessed node; when a node in the object reference relationship graph is accessed by executing program code, the relevant information of the accessed node is recorded in the access record, and the historical record in the access record is queried to determine the content of the label mark for the node based on the currently accessed node and the historical record in the access record.
[0064] According to another embodiment of the present invention, by executing the program code, node access is performed on the nodes in the object reference relationship graph, the visited nodes are recorded in the access record, and according to the access record, the visited nodes are marked, including: according to the program code and a preset recursive depth, the root node in the object reference relationship graph is verified, the root node is marked using a preset backtracking value and a preset index value, and the root node is added to the access record; by executing the program code, the adjacent nodes of the root node in the object reference relationship graph are traversed and accessed, and the visited adjacent nodes are recorded in the access record; according to the access record, the backtracking value and index value of the adjacent node are determined, and the adjacent node is marked using the backtracking value and index value of the adjacent node, so as to determine the strongly connected component according to the backtracking value and the index value.
[0065] Specifically, before detecting circular references, related auxiliary data structures need to be initialized. The embodiments of the present invention mainly involve storing a memory address set visitedNode of all visited nodes, a retainCycles array storing all strongly connected components in the object reference relationship graph, and a stack for temporarily storing all nodes on the current access path.
[0066] First, according to the program code and the preset recursive depth, judge and determine that the access depth of the root node in the object reference relationship graph is not greater than the recursive depth requirement. If it is greater, exit directly without further detection. After the root node is verified, use the preset backtracking value lowLink and index value index to mark the root node. In the embodiment of the present invention, the lowLink of the root node is set to index = 1, and then the root node is added to the stack and visitedNode. For the convenience of expression, the embodiment of the present invention refers to stack and visitedNode as access records.
[0067] Further, by executing the program code, all adjacent nodes of the root node in the object reference relationship graph are traversed, that is, the adjacent child nodes of the root node mentioned above. Each time an adjacent node is visited, it is determined whether the currently visited adjacent node has been saved in the visitedNode of the access record. If it has not been visited, the adjacent node is added to the stack and visitedNode, and according to the incremental rule of the backtracking value and the index value, 1 is added to the numerical value of the backtracking value and the index value of the last visited node to obtain the backtracking value and the index value of the current adjacent node, and the backtracking value and the index value of the current adjacent node are used for marking, and then the access to the next level of nodes is continued; if it has been visited and is in the current path stack, it indicates that there is a ring composed of nodes in the object reference graph, and at this time, the backtracking value of the upper node of the current adjacent node needs to be updated, and the minimum value of the index value index of the current adjacent node and the backtracking value lowLink of the upper node is used as the update value of the backtracking value of the upper node. For example, the access record of the nodes in the object reference graph is 5→6→7→8. When the adjacent node of node 8 is node 5, a cycle is formed. The initial backtracking value lowLink of 8 is 7, and the index value index of node 5 is 4. Since the adjacent node 5 of 8 has been visited, the lowLink of node 8 is updated to the minimum value of the index of node 5 and the lowLink of node 8, that is, the minimum value 4 of 4 and 7.
[0068] Furthermore, if the recursive traversal of the adjacent nodes of the root node is completed, it is necessary to backtrack the adjacent nodes in the loop step by step, and update the lowLink of the backtracking node to the minimum value of the lowLink of the backtracking node and the lowLink of the next level node during the traversal visit. For example, the lowLink of the backtracking node 7 is 6, and node 8 is the next level node, and its lowLink is 4 after the above update, so the lowLink of the backtracking node 7 is min(6,4)=4, and the backtracking is continued. If the lowLink of the last backtracking node is index after the backtracking is completed, it means that the current backtracking node is the root node of the strongly connected component, and it is looped out of the stack and saved in the temporarily established Cycles array to obtain the strongly connected component. Further, if the number in the Cycles array is greater than 1, it is determined that the object corresponding to the node stored in Cycles is a circular reference, and it is added to the retainCycles array to obtain the circular reference corresponding to the detection request.
[0069] It should be noted that the determination of the strongly connected components in the embodiment of the present invention can ensure that the corresponding circular reference is a maximal circular reference, that is, when there are multiple sub-rings with common nodes in the object reference graph, these sub-rings can be identified as a set, that is, a maximal circular reference. It can be understood that there can be multiple maximal circular references in a program code. When multiple maximal circular references are obtained, they can be sorted by size. Larger circular references usually indicate more serious dependency problems, so it can help us identify and resolve the highest priority circular dependencies.
[0070] In addition, the detection method of circular references in the embodiment of the present invention has a high detection efficiency, and the time complexity is O (the number of nodes in the object reference relationship graph + the number of connecting edges in the object reference relationship graph). While ensuring the detection accuracy, it also greatly improves the detection efficiency and saves system resources. It can be understood that in order to further reduce the space and time complexity, more efficient data structures (such as hash tables, self-balancing binary search trees, etc.) can be used, or optimized data structures (such as compact arrays) can be used to replace the data structures in the access records, such as using linked lists to implement the stack, because linked lists have a time complexity of O (1) when adding and deleting elements.
[0071] Figure 2It is a detailed flow chart of the detection method of circular references of an embodiment of the present invention. According to the constructed object reference relationship graph, the root node is determined, and the root node information is stored in the access record; the access starts from the root node and the root node is marked; by executing the program code, all adjacent nodes of the root node are traversed, the access record is queried, and it is determined whether the adjacent node has been visited and is in the current path. If it has not been visited, then the adjacent node is recorded in the access record, the backtracking value and index value of the adjacent node are determined, and the access to the next level node is prepared, and the adjacent node is marked using the determined backtracking value and index value. If it has been visited and is in the current path, it means that there is a cycle in the object reference graph. Then the backtracking value is updated according to the above method to determine the update backtracking value, and it is determined whether the current adjacent node is the root node of the strongly connected component. If not, it is necessary to backtrack through the adjacent nodes in the ring step by step, and update the backtracking value of the corresponding node until the lowLink of the backtracking node = index, indicating that the node is the root node of the strongly connected component. The stack is popped out of the loop to obtain the strongly connected component, and the strongly connected component with a node number greater than 1 is recorded as a circular reference.
[0072] According to another embodiment of the present invention, when the number of objects to be detected exceeds a preset concurrency threshold, the method further includes: adding multiple objects to be detected to a pre-established parallel queue; assigning a corresponding sub-thread to each object to be detected in the parallel queue according to a preset number of sub-threads, generating a detection task for each object to be detected; and executing the detection task of each object to be detected in parallel through each sub-thread.
[0073] Specifically, considering that when the above-mentioned method of acquiring the objects to be detected is global scanning, it is necessary to perform circular reference detection on a large number of objects to be detected. In order to ensure the detection efficiency, when the number of objects to be detected exceeds the preset concurrency threshold, a parallel queue suitable for executing concurrent tasks is created, and multiple objects to be detected are added to the parallel queue; according to the number of system sub-threads, a corresponding sub-thread is assigned to each object to be detected in the parallel queue, and a detection task for each object to be detected is generated, and circular reference detection is performed through multi-thread concurrency.
[0074] According to another embodiment of the present invention, the execution of the detection task of the object to be detected includes updating the access record used to record the access node; before the detection tasks of the object to be detected are executed in parallel by each sub-thread, the method also includes: locking the access record so that only one sub-thread's detection task has the authority to update the access record within a time period.
[0075] Specifically, considering that the concurrent detection of circular references by multiple threads will inevitably modify and update the shared access records, in order to avoid disordered occupation of access records and access conflicts by each sub-thread, the embodiment of the present invention locks the shared access records before executing the detection tasks of each object to be detected in parallel through each sub-thread. Subsequently, when each detection task needs to edit the query access record, it is necessary to apply for unlocking permission. Only tasks with unlocking permission can edit the query access record. This ensures that only one sub-thread detection task can update the query access record within a time period, thereby ensuring the data security and accuracy of the access record.
[0076] Figure 3 It is a schematic diagram of the overall process of the circular reference detection method of the embodiment of the present invention. According to the received detection request, determine whether the acquisition method of the object to be detected is global scanning or directional scanning, and obtain the corresponding object to be detected according to different acquisition methods; filter the object to be detected, and filter out the objects that will not generate circular references in the object to be detected; parse the object to be detected and build an object reference relationship graph; according to the program code where the object to be detected is executed, perform node access and labeling processing on the nodes in the object reference relationship graph, and determine the strongly connected components according to the results of the labeling processing; export the objects corresponding to the strongly connected components to obtain the extremely large circular reference of the program code, so that the requester of the detection request can eliminate the appropriate nodes and connecting edges according to the obtained extremely large circular reference, break the circular reference, and solve the memory leak problem.
[0077] The embodiment of the present invention constructs an object reference relationship graph based on a determined object to be detected. During the running phase of the program, the circular reference relationship of the program code is obtained by searching for strongly connected components in the object reference relationship graph, thereby converting the detection of circular references in the program code into the detection of strongly connected components in the object reference relationship graph. During the running of the program, the extremely large circular reference relationship is detected through the object reference relationship graph, which effectively improves the accuracy and efficiency of the detection results and reduces the time consumption of the detection.
[0078] Figure 4 FIG. 1 is a schematic diagram of the main modules of a circular reference detection device according to an embodiment of the present invention. Figure 4 As shown, the circular reference detection device 400 mainly includes a to-be-detected object determination module 401 , a reference relationship graph construction module 402 and a circular reference relationship acquisition module 403 .
[0079] The to-be-detected object determination module 401 is used to determine the to-be-detected object in response to receiving a detection request for a program code circular reference;
[0080] A reference relationship graph construction module 402 is used to parse the object to be detected and construct an object reference relationship graph;
[0081] The circular reference acquisition module 403 is used to perform node access and marking processing on the nodes in the object reference relationship graph according to the program code where the object to be detected is executed, determine the strongly connected components according to the result of the marking processing, and then obtain the circular reference corresponding to the detection request.
[0082] According to one embodiment of the present invention, the module 401 for determining the object to be detected is further used to: determine a method for acquiring the object to be detected according to the detection request; when the acquisition method is directional scanning, acquire the object to be detected from a specified storage medium; when the acquisition method is global scanning, acquire a memory snapshot of the program when the detection request is received, and parse the memory snapshot to obtain the object to be detected.
[0083] According to another embodiment of the present invention, the circular reference detection device 400 also includes an object filtering module (not shown in the figure), which is used to: after determining the object to be detected, filter the object to be detected according to preset screening conditions, and filter out objects in the object to be detected that will not generate circular references.
[0084] According to another embodiment of the present invention, the reference relationship graph construction module 402 is also used to: determine the type of the object to be detected; parse the object to be detected according to the type to obtain a strong reference object of the object to be detected; determine the strong reference relationship between the object to be detected and the strong reference object of the object to be detected according to the object to be detected and the strong reference object of the object to be detected to obtain an object reference relationship graph.
[0085] According to another embodiment of the present invention, the reference relationship graph construction module 402 is also used for: when the type of the object to be detected is directly held, obtaining the strong reference object of the object to be detected according to the reference table of the object to be detected; when the type of the object to be detected is a structure type, obtaining the strong reference object of the object to be detected through the object pointer of the object to be detected; when the type of the object to be detected is a collection type, traversing and obtaining the strong reference object of each element in the object to be detected, and the strong references of each element constitute the strong reference object of the object to be detected.
[0086] According to another embodiment of the present invention, the circular reference acquisition module 403 is also used to: establish an access record for storing access nodes; perform node access on the nodes in the object reference relationship graph by executing the program code, record the accessed nodes in the access record, and mark the accessed nodes according to the access record.
[0087] According to another embodiment of the present invention, the circular reference acquisition module 403 is also used to: verify the root node in the object reference relationship graph according to the program code and a preset recursive depth, mark the root node using a preset backtracking value and a preset index value, and add the root node to the access record; traverse and access the adjacent nodes of the root node in the object reference relationship graph by executing the program code; determine whether to record the visited adjacent nodes in the access record according to the access record, and determine the backtracking value and index value of the adjacent nodes, and mark the adjacent nodes using the backtracking value and index value of the adjacent nodes, so as to determine the strongly connected components according to the backtracking value and the index value.
[0088] According to another embodiment of the present invention, when the number of objects to be detected exceeds a preset concurrency threshold, the circular reference detection device 400 also includes a parallel detection module (not shown in the figure), which is used to: add multiple objects to be detected to a pre-established parallel queue; assign a corresponding sub-thread to each object to be detected in the parallel queue according to a preset number of sub-threads, and generate a detection task for each object to be detected; and execute the detection task of each object to be detected in parallel through each sub-thread.
[0089] According to another embodiment of the present invention, the execution of the detection task of the object to be detected includes updating the access record used to record the access node; the circular reference detection device 400 also includes a locking module (not shown in the figure), which is used to: before executing the detection tasks of each object to be detected in parallel through each sub-thread, the access record is locked, so that within a time period, only one sub-thread's detection task has the authority to update the access record.
[0090] Figure 5 is an exemplary system architecture diagram to which embodiments of the present invention can be applied.
[0091] like Figure 5 As shown, system architecture 500 may include terminal devices 501, 502, 503, a network 504 and a server 505. Network 504 is used to provide a medium for communication links between terminal devices 501, 502, 503 and server 505. Network 504 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0092] Users can use terminal devices 501, 502, 503 to interact with server 505 via network 504 to receive or send messages, etc. Terminal devices 501, 502, 503 can be installed with various communication client applications, such as circular reference detection applications, etc. (only as an example).
[0093] The terminal devices 501 , 502 , and 503 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, and the like.
[0094] The server 505 may be a server that provides various services, such as a background management server that provides support for circular references made by users using terminal devices 501, 502, and 503 (for example only). The background management server may determine the object to be detected in response to receiving a detection request for circular references of program code; parse the object to be detected and construct an object reference relationship graph; perform node access and labeling processing on the nodes in the object reference relationship graph according to the program code where the object to be detected is executed, determine the strongly connected components according to the result of the labeling processing, and then obtain the circular reference and other processing corresponding to the detection request, and feed back the processing results (such as circular reference relationships, etc. - for example only) to the terminal device.
[0095] It should be noted that the circular reference detection method provided in the embodiment of the present invention is generally executed by the server 505 , and accordingly, the circular reference detection device is generally set in the server 505 .
[0096] It should be understood that Figure 5 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0097] Reference below Figure 6 , which shows a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. Figure 6 The terminal device or server shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0098] like Figure 6 As shown, the computer system 600 includes a central processing unit (CPU) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage part 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the system 600 are also stored. The CPU 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0099] The following components are connected to the I / O interface 605: an input section 606 including a keyboard, a mouse, etc.; an output section 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, a modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the I / O interface 605 as needed. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as needed, so that a computer program read therefrom is installed into the storage section 608 as needed.
[0100] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 609, and / or installed from the removable medium 611. When the computer program is executed by the central processing unit (CPU) 601, the above-mentioned functions defined in the system of the present invention are executed.
[0101] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination thereof.
[0102] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a sequence different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0103] The units involved in the embodiments of the present invention may be implemented by software or hardware. The units described may also be set in a processor, for example, it may be described as: a processor includes: a module for determining an object to be detected, a module for constructing a reference relationship graph, and a module for obtaining a circular reference.
[0104] The names of these modules do not, in some cases, constitute limitations on the modules themselves. For example, the module for determining the object to be detected can also be described as "a module for determining the object to be detected in response to receiving a detection request for a circular reference in a program code."
[0105] On the other hand, the present invention also provides a computer-readable medium, which may be included in the device described in the embodiment; or it may exist independently without being assembled into the device. The computer-readable medium carries one or more programs, and when the one or more programs are executed by a device, the device includes: determining an object to be detected in response to receiving a detection request for a circular reference of a program code; parsing the object to be detected and constructing an object reference relationship graph; performing node access and marking processing on nodes in the object reference relationship graph according to executing the program code where the object to be detected is located, determining a strongly connected component according to the result of the marking processing, and then obtaining a circular reference corresponding to the detection request.
[0106] According to the technical solution of the embodiment of the present invention, it has the following advantages or beneficial effects: by responding to the detection request of circular reference of program code, the object to be detected is determined; the object to be detected is parsed and an object reference relationship graph is constructed; according to the program code where the object to be detected is executed, the nodes in the object reference relationship graph are accessed and marked, and the strongly connected components are determined according to the results of the marking process, so as to obtain the technical solution of circular reference corresponding to the detection request, and the detection of circular reference of program code is converted into the detection of strongly connected components of the object reference relationship graph. During the program running process, the extremely large circular reference relationship is detected through the object reference relationship graph, which effectively improves the accuracy of the detection result.
[0107] The specific implementation methods described herein do not constitute limitations on the scope of protection of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention shall be included in the scope of protection of the present invention.
Claims
1. A method for detecting circular references, It is characterized in that include: In response to receiving a detection request for a program code circular reference, determining an object to be detected; Parsing the object to be detected and constructing an object reference relationship graph; According to the program code where the object to be detected is executed, the nodes in the object reference relationship graph are accessed and marked, and the strongly connected components are determined according to the result of the marking process, so as to obtain the circular reference corresponding to the detection request.
2. The method according to claim 1, It is characterized in that The objects to be detected include: Determine, according to the detection request, a method for acquiring the object to be detected; When the acquisition method is directional scanning, acquiring the object to be detected from a designated storage medium; When the acquisition mode is global scanning, a memory snapshot of the program when the detection request is received is acquired, and the memory snapshot is parsed to obtain the object to be detected.
3. The method according to claim 1, It is characterized in that After determining the object to be detected, the method further includes: The objects to be detected are filtered according to preset screening conditions, and objects that will not generate circular references in the objects to be detected are filtered out.
4. The method according to claim 1, It is characterized in that Parsing the object to be detected and constructing an object reference relationship graph includes: Determining the type of the object to be detected; According to the type, the object to be detected is parsed to obtain a strong reference object of the object to be detected; According to the object to be detected and the strongly referenced object of the object to be detected, a strong reference relationship between the object to be detected and the strongly referenced object of the object to be detected is determined to obtain an object reference relationship graph.
5. The method according to claim 4, It is characterized in that According to the type, parsing the object to be detected to obtain a strong reference object of the object to be detected includes: In the case where the type of the object to be detected is directly held, obtaining a strong reference object of the object to be detected according to a reference table of the object to be detected; In the case where the type of the object to be detected is a structure type, obtaining a strong reference object of the object to be detected through an object pointer of the object to be detected; In the case that the type of the object to be detected is a collection type, the strong reference object of each element in the object to be detected is traversed and obtained, and the strong references of each element constitute the strong reference object of the object to be detected.
6. The method according to claim 1, It is characterized in that According to the program code where the object to be detected is executed, the nodes in the object reference relationship graph are accessed and marked, including: Establish access records for storage access nodes; By executing the program code, nodes in the object reference relationship graph are accessed, the accessed nodes are recorded in the access records, and the accessed nodes are marked according to the access records.
7. The method according to claim 6, It is characterized in that By executing the program code, performing node access on the nodes in the object reference relationship graph, recording the accessed nodes in the access record, and marking the accessed nodes according to the access record, including: According to the program code and a preset recursive depth, verify the root node in the object reference relationship graph, use a preset backtracking value and a preset index value to mark the root node, and add the root node to the access record; By executing the program code, the adjacent nodes of the root node in the object reference relationship graph are traversed and accessed; according to the access record, it is determined whether to record the visited adjacent nodes in the access record, and the backtracking value and index value of the adjacent nodes are determined, and the adjacent nodes are marked using the backtracking value and index value of the adjacent nodes, so as to determine the strongly connected components according to the backtracking value and the index value.
8. The method according to claim 1, It is characterized in that When the number of the objects to be detected exceeds a preset concurrency threshold, the method further includes: Adding a plurality of the objects to be detected into a pre-established parallel queue; According to the preset number of sub-threads, a corresponding sub-thread is allocated to each of the objects to be detected in the parallel queue, and a detection task for each of the objects to be detected is generated; The detection tasks of the objects to be detected are executed in parallel by each sub-thread.
9. The method according to claim 8, It is characterized in that The execution of the detection task of the object to be detected includes updating an access record for recording access nodes; Before executing the detection tasks of the objects to be detected in parallel through the sub-threads, the method further includes: The access record is locked so that only one detection task of a sub-thread has the authority to update the access record within a time period.
10. A circular reference detection device, It is characterized in that include: A module for determining an object to be detected, configured to determine an object to be detected in response to receiving a detection request for a circular reference of a program code; A reference relationship graph construction module is used to parse the object to be detected and construct an object reference relationship graph; The circular reference acquisition module is used to perform node access and marking processing on the nodes in the object reference relationship graph according to the program code where the object to be detected is executed, determine the strongly connected components according to the result of the marking processing, and then obtain the circular reference corresponding to the detection request.
11. A mobile electronic device terminal, It is characterized in that include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 9.
12. A computer readable medium having a computer program stored thereon, It is characterized in that When the program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.