Method, device, storage medium and processor for monitoring website memory leaks
The method converts website source files to an abstract syntax tree to identify and monitor memory leaks in front-end projects, effectively addressing issues with global variables, timer functions, and DOM events without additional plugins, ensuring thorough and customizable statistical analysis.
Patent Information
- Application Number
- CN202111272872.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-29
- Publication Date
- 2024-12-20
- Estimated Expiration
- 2041-10-29
AI Technical Summary
Existing methods for detecting memory leaks are ineffective for front-end projects, particularly websites, as they are designed for back-end applications and do not adequately address issues with global variables, closures, timer functions, and DOM binding events.
A method for monitoring website memory leaks involves converting the source execution file to an abstract syntax tree, traversing it to identify target variables, functions, and events, and collecting statistics on global variables, timer functions, and listening events, without requiring additional plugins or code reviews.
This approach provides comprehensive and flexible monitoring of website memory leaks, ensuring no extra processing is needed, with customizable and detailed statistical information for accurate analysis.
Smart Images

Figure CN113986739B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device, storage medium, processor and computer program product for monitoring website memory leaks. Background Art
[0002] Programs require memory to run, and for continuously running programs, memory that is no longer used must be released in a timely manner. Otherwise, the memory usage will increase, leading to serious consequences such as slowing down the program or even system crashes. JavaScript, C#, and Java's operating environments all have garbage collection mechanisms, so developers do not need to manually track and release memory. However, global variables, closures, timer functions, DOM binding events, etc. in the program need to be processed accordingly. If not processed, it is easy to cause memory leaks.
[0003] In view of the problem of memory leakage, a method and device for memory leakage detection are provided in the prior art CN112463632A. The method includes: traversing the classes contained in the project, and recording the classes that may have the risk of memory leakage and the member variables in the class that have the risk to form a risk class diagram; traversing all the functions contained in the project, and constructing the linked list between the function calls according to the calling relationship between the functions to form a function linked list; based on the risk class diagram and the function linked list, detecting the memory leakage in the project. By using the solution of the prior art, it is possible to reduce the underreporting and false alarm in memory leakage detection, and improve the quality and efficiency of scanning and auditing memory leakage.
[0004] However, the technical solution provided by this prior art is aimed at the detection of memory leaks in back-end applications, but cannot handle the memory leak problem of front-end projects (websites). Summary of the invention
[0005] The purpose of the embodiments of the present application is to provide a method, device, storage medium, processor and computer program product for monitoring website memory leaks.
[0006] In order to achieve the above-mentioned purpose, the first aspect of the present application provides a method for monitoring website memory leaks, comprising:
[0007] Get the source execution file of the website to be monitored;
[0008] Converting the source execution file to determine an abstract syntax tree corresponding to the source execution file;
[0009] Traverse the abstract syntax tree according to the target file;
[0010] Get the target file's statistics information in the abstract syntax tree;
[0011] Collect and save the statistics of the target file.
[0012] In an embodiment of the present application, the target file includes at least one of a target variable type, a target function, and a target event; the information to be counted includes the name and location information of the target file, and the location information includes the row number and column number of the target file in the abstract syntax tree.
[0013] In the embodiment of the present application, the target variable type includes a global variable, and obtaining the information to be counted of the target file in the abstract syntax tree includes:
[0014] Obtaining a first type of expression file corresponding to a global variable in an abstract syntax tree;
[0015] Statistics are collected on the expression files of the first type to determine the variable names of the global variables and the position information of the global variables in the abstract syntax tree.
[0016] In the embodiment of the present application, the global variable includes at least one of a global variable located within a function body and a global variable located outside a function body.
[0017] In an embodiment of the present application, statistics are performed on the expression file of the first type to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree, including:
[0018] Get the attribute value of the name attribute in the edge attributes contained in the first type of expression file;
[0019] The attribute value of the name attribute is determined as the variable name of the global variable;
[0020] Obtain a first start attribute from expression attributes contained in an expression file of the first type;
[0021] The attribute value of the row attribute in the first start attribute is determined as the row number of the global variable in the abstract syntax tree, and the attribute value of the column attribute in the first start attribute is determined as the column number of the global variable in the abstract syntax tree.
[0022] In an embodiment of the present application, counting and saving the information to be counted of the target file includes:
[0023] Concatenate the variable names of all the global variables counted and the position information corresponding to each global variable into a first character string;
[0024] Send the first string to the preset address for storage.
[0025] In an embodiment of the present application, the target function includes a timer function, and obtaining the information to be counted of the target file in the abstract syntax tree includes:
[0026] Obtaining a second type of expression file corresponding to the timer function in the abstract syntax tree;
[0027] Statistics are performed on the expression files of the second type to determine the function name of the timer function and the position information of the timer function in the abstract syntax tree.
[0028] In an embodiment of the present application, the timer function includes a single timer function, the single timer function includes a first single timer function and a second single timer function, and statistics are performed on the expression file of the second type to determine the function name of the timer function and the position information of the timer function in the abstract syntax tree, including:
[0029] Obtaining a first attribute value of a calling attribute contained in an expression file of the second type;
[0030] In the case where the first attribute value is an identifier type value, determining that the function corresponding to the identifier type value is a first single timer function;
[0031] In the case where the first attribute value is a member expression type value, determining that the function corresponding to the member expression type value is a second single timer function;
[0032] The second start attribute in the call attributes of the first single timer function and the second single timer function is obtained respectively, and the number of rows and columns of the first single timer function and the second single timer function in the abstract syntax tree are determined respectively according to the values of the row attribute and the column attribute in the second start attribute.
[0033] In the embodiment of the present application, the timer function also includes a cyclic timer function, which counts and saves the information to be counted of the target file, including:
[0034] Respectively concatenate the function names of the single timer function and the cyclic timer function and the position information corresponding to each timer function into corresponding second and third character strings;
[0035] The second character string and the third character string are sent to a preset address for storage.
[0036] In the embodiment of the present application, obtaining the information to be counted of the target file in the abstract syntax tree further includes:
[0037] When it is determined that the abstract syntax tree contains a timer function, detecting whether the timer function has a corresponding cancel function;
[0038] When it is detected that a corresponding cancel function exists for the timer function, the function name corresponding to the cancel function and the row number and column number where the cancel function is located are obtained.
[0039] In the embodiment of the present application, the target event includes a monitoring event, and obtaining the information to be counted of the target file in the abstract syntax tree includes:
[0040] Obtaining a third type of expression file corresponding to the listening event in the abstract syntax tree;
[0041] Statistics are collected on the expression files of the third type to determine the event names of the monitoring events and the position information of the monitoring events in the abstract syntax tree.
[0042] In an embodiment of the present application, the listening event includes a first type of listening event and a second type of listening event; statistics are performed on the expression file of the third type to determine the event name of the listening event and the position information of the listening event in the abstract syntax tree, including:
[0043] Get the third start attribute in the call attribute contained in the expression file of the third type;
[0044] The third start attributes corresponding to the first type listening event and the second type listening event are obtained respectively, and the number of rows and the number of columns of the first type listening event and the second type listening event in the abstract syntax tree are determined respectively according to the values of the row attribute and the column attribute in the third start attribute.
[0045] In an embodiment of the present application, counting and saving the information to be counted of the target file includes:
[0046] Respectively concatenate the event names of the first type of listening event and the second type of listening event and the position information corresponding to each listening event into a corresponding fourth character string and a fifth character string;
[0047] The fourth and fifth character strings are sent to a preset address for storage.
[0048] In the embodiment of the present application, obtaining the information to be counted of the target file in the abstract syntax tree further includes:
[0049] When it is determined that the abstract syntax tree contains a listening event, detecting whether there is a corresponding unbinding function for the listening event;
[0050] When it is detected that a corresponding unbinding function exists for a listening event, the function name corresponding to the unbinding function, as well as the row and column number of the unbinding function are obtained.
[0051] In an embodiment of the present application, converting the source execution file to determine the abstract syntax tree corresponding to the source execution file includes:
[0052] The source execution file is converted into the corresponding abstract syntax tree through the preset database.
[0053] A second aspect of the present application provides a processor configured to execute the above-mentioned monitoring method for website memory leaks.
[0054] A third aspect of the present application provides a monitoring device for website memory leaks, comprising the above-mentioned processor.
[0055] A fourth aspect of the present application provides a machine-readable storage medium having instructions stored thereon, which, when executed by a processor, configures the processor to execute the above-mentioned method for monitoring website memory leaks.
[0056] A fifth aspect of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the above-mentioned method for monitoring website memory leaks.
[0057] The above-mentioned monitoring method for website memory leaks obtains the source execution file of the website to be monitored and converts the source execution file to obtain the abstract syntax tree corresponding to the source execution file. After traversing the abstract syntax tree according to the target file, the target file's information to be counted in the abstract syntax tree can be obtained. Then, the target file's information to be counted can be counted and saved. In this way, no extra plug-ins are required for processing, and no code review or retrieval is required. At the same time, comprehensive monitoring of the memory leakage code on the website side is guaranteed. At the same time, the statistical information can also be customized, and the information statistics are more flexible, rich and adapted to actual needs.
[0058] Other features and advantages of the embodiments of the present application will be described in detail in the subsequent specific implementation section. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] The accompanying drawings are used to provide a further understanding of the embodiments of the present application and constitute a part of the specification. Together with the following specific implementations, they are used to explain the embodiments of the present application, but do not constitute a limitation on the embodiments of the present application. In the accompanying drawings:
[0060] Figure 1 A schematic diagram of an application environment of a method for monitoring website memory leakage according to an embodiment of the present application is schematically shown;
[0061] Figure 2 A schematic diagram schematically shows a memory leak code monitoring process according to an embodiment of the present application;
[0062] Figure 3 The internal structure diagram of a computer device according to an embodiment of the present application is schematically shown. DETAILED DESCRIPTION
[0063] In order to make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. It should be understood that the specific implementation methods described herein are only used to illustrate and explain the embodiments of the present application, and are not used to limit the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0064] It should be noted that if the embodiments of the present application involve directional indications (such as up, down, left, right, front, back...), the directional indications are only used to explain the relative position relationship, movement status, etc. between the components under a certain specific posture (as shown in the accompanying drawings). If the specific posture changes, the directional indication will also change accordingly.
[0065] In addition, if there are descriptions involving "first", "second", etc. in the embodiments of the present application, the descriptions of "first", "second", etc. are only used for descriptive purposes and cannot be understood as indicating or suggesting their relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In addition, the technical solutions between the various embodiments can be combined with each other, but they must be based on the ability of ordinary technicians in the field to implement them. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such combination of technical solutions does not exist and is not within the scope of protection required by this application.
[0066] Figure 1 The flowchart of the method for monitoring website memory leakage according to the embodiment of the present application is schematically shown. Figure 1 As shown, in one embodiment of the present application, a method for monitoring website memory leakage is provided. This embodiment mainly uses the method applied to a terminal as an example, and includes the following steps:
[0067] Step 102, obtaining the source execution file of the website to be monitored.
[0068] Step 104: convert the source execution file to determine an abstract syntax tree corresponding to the source execution file.
[0069] Step 106, traverse the abstract syntax tree according to the target file.
[0070] Step 108: Obtain the information to be counted of the target file in the abstract syntax tree.
[0071] Step 110, collect and save the information to be counted of the target file.
[0072] The source execution file in this embodiment may refer to a JavaScript file, so this method can monitor memory leak code for JavaScript files on website pages. After the terminal obtains the source execution file to be monitored, it can convert the source execution file to obtain an abstract syntax tree corresponding to the source execution file. The abstract syntax tree, referred to as AST (Abstract Syntax Tree), is an abstract representation of the grammatical structure of the source code. It represents the grammatical structure of the programming language in a tree-like form, and each node on the tree represents a structure in the source code. In one embodiment, the source execution file is converted to determine the abstract syntax tree corresponding to the source execution file, including: converting the source execution file to a corresponding abstract syntax tree through a preset database. When converting the source execution file, babel technology can be used to convert it. Babel is a JavaScript compiler that can perform syntax parsing on the source execution file and generate a corresponding AST syntax tree. Specifically, the source execution file, i.e., the JavaScript file, can be converted into AST using a specific database of babel / parser.
[0073] After determining the abstract syntax tree corresponding to the source execution file, the abstract syntax tree can be traversed according to the target file. Among them, the target file is a file determined in advance according to the memory leak dimension that needs to be counted. The target file may include at least one of the target variable type, the target function, and the target event. The technician can also adjust the dimensions that need to be counted according to actual needs. After traversing the abstract syntax tree, the information to be counted of the target file in the abstract syntax tree can be obtained, the information to be counted of the target file can be counted and saved, and the code that may have memory leaks in the source execution file can be monitored. The technician can then monitor and process the statistical information. The information to be counted may include the name and location information of the target file, and the location information includes the number of rows and columns where the target file is located in the abstract syntax tree. The information to be counted may also include other more detailed information, which can be adjusted according to the needs of the technician.
[0074] In one embodiment, the target variable type includes a global variable, and obtaining the information to be counted of the target file in the abstract syntax tree includes: obtaining an expression file of a first type corresponding to the global variable in the abstract syntax tree; and performing statistics on the expression file of the first type to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree.
[0075] Variable types include global variables and local variables. This solution targets global variables. When a variable is declared using var, let, or const, the scope of the variable is limited to the context of its declaration location, while non-declared variables (i.e. variables not declared using var, let, and const) are always global, that is, global variables. In the operating environment of JavaScript, C#, and Java, there is a garbage collection mechanism, and developers do not need to manually track and release memory. However, the code for global variables, closures, timer functions, and DOM binding events in the source execution file needs to be processed. For example, modify the global variable to a variable declared with let, const, or var; for the timer function, you can write a timer cleanup function; for the DOM event, you can add unbinding processing, etc. In addition, since global variables cannot be collected by the garbage collector, if you must use global variables to store data, you need to ensure that they are assigned to null or reallocated after use. When a global variable is not assigned to null after use, it will cause memory leaks, so it is necessary to count the usage of global variables. In order to find the global variables in the source execution file, the first type of expression file corresponding to the global variable in the abstract syntax tree can be obtained first, and then the first type of expression file can be counted to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree. In one embodiment, the global variable includes at least one of the global variable located in the function body and the global variable located outside the function body. Among them, the first type of expression file can be of ExpressionStatement type. Because the global variable is an expression, all global variables will be in ExpressionStatement. Therefore, the ExpressionStatement type in the abstract syntax tree can be counted to obtain the variable name of each global variable and the position information of each global variable in the abstract syntax tree. The position information includes the row and column where each global variable is located in the abstract syntax tree.
[0076] In one embodiment, statistics are performed on the first type of expression file to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree, including: obtaining the attribute value of the name attribute in the edge attributes contained in the first type of expression file; determining the attribute value of the name attribute as the variable name of the global variable; obtaining the first start attribute in the expression attributes contained in the first type of expression file; determining the attribute value of the row attribute in the first start attribute as the number of rows of the global variable in the abstract syntax tree, and determining the attribute value of the column attribute in the first start attribute as the number of columns of the global variable in the abstract syntax tree.
[0077] In the abstract syntax tree, the attribute value of the name attribute in the edge attribute contained in the first type of expression file can be obtained, that is, the name attribute of the left attribute of the expression attribute in the ExpressionStatement can be obtained, and the attribute value of the name attribute can be determined as the variable name of the global variable. Furthermore, the first start attribute in the expression attribute contained in the first type of expression file can also be obtained, and the first start attribute can refer to the start attribute of the loc attribute of the expression attribute in the ExpressionStatement. The attribute value of the row attribute in the first start attribute can be determined as the number of rows of the global variable in the abstract syntax tree, and the attribute value of the column attribute in the first start attribute can be determined as the number of columns of the global variable in the abstract syntax tree.
[0078] Furthermore, in one embodiment, counting and saving the information to be counted of the target file includes: concatenating the variable names of all counted global variables and the position information corresponding to each global variable into a first string; and sending the first string to a preset address for storage.
[0079] After determining the variable name of each global variable included in the abstract syntax tree and the location information of each global variable, that is, the row and column where each global variable is located in the abstract syntax tree, the variable names of all global variables and the location information corresponding to each global variable can be spliced into a first string, and then the first string can be sent to a preset address for storage. For example, the first string can be sent to an execution folder, and subsequent technicians can analyze and process the statistical data.
[0080] In one embodiment, the target function includes a timer function, and obtaining the information to be counted of the target file in the abstract syntax tree includes: obtaining a second type of expression file corresponding to the timer function in the abstract syntax tree; and performing statistics on the second type of expression file to determine the function name of the timer function and the location information of the timer function in the abstract syntax tree.
[0081] If the timer function only has the code to execute the operation but no code to cancel the operation, it will cause memory leakage. Therefore, in order to find the timer function in the source execution file, the second type of expression file corresponding to the timer function in the abstract syntax tree can be obtained for the abstract syntax tree corresponding to the source execution file. Among them, the second type of expression file can refer to CallExpression. Similarly, the second type of expression file can be counted to determine the function name of each timer function contained in the abstract tree and the position information of each timer function in the abstract syntax tree. In one embodiment, the timer function includes a single timer function and a cyclic timer function. The single timer function refers to the setTimeout timer function, which executes a function or executes a piece of code after the timer expires. The cyclic timer function refers to the setInterval timer function, which repeatedly calls a function or executes a code segment, with a fixed time delay between each call.
[0082] Further, in one embodiment, the single timer function includes a first single timer function and a second single timer function. Performing statistics on the second type of expression file to determine the function name of the timer function and the position information of the timer function in the abstract syntax tree includes: obtaining the first attribute value of the call attribute contained in the second type of expression file; when the first attribute value is an identifier type value, determining that the function corresponding to the identifier type value is the first single timer function; when the first attribute value is a member expression type value, determining that the function corresponding to the member expression type value is the second single timer function; respectively obtaining the second start attribute in the call attributes of the first single timer function and the second single timer function, and determining the number of rows and columns of the first single timer function and the second single timer function in the abstract syntax tree according to the values of the row attribute and the column attribute in the second start attribute.
[0083] The single timer function, i.e., the setTimeout timer function, includes two types, namely, the first single timer function and the second single timer function. The first single timer function may be a setTimeout directly defined and called, and the second single timer function may be a setTimeout called using a window object. When setTimeout is directly defined and called using a window object, the internal data of CallExpression (the second type of expression file) is different, so it is necessary to handle the two types of setTimeout directly defined and called using a window object separately. When using these two single timer functions, the value of the call attribute under CallExpression is different. Specifically, the first attribute value of the callee attribute under CallExpression is different. When setTimeout is directly defined and called, the first attribute value of the callee attribute is an identifier type value, such as Identifier. When using window to call, the first attribute value of the callee attribute is a member expression type value, such as MemberExpression. Therefore, in the case where the first attribute value is an identifier type value, it can be determined that the function corresponding to the identifier type value is the first single timer function (the call function directly defined by setTimeout). In the case where the first attribute value is a member expression type value, it can be determined that the function corresponding to the member expression type value is the second single timer function (the function called using window). Then, the second start attribute in the calling attributes of the first single timer function and the second single timer function can be obtained respectively, and the values of the row attribute and the column attribute in the second start attribute in the calling attributes of the first single timer function can be determined as the number of rows and columns of the first single timer function in the abstract syntax tree, and the values of the row attribute and the column attribute in the second start attribute in the calling attributes of the second single timer function can be determined as the number of rows and columns of the second single timer function in the abstract syntax tree.
[0084] In one embodiment, counting and saving the information to be counted of the target file includes: respectively concatenating the function names of the single timer function and the loop timer function and the location information corresponding to each timer function into corresponding second and third character strings; and sending the second and third character strings to a preset address for saving.
[0085] For two types of timer functions, namely, one-shot timer functions and loop timer functions, the function name of each one-shot timer function contained in the abstract syntax tree and the position information corresponding to each one-shot timer function can be concatenated into a corresponding second string, and the function name of each loop timer function contained in the abstract syntax tree and the position information corresponding to each loop timer function can be concatenated into a corresponding third string, and the second string and the third string can be sent to a preset address for saving.
[0086] Furthermore, in one embodiment, obtaining the to-be-counted information of the target file in the abstract syntax tree also includes: in a case where it is determined that the abstract syntax tree contains a timer function, detecting whether there is a corresponding cancel function for the timer function; in a case where it is detected that there is a corresponding cancel function for the timer function, obtaining the function name corresponding to the cancel function, and the row and column number of the cancel function.
[0087] For a timer function, if there is only a timer function that executes the timing, but there is no corresponding cancellation function for canceling the timer, then the execution process of the timer will continue to run, resulting in a memory leak. Therefore, it is possible to count the timer functions that do not have a corresponding cancellation function. Specifically, after the function name and location information of each timer function have been obtained, it is possible to continue to detect whether each timer function has a corresponding cancellation function. If there is a corresponding cancellation function for a timer function, the function name corresponding to each cancellation function, as well as the row and column number of each cancellation function, can be obtained to record the execution code and cancellation code of each timer function in detail, so that subsequent technical personnel can conduct a more intuitive and accurate analysis.
[0088] In one embodiment, the target event includes a listening event. Obtaining the information to be counted of the target file in the abstract syntax tree includes: obtaining a third type of expression file corresponding to the listening event in the abstract syntax tree; and performing statistics on the third type of expression file to determine the event name of the listening event and the position information of the listening event in the abstract syntax tree.
[0089] In this embodiment, the listening event may refer to a DOM event, specifically, the first type of listening event and the second type of listening event used in JavaScript. Among them, the first type of listening event may refer to addEventListener, which registers the specified listener to EventTarget (target event). When the object triggers the specified event, the specified callback function will be executed. The event target may be an element Element, Document (file) and Window (window) on a document or any other object that supports events (such as XMLHttpRequest, web page request). The second type of listening event may refer to attachEvent, and its working principle is to add the function or object that implements EventListener (event listening) to the event listener list of the specified event type on the EventTarget (target event) that calls it. For the listening event, when the element is destroyed, the event listened to on the element needs to be unbound, otherwise it will cause memory leakage. Therefore, when monitoring the memory leakage of the source execution file, it is also necessary to count the information of the listening event contained in the source execution file. For the listening event in the source execution file, it can be first detected whether there is a third type of expression file corresponding to the listening event in the abstract syntax tree. The third type of expression file can be CallExpression. If so, the third type of expression file in the abstract syntax tree can be obtained, and then the third type of expression file can be counted to determine the event name of each listening event contained in the abstract syntax tree, as well as the location information of each listening event in the abstract syntax tree.
[0090] In one embodiment, statistics are performed on the third type of expression file to determine the event name of the listening event and the location information of the listening event in the abstract syntax tree, including: obtaining the third start attribute in the call attribute contained in the third type of expression file; obtaining the third start attributes corresponding to the first type of listening event and the second type of listening event respectively, and determining the number of rows and columns of the first type of listening event and the second type of listening event in the abstract syntax tree according to the values of the row attribute and the column attribute in the third start attribute.
[0091] Specifically, the first type of listening event and the second type of listening event in the source execution file are parsed into the CallExpression type, that is, the third type of expression file, after the source execution file is converted into an abstract syntax tree. The information of the first type of listening event and the second type of listening event can be obtained from CallExpression, that is, the event name and location information of the first type of listening event and the second type of listening event. Specifically, the location information of the listening event is under the third start attribute in the call attribute contained in CallExpression. For example, the location information of the listening event can be under the loc attribute start attribute of the property attribute of the callee attribute in CallExpression. Therefore, the third start attribute corresponding to the first type of listening event and the second type of listening event can be obtained respectively, and the values of the row attribute and the column attribute in the third start attribute are determined as the number of rows and columns of the first type of listening event and the second type of listening event in the abstract syntax tree respectively. Furthermore, the event name of each first-type listening event and the location information corresponding to each first-type listening event can be concatenated into a corresponding fourth character string, and the event name of each second-type listening event and the location information corresponding to each second-type listening event can be concatenated into a corresponding fifth character string. Then, the fourth character string and the fifth character string can also be sent to a preset address for saving, such as sending them to a designated folder for saving.
[0092] In one embodiment, obtaining the information to be counted of the target file in the abstract syntax tree also includes: when it is determined that the abstract syntax tree contains a listening event, detecting whether there is a corresponding unbinding function for the listening event; when it is detected that there is a corresponding unbinding function for the listening event, obtaining the function name corresponding to the unbinding function, and the row number and column number of the unbinding function.
[0093] If there is no corresponding unbinding function for the listening event, that is, there is no corresponding unbinding operation, memory leakage will occur. Therefore, for the listening events in the source execution file, the unbinding functions corresponding to each listening event can be further counted. Specifically, after the event name and location information of each listening event have been obtained, it is possible to continue to detect whether there is an unbinding function corresponding to each listening event. If there is a corresponding unbinding function for a listening event, the function name corresponding to each unbinding function, as well as the number of rows and columns where each unbinding function is located, can be obtained to record the listening code and unbinding code of each listening event in detail, so that subsequent technical personnel can conduct more intuitive and accurate analysis.
[0094] The above-mentioned monitoring method for website memory leaks obtains the source execution file of the website to be monitored and converts the source execution file to obtain the abstract syntax tree corresponding to the source execution file. After traversing the abstract syntax tree according to the target file, the target file's information to be counted in the abstract syntax tree can be obtained. Then, the target file's information to be counted can be counted and saved. In this way, no extra plug-ins are required for processing, and no code review or retrieval is required. At the same time, comprehensive monitoring of the memory leakage code on the website side is guaranteed. At the same time, the statistical information can also be customized, and the information statistics are more flexible, rich and adapted to actual needs.
[0095] In one embodiment, Figure 2 As shown, a schematic diagram of a memory leak code monitoring process is provided. First, the plug-in parameters can be obtained, and the plug-in parameters refer to the acquisition of the target file, and the target file is a file determined in advance according to the memory leak dimension that needs to be counted. The target file may include at least one of the target variable type, the target function and the target event. The technician can also adjust the dimension that needs to be counted according to actual needs. Therefore, after obtaining the plug-in parameters, the memory leak statistical dimension configured in the parameter can be further obtained, that is, it is determined which types of code in the source execution file need to be counted. In this embodiment, the target file includes the target variable type, the target function and the target event as an example. The source execution file can be converted first to obtain the abstract syntax tree corresponding to the source execution file. Then the abstract syntax tree is traversed, and the information to be counted of the obtained target file in the abstract syntax tree is counted and saved. Specifically, if the memory leak statistical dimension includes global variables, the information to be counted of the global variables can be counted, that is, the variable name of each global variable and the position information of each global variable in the abstract syntax tree are counted. Similarly, if the memory leak statistical dimensions include timer functions and DOM events (listening events), the function name of each timer function and the location information of each timer function in the abstract syntax tree, as well as the event name of each listening event and the location information of each listening event in the abstract syntax tree can also be counted separately.
[0096] Figure 1 and Figure 2 FIG. 1 is a flow chart of a method for monitoring a website memory leak in one embodiment. It should be understood that although Figure 1 and Figure 2 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 1 and Figure 2At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.
[0097] An embodiment of the present application provides a storage medium on which a program is stored. When the program is executed by a processor, the above-mentioned monitoring method for website memory leaks is implemented.
[0098] An embodiment of the present application provides a processor, which is used to run a program, wherein the program executes the above-mentioned monitoring method for website memory leakage when running.
[0099] In one embodiment, a monitoring device for website memory leakage is provided, including the above-mentioned processor and memory, and the processor can execute a program stored in the memory to implement the above-mentioned monitoring method for website memory leakage.
[0100] The processor includes a kernel, which calls the corresponding program unit from the memory. One or more kernels can be set, and the monitoring method for website memory leaks can be implemented by adjusting kernel parameters.
[0101] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0102] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 3As shown. The computer device includes a processor A01, a network interface A02, a display screen A04, an input device A05 and a memory (not shown in the figure) connected through a system bus. Among them, the processor A01 of the computer device is used to provide computing and control capabilities. The memory of the computer device includes an internal memory A03 and a non-volatile storage medium A06. The non-volatile storage medium A06 stores an operating system B01 and a computer program B02. The internal memory A03 provides an environment for the operation of the operating system B01 and the computer program B02 in the non-volatile storage medium A06. The network interface A02 of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor A01, a monitoring method for website memory leakage is implemented. The display screen A04 of the computer device can be a liquid crystal display or an electronic ink display, and the input device A05 of the computer device can be a touch layer covered on the display screen, or a key, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.
[0103] Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0104] An embodiment of the present application provides a device, which includes a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, the following steps are implemented: obtaining a source execution file of a website to be monitored; converting the source execution file to determine an abstract syntax tree corresponding to the source execution file; traversing the abstract syntax tree according to a target file; obtaining information to be counted of the target file in the abstract syntax tree; and counting and saving the information to be counted of the target file.
[0105] In one embodiment, the target file includes at least one of a target variable type, a target function, and a target event; the information to be counted includes the name and location information of the target file, and the location information includes the row number and column number of the target file in the abstract syntax tree.
[0106] In one embodiment, the target variable type includes a global variable, and obtaining the information to be counted of the target file in the abstract syntax tree includes: obtaining an expression file of a first type corresponding to the global variable in the abstract syntax tree; and performing statistics on the expression file of the first type to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree.
[0107] In one embodiment, the global variable includes at least one of a global variable located within a function body and a global variable located outside a function body.
[0108] In one embodiment, statistics are performed on the first type of expression file to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree, including: obtaining the attribute value of the name attribute in the edge attributes contained in the first type of expression file; determining the attribute value of the name attribute as the variable name of the global variable; obtaining the first start attribute in the expression attributes contained in the first type of expression file; determining the attribute value of the row attribute in the first start attribute as the number of rows of the global variable in the abstract syntax tree, and determining the attribute value of the column attribute in the first start attribute as the number of columns of the global variable in the abstract syntax tree.
[0109] In one embodiment, counting and saving the information to be counted of the target file includes: concatenating the variable names of all counted global variables and the position information corresponding to each global variable into a first character string; and sending the first character string to a preset address for storage.
[0110] In one embodiment, the target function includes a timer function, and obtaining the information to be counted of the target file in the abstract syntax tree includes: obtaining a second type of expression file corresponding to the timer function in the abstract syntax tree; and performing statistics on the second type of expression file to determine the function name of the timer function and the location information of the timer function in the abstract syntax tree.
[0111] In one embodiment, a timer function includes a single timer function, and the single timer function includes a first single timer function and a second single timer function. Statistics are performed on the expression file of the second type to determine the function name of the timer function and the position information of the timer function in the abstract syntax tree, including: obtaining the first attribute value of the call attribute contained in the expression file of the second type; when the first attribute value is an identifier type value, determining that the function corresponding to the identifier type value is the first single timer function; when the first attribute value is a member expression type value, determining that the function corresponding to the member expression type value is the second single timer function; respectively obtaining the second start attribute in the call attributes of the first single timer function and the second single timer function, and determining the number of rows and columns of the first single timer function and the second single timer function in the abstract syntax tree according to the values of the row attribute and the column attribute in the second start attribute.
[0112] In one embodiment, the timer function also includes a cyclic timer function, and statistics and saving of the to-be-stated information of the target file include: respectively concatenating the function names of the single timer function and the cyclic timer function and the location information corresponding to each timer function into corresponding second and third character strings; and sending the second and third character strings to a preset address for saving.
[0113] In one embodiment, obtaining the information to be counted of the target file in the abstract syntax tree also includes: when it is determined that the abstract syntax tree contains a timer function, detecting whether the timer function has a corresponding cancel function; when it is detected that the timer function has a corresponding cancel function, obtaining the function name corresponding to the cancel function, and the row and column number where the cancel function is located.
[0114] In one embodiment, the target event includes a listening event, and obtaining the information to be counted of the target file in the abstract syntax tree includes: obtaining an expression file of a third type corresponding to the listening event in the abstract syntax tree; and performing statistics on the expression file of the third type to determine the event name of the listening event and the location information of the listening event in the abstract syntax tree.
[0115] In one embodiment, a listening event includes a first type of listening event and a second type of listening event; statistics are performed on a third type of expression file to determine the event name of the listening event and the location information of the listening event in an abstract syntax tree, including: obtaining a third start attribute in a call attribute contained in the third type of expression file; obtaining the third start attributes corresponding to the first type of listening event and the second type of listening event respectively, and determining the number of rows and columns of the first type of listening event and the second type of listening event in the abstract syntax tree according to the values of the row attribute and the column attribute in the third start attribute.
[0116] In one embodiment, counting and saving the information to be counted of the target file includes: respectively concatenating the event names of the first type of listening events and the second type of listening events and the location information corresponding to each listening event into corresponding fourth and fifth character strings; and sending the fourth and fifth character strings to a preset address for saving.
[0117] In one embodiment, obtaining the information to be counted of the target file in the abstract syntax tree also includes: when it is determined that the abstract syntax tree contains a listening event, detecting whether there is a corresponding unbinding function for the listening event; when it is detected that there is a corresponding unbinding function for the listening event, obtaining the function name corresponding to the unbinding function, and the row number and column number of the unbinding function.
[0118] In one embodiment, converting the source execution file to determine the abstract syntax tree corresponding to the source execution file includes: converting the source execution file into the corresponding abstract syntax tree through a preset database.
[0119] The present application also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program that initializes steps of a monitoring method for website memory leaks.
[0120] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0121] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0122] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0123] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0124] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0125] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0126] Computer readable media include permanent and non-permanent, removable and non-removable media, and can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0127] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0128] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.
Claims
1. A method for monitoring website memory leakage, characterized in that: The monitoring method comprises: Get the source execution file of the website to be monitored; Converting the source execution file to determine an abstract syntax tree corresponding to the source execution file; Traversing the abstract syntax tree according to a target file, the target file including at least one of a target variable type, a target function, and a target event, wherein the target variable type includes a global variable, the global variable includes at least one of a global variable located in a function body and a global variable located outside a function body, and the target function includes a timer function; Obtaining information to be counted of the target file in the abstract syntax tree, the information to be counted including the name and location information of the target file, the location information including the number of rows and columns where the target file is located in the abstract syntax tree; Collecting and saving the information to be counted of the target file; The step of obtaining the information to be counted of the target file in the abstract syntax tree includes: Obtaining an expression file of a first type corresponding to the global variable in the abstract syntax tree; Performing statistics on the expression files of the first type to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree; The step of performing statistics on the expression file of the first type to determine the variable name of the global variable and the position information of the global variable in the abstract syntax tree includes: Obtaining the attribute value of the name attribute in the edge attributes contained in the expression file of the first type; Determine the attribute value of the name attribute as the variable name of the global variable; Obtaining a first start attribute from expression attributes contained in the expression file of the first type; Determine the attribute value of the row attribute in the first start attribute as the number of rows of the global variable in the abstract syntax tree, and determine the attribute value of the column attribute in the first start attribute as the number of columns of the global variable in the abstract syntax tree; The step of obtaining the information to be counted of the target file in the abstract syntax tree includes: Obtaining an expression file of a second type corresponding to the timer function in the abstract syntax tree; Statistics are performed on the expression files of the second type to determine the function name of the timer function and the position information of the timer function in the abstract syntax tree.
2. The monitoring method according to claim 1, characterized in that: The counting and saving of the information to be counted of the target file includes: Concatenate the variable names of all the global variables counted and the position information corresponding to each global variable into a first character string; The first character string is sent to a preset address for storage.
3. The monitoring method according to claim 1, characterized in that: The timer function includes a single timer function, the single timer function includes a first single timer function and a second single timer function, and the performing statistics on the expression file of the second type to determine the function name of the timer function and the position information of the timer function in the abstract syntax tree includes: Obtaining a first attribute value of a calling attribute contained in the expression file of the second type; In a case where the first attribute value is an identifier type value, determining that the function corresponding to the identifier type value is the first single timer function; In the case where the first attribute value is a member expression type value, determining that the function corresponding to the member expression type value is the second single timer function; The second start attribute in the calling attributes of the first single timer function and the second single timer function is obtained respectively, and the number of rows and columns of the first single timer function and the second single timer function in the abstract syntax tree are determined respectively according to the values of the row attribute and the column attribute in the second start attribute.
4. The monitoring method according to claim 3, characterized in that: The timer function also includes a cyclic timer function, and the counting and saving of the to-be-counted information of the target file includes: Respectively concatenate the function names of the single timer function and the cyclic timer function and the position information corresponding to each timer function into a corresponding second character string and a third character string; The second character string and the third character string are sent to a preset address for storage.
5. The monitoring method according to any one of claims 1, 3 and 4, characterized in that: The obtaining of the to-be-counted information of the target file in the abstract syntax tree further includes: In the case where it is determined that the timer function is included in the abstract syntax tree, detecting whether the timer function has a corresponding cancel function; When it is detected that the timer function has a corresponding cancel function, a function name corresponding to the cancel function, and the row number and column number where the cancel function is located are obtained.
6. The monitoring method according to claim 1, characterized in that: The target event includes a monitoring event, and obtaining the information to be counted of the target file in the abstract syntax tree includes: Obtaining an expression file of a third type corresponding to the listening event in the abstract syntax tree; Statistics are performed on the expression files of the third type to determine the event name of the monitoring event and the position information of the monitoring event in the abstract syntax tree.
7. The monitoring method according to claim 6, characterized in that: The monitoring events include a first type of monitoring events and a second type of monitoring events; and the performing of statistics on the expression files of the third type to determine the event names of the monitoring events and the position information of the monitoring events in the abstract syntax tree includes: Obtaining a third start attribute in the call attributes contained in the expression file of the third type; The third start attributes corresponding to the first type listening event and the second type listening event are obtained respectively, and the number of rows and columns of the first type listening event and the second type listening event in the abstract syntax tree are determined respectively according to the values of the row attribute and the column attribute in the third start attribute.
8. The monitoring method according to claim 7, characterized in that: The counting and saving of the information to be counted of the target file includes: Respectively concatenate the event names of the first type of listening event and the second type of listening event and the location information corresponding to each listening event into a corresponding fourth character string and a fifth character string; The fourth character string and the fifth character string are sent to a preset address for storage.
9. The monitoring method according to any one of claims 6 to 8, characterized in that: The obtaining of the to-be-counted information of the target file in the abstract syntax tree further includes: In the case where it is determined that the abstract syntax tree contains the listening event, detecting whether the listening event has a corresponding unbinding function; When it is detected that the listening event has a corresponding unbinding function, a function name corresponding to the unbinding function, and the row number and column number of the unbinding function are obtained.
10. The monitoring method according to claim 1, characterized in that: The converting the source execution file to determine the abstract syntax tree corresponding to the source execution file includes: The source execution file is converted into a corresponding abstract syntax tree through a preset database.
11. A processor, characterized in that: The method is configured to execute the method for monitoring website memory leakage according to any one of claims 1 to 10.
12. A monitoring device for website memory leakage, characterized in that: Comprising a processor according to claim 11.
13. A machine-readable storage medium having instructions stored thereon, characterized in that: When the instruction is executed by a processor, the processor is configured to execute the method for monitoring website memory leakage according to any one of claims 1 to 10.
14. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the computer program implements the method for monitoring website memory leakage according to any one of claims 1 to 10.
