Memory optimization method and device

By obtaining the abstract syntax tree and generating a memory optimization strategy, it automatically optimizes possible memory leak nodes in JavaScript, solves the memory leak problem, and improves system performance.

CN118779088APending Publication Date: 2024-10-15BEIJING JINGDONG ZHENSHI INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310368714.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-04-07
Publication Date
2024-10-15

AI Technical Summary

Technical Problem

Existing technology is difficult to effectively solve memory leak problems in JavaScript caused by unexpected global variables, forgotten timers, unremoved event listeners, etc., resulting in system performance degradation or process crash.

Method used

By obtaining the abstract syntax tree in memory, determining node attributes based on node expressions, generating memory optimization strategies, automatically optimizing memory and releasing useless memory when corresponding operations are triggered.

Benefits of technology

It improves memory stability and system performance, ensures effective utilization of memory space, and avoids performance problems caused by memory leaks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118779088A_ABST
    Figure CN118779088A_ABST
Patent Text Reader

Abstract

The invention discloses a memory optimization method and device, and relates to the technical field of computers. A specific embodiment of the method comprises the steps of obtaining an abstract syntax tree in a memory; respectively determining node attributes of one or more nodes according to expressions corresponding to one or more nodes in the abstract syntax tree; determining memory optimization nodes from one or more nodes according to the node attributes; and generating a memory optimization strategy corresponding to the memory optimization node, and inserting the memory optimization strategy into the action range corresponding to the memory optimization node so as to optimize the memory corresponding to the memory optimization node through the memory optimization strategy. According to the embodiment, the memory optimization node corresponding to each attribute can be determined from the nodes, and the memory optimization strategy is correspondingly inserted, so that the memory optimization node is automatically optimized and the memory is released when the corresponding operation is triggered according to the memory optimization strategy, the stability of the storage space in the memory is ensured, and the memory optimization efficiency is improved. The overall performance of the system is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for memory optimization. Background Art

[0002] A memory leak is when the allocated memory cannot be released during the running of an application, resulting in a waste of system memory. Common JavaScript memory leaks include: unexpected global variables, forgotten timers, and unremoved event listeners.

[0003] Currently, in order to prevent possible memory leaks, developers need to add corresponding processing logic in the code to release memory in a timely manner. However, due to lack of developer experience or negligence, some no longer needed memory is often not released, resulting in memory leaks, which in turn affects system performance and even causes process crashes. Summary of the invention

[0004] In view of this, an embodiment of the present invention provides a method and device for memory optimization, which can determine the node attributes corresponding to each node according to the expressions corresponding to each node in the abstract syntax tree, determine the memory optimization nodes that can be optimized (may have memory leaks) under each attribute from multiple nodes, and insert memory optimization strategies accordingly, so that when the corresponding operation is triggered according to the memory optimization strategy, the memory optimization nodes are automatically optimized and the memory is released, thereby ensuring the stability of the storage space in the memory and improving the overall performance of the system.

[0005] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for memory optimization is provided.

[0006] A memory optimization method of an embodiment of the present invention includes: obtaining an abstract syntax tree in the memory; determining node attributes of the one or more nodes according to expressions corresponding to the one or more nodes in the abstract syntax tree; determining a memory optimization node from the one or more nodes according to the node attributes; generating a memory optimization strategy corresponding to the memory optimization node, and inserting the memory optimization strategy into a scope corresponding to the memory optimization node, so as to optimize the memory corresponding to the memory optimization node through the memory optimization strategy.

[0007] Optionally, the method further includes: determining the callee of the node according to the calling expression corresponding to the one or more nodes; and determining that the node is a memory optimization node when the callee of the node is a timer.

[0008] Optionally, generating a memory optimization policy corresponding to the memory optimization node includes: generating a memory optimization policy for the memory optimization node according to a variable name declared by a parent node of the node, so as to utilize the memory optimization policy to release the memory occupied by the timer when the page is inactivated.

[0009] Optionally, the method further includes: when it is detected that the page is reactivated, re-adding a timer corresponding to the node according to the variable name declared by the parent node.

[0010] Optionally, the method further includes: determining the attribute name of the node according to the member expression corresponding to the one or more nodes; and when the node is determined to be an event listening node according to the attribute name, determining the event listening node to be a memory optimization node.

[0011] Optionally, generating a memory optimization policy corresponding to the memory optimization node includes: detecting whether the DOM tree corresponding to the event listening node is broken; generating a memory optimization policy corresponding to the memory optimization node when the DOM tree is broken, the memory optimization policy indicating removal of the event listening node and the corresponding DOM tree.

[0012] Optionally, the method further includes: determining the variable name corresponding to the assignment expression according to the assignment expression of the one or more nodes; querying whether there is a declaration keyword corresponding to the variable name within the scope corresponding to the assignment expression; if not, determining that the node is a memory optimized node.

[0013] Optionally, generating the memory optimization strategy corresponding to the memory optimization node includes: adding the declaration keyword to the variable name to convert the variable in the assignment expression into a local variable.

[0014] To achieve the above objective, according to another aspect of an embodiment of the present invention, a memory optimization device is provided.

[0015] A memory optimization device according to an embodiment of the present invention includes: an acquisition module for acquiring an abstract syntax tree in a memory; a determination module for determining node attributes of one or more nodes according to expressions corresponding to one or more nodes in the abstract syntax tree; determining a memory optimization node from the one or more nodes according to the node attributes; and an optimization module for generating a memory optimization strategy corresponding to the memory optimization node, and inserting the memory optimization strategy into a scope corresponding to the memory optimization node, so as to optimize the memory corresponding to the memory optimization node through the memory optimization strategy.

[0016] To achieve the above objective, according to another aspect of an embodiment of the present invention, a memory optimized electronic device is provided.

[0017] A memory optimized electronic device according to an embodiment of the present invention comprises: one or more processors; a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement a memory optimization method according to an embodiment of the present invention.

[0018] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided.

[0019] A computer-readable storage medium according to an embodiment of the present invention stores a computer program, and when the program is executed by a processor, a memory optimization method according to an embodiment of the present invention is implemented.

[0020] An embodiment of the above invention has the following advantages or beneficial effects: it can determine the node attributes corresponding to each node according to the expressions corresponding to each node in the abstract syntax tree, determine the memory optimization nodes that can be optimized (may have memory leaks) under each attribute from multiple nodes, and insert the corresponding memory optimization strategy, so that when the corresponding operation is triggered according to the memory optimization strategy, the memory optimization node is automatically optimized and the memory is released, thereby ensuring the stability of the storage space in the memory and improving the overall performance of the system.

[0021] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0023] Figure 1 is a flowchart of a method for memory optimization according to an embodiment of the present invention;

[0024] Figure 2 is a schematic diagram of a process for determining a memory optimization node according to an embodiment of the present invention;

[0025] Figure 3 is another schematic diagram of a process for determining a memory optimization node according to an embodiment of the present invention;

[0026] Figure 4 is a schematic diagram of a process for determining whether a DOM tree is broken according to an embodiment of the present invention;

[0027] Figure 5 is a schematic diagram of another process of determining a memory optimization node according to an embodiment of the present invention;

[0028] Figure 6is a schematic diagram of main modules of a memory optimization device according to an embodiment of the present invention;

[0029] Figure 7 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0030] Figure 8 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

[0031] 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.

[0032] It should be pointed out that the embodiments of the present invention and the technical features therein may be combined with each other without conflict.

[0033] During the operation of the front-end server, JavaScript is usually selected to complete the automatic recycling of memory, that is, JavaScript helps developers manage memory by periodically checking whether the previously allocated memory is reachable, and determines and marks the unreachable memory through an algorithm, and is recycled by the operating system at a preset time, while those that are not marked cannot be recycled by the operating system, thereby realizing the regular release of memory. However, this automatic recycling strategy is not foolproof, and there are still some JavaScript memory leaks, such as unexpected global variables, forgotten timers, and unremoved event listeners. Therefore, the present invention mainly sets corresponding memory optimization strategies on the memory optimization node for the above three possible memory leak situations, so as to release useless memory space in a timely manner in the continuously running service process.

[0034] Figure 1 It is a schematic diagram of main steps of a method for memory optimization according to an embodiment of the present invention.

[0035] like Figure 1 As shown, the memory optimization method of the embodiment of the present invention mainly includes the following steps:

[0036] Step S101: Obtain an abstract syntax tree in memory;

[0037] Step S102: determining node attributes of one or more nodes respectively according to expressions corresponding to one or more nodes in the abstract syntax tree;

[0038] Step S103: determining a memory optimization node from one or more nodes according to node attributes;

[0039] Step S104: Generate a memory optimization policy corresponding to the memory optimization node, and insert the memory optimization policy into the scope corresponding to the memory optimization node, so as to optimize the memory corresponding to the memory optimization node through the memory optimization policy.

[0040] Among them, the Abstract Syntax Tree (AST), referred to as the syntax tree, is an abstract representation of the syntax structure of the source code. It represents the syntax structure of the programming language in a tree form, and each node on the tree represents a structure in the source code, which can be an expression, a string, a function, a variable, etc. For step S101, the structure of each node in the abstract syntax tree can be directly obtained through the front-end construction tool, that is, when the node structure is an expression, the expression can be directly obtained, and when the node structure is a function, the function can be directly obtained.

[0041] For step S102, the node attribute can be understood as the attribute corresponding to the expression in the node. In an optional embodiment, when the expression is a call expression, the process of determining the memory optimization node in steps S102 to S103 is as follows: Figure 2 As shown, including:

[0042] Step S201: determining the callee of a node according to a call expression corresponding to one or more nodes;

[0043] Step S202: when the callee of the node is a timer, determine that the node is a memory optimization node.

[0044] For a calling expression, the callee is one of the node attributes of the node. When the callee is a timer, it means that the calling expression is to trigger a specific operation at a certain specific time. After exceeding the specific time, the timer will face failure. It is necessary to delete the corresponding node to release the occupied memory after the timer expires. Therefore, in an optional embodiment, when the expression is a calling expression and the callee of the node is a timer, the memory optimization strategy corresponding to the memory optimization node is generated in step S104, which can further include: generating a memory optimization strategy for the memory optimization node according to the variable name declared by the parent node of the node, so as to use the memory optimization strategy to release the memory occupied by the timer when the page is inactivated.

[0045] Specifically, the variable name declared by the node can represent the meaning of the node, such as the content of the timing and the time of the timing, etc. It should be noted that if the parent node of the node has a declared variable name, the memory optimization strategy of the memory optimization node can be generated directly according to the variable name declared by the parent node of the node. In the case that the parent node of the node does not have a declared variable name, it is necessary to generate the variable name corresponding to the parent node of the node before continuing to generate the memory optimization strategy of the memory optimization node. In an optional embodiment, the memory optimization strategy of the memory optimization node can be to remove the timer when the page is deactivated, which is achieved by inserting the code clearTimeInterval() into the scope of the timer, wherein the content in the brackets is the variable name corresponding to the parent node. Through the above-mentioned optimization strategy, when the page is deactivated, the timer can be automatically removed from the parent node of the node through the code in the scope of the timer, thereby achieving the purpose of releasing memory.

[0046] In a further optional embodiment, when it is detected that the page is reactivated, it may also include: re-adding the timer corresponding to the node according to the variable name declared by the parent node. In other words, by activating and deactivating the page, the timer is automatically inserted or removed, which can not only ensure the normal use of the timer when the page is applied, but also release the memory space when the page is closed, effectively improving the system performance.

[0047] For step S102, in another optional embodiment, when the expression is a member expression, the process of determining the memory optimization node in steps S102 to S103 is as follows: Figure 3 As shown, including:

[0048] Step S301: determining the attribute name of the node according to the member expression corresponding to one or more nodes;

[0049] Step S302: when it is determined according to the attribute name that the node is an event listening node, the event listening node is determined to be a memory optimization node.

[0050] For a member expression, the attribute name of a node is one of the node attributes of the node. In an optional embodiment, when the attribute name is on or addeventlistener, the node can be determined as an event listening node.

[0051] In actual application, the developer may add a listener to the code, but fail to remove the listener in the subsequent logic, causing the listener to occupy memory for a long time, resulting in a memory leak. Therefore, it is necessary to detect the listener first to determine the listener that needs to be removed, and the listener usually exists in the form of a DOM tree in the listener node. Therefore, in an optional embodiment, when the expression is a member expression, the memory optimization strategy corresponding to the memory optimization node generated in step S104 can be as follows Figure 4 As shown, further comprising:

[0052] Step S401: Detect whether the DOM tree corresponding to the event listening node is broken;

[0053] Step S402: generating a memory optimization policy corresponding to the memory optimization node when the DOM tree is broken, the memory optimization policy instructing to remove the event listening node and the corresponding DOM tree.

[0054] For step S401, it is possible to determine whether the DOM tree is broken by traversing the child nodes in the DOM tree, depending on whether the root node can be found. By searching upwards for the child nodes step by step, if the root node can be found, it means that the DOM tree is not broken. But if the root node cannot be found, it means that the parent node cannot be found in a certain level of search process, i.e., the DOM tree is broken. For the broken DOM tree, in an optional embodiment, the memory optimization strategy in step S402 can be inserted into the scope of the event listener to achieve removal of the listener and the DOM, so that the garbage collector can automatically recycle the useless event listener and the occupied memory is released.

[0055] For step S102, in another optional embodiment, when the expression is an assignment expression, the process of determining the memory optimization node in steps S102 to S103 is as follows: Figure 5 As shown, including:

[0056] Step S501: Determine the variable name corresponding to the assignment expression according to the assignment expression of one or more nodes;

[0057] Step S502: Query whether there is a declaration keyword corresponding to the variable name in the scope corresponding to the assignment expression;

[0058] Step S503: If not, determine that the node is a memory optimized node.

[0059] For assignment expressions, the declaration keyword corresponding to the variable name is one of the node attributes of the node. In actual applications, when developers forget to set the corresponding keyword in the declaration variable of the local scope, it will cause unexpected global variables, and all variables will continue to occupy memory, which is also a common memory leak in the front end.

[0060] It should be noted that if the corresponding declaration keyword exists in the scope of the assignment expression, it means that it is a local variable. Therefore, memory optimization only needs to be performed on the assignment expressions that are empty in the scope or have been declared by other assignment expressions.

[0061] In an optional embodiment, when the expression is an assignment expression, the memory optimization strategy corresponding to the memory optimization node generated in step S104 may further include: adding a declaration keyword to the variable name to convert the variable in the assignment expression into a local variable. Exemplarily, adding "var" before the variable name can convert the scope from a global variable to a local variable.

[0062] To sum up, the memory optimization method according to the embodiment of the present invention can determine the node attributes corresponding to each node according to the expressions corresponding to each node in the abstract syntax tree, determine the memory optimization nodes that can be optimized (may have memory leaks) under each attribute from multiple nodes, and insert the memory optimization strategy accordingly, so that when the corresponding operation is triggered according to the memory optimization strategy, the memory optimization node is automatically optimized to release the memory, thereby ensuring the stability of the storage space in the memory and improving the overall performance of the system.

[0063] Figure 6 4 is a schematic diagram of main modules of a memory optimization device according to an embodiment of the present invention.

[0064] like Figure 6 As shown, the memory optimization device 600 of the embodiment of the present invention includes:

[0065] An acquisition module 601 is used to acquire an abstract syntax tree in a memory;

[0066] A determination module 602 is used to determine node attributes of the one or more nodes in the abstract syntax tree according to expressions corresponding to the one or more nodes; and to determine a memory optimization node from the one or more nodes according to the node attributes;

[0067] The optimization module 603 is used to generate a memory optimization strategy corresponding to the memory optimization node, and insert the memory optimization strategy into the scope corresponding to the memory optimization node, so as to optimize the memory corresponding to the memory optimization node through the memory optimization strategy.

[0068] In an optional embodiment of the present invention, the determination module 602 is also used to determine the callee of the node according to the calling expression corresponding to the one or more nodes; when the callee of the node is a timer, determine that the node is a memory optimization node.

[0069] In an optional embodiment of the present invention, the determination module 602 is also used to generate a memory optimization strategy for the memory optimization node based on the variable name declared by the parent node of the node, so as to utilize the memory optimization strategy to release the memory occupied by the timer when the page is inactivated.

[0070] In an optional embodiment of the present invention, the memory optimization device 600 further includes an adding module for re-adding the timer corresponding to the node according to the variable name declared by the parent node when it is detected that the page is reactivated.

[0071] In an optional embodiment of the present invention, the determination module 602 is also used to determine the attribute name of the node according to the member expression corresponding to the one or more nodes; when the node is determined to be an event listening node according to the attribute name, the event listening node is determined to be a memory optimization node.

[0072] In an optional embodiment of the present invention, the determination module 602 is also used to detect whether the DOM tree corresponding to the event listening node is broken; if the DOM tree is broken, a memory optimization policy corresponding to the memory optimization node is generated, and the memory optimization policy indicates the removal of the event listening node and the corresponding DOM tree.

[0073] In an optional embodiment of the present invention, the determination module 602 is also used to determine, based on the assignment expression of the one or more nodes, the variable name corresponding to the assignment expression; query whether there is a declaration keyword corresponding to the variable name in the scope corresponding to the assignment expression; if not, determine that the node is a memory optimization node.

[0074] In an optional embodiment of the present invention, the declaration keyword is added to the variable name to convert the variable in the assignment expression into a local variable.

[0075] According to the memory optimization device of the embodiment of the present invention, it is possible to determine the node attributes corresponding to each node according to the expressions corresponding to each node in the abstract syntax tree, determine the memory optimization nodes that can be optimized (may have memory leaks) under each attribute from multiple nodes, and insert the memory optimization strategy accordingly, so that when the corresponding operation is triggered according to the memory optimization strategy, the memory optimization nodes are automatically optimized and the memory is released, thereby ensuring the stability of the storage space in the memory and improving the overall performance of the system.

[0076] Figure 7 An exemplary system architecture 700 is shown to which the memory optimization method or memory optimization apparatus according to the embodiments of the present invention can be applied.

[0077] like Figure 7 As shown, system architecture 700 may include terminal devices 701, 702, 703, network 704 and server 705. Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0078] Users can use terminal devices 701, 702, and 703 to interact with server 705 through network 704 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 701, 702, and 703, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc.

[0079] The terminal devices 701 , 702 , and 703 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.

[0080] The server 705 may be a server that provides various services, such as a background management server that provides support for browser pages browsed by users using the terminal devices 701, 702, and 703. The background management server may analyze and process the acquired abstract syntax tree and other data, and feed back the processing results (such as memory optimization nodes) to the terminal device.

[0081] It should be noted that the memory optimization method provided in the embodiment of the present invention is generally executed by the server 705 , and accordingly, the memory optimization device is generally set in the server 705 .

[0082] It should be understood that Figure 7 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.

[0083] Reference below Figure 8 , which shows a schematic diagram of the structure of a computer system 800 of a terminal device suitable for implementing an embodiment of the present invention. Figure 8 The terminal device 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.

[0084] like Figure 8As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.

[0085] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.

[0086] 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 809, and / or installed from the removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.

[0087] 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 above 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 of the above.

[0088] 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 above-mentioned module, program segment, or a part of a 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 different order from the order 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 a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0089] The modules involved in the embodiments of the present invention may be implemented by software or hardware. The modules described may also be set in a processor, for example, it may be described as: a processor includes an acquisition module, a determination module and an optimization module. The names of these modules do not constitute a limitation on the modules themselves in some cases, for example, the acquisition module may also be described as a "module for acquiring an abstract syntax tree in memory".

[0090] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: obtaining an abstract syntax tree in the memory; determining the node attributes of the one or more nodes respectively according to the expressions corresponding to the one or more nodes in the abstract syntax tree; determining a memory optimization node from the one or more nodes according to the node attributes; generating a memory optimization strategy corresponding to the memory optimization node, and inserting the memory optimization strategy into the scope corresponding to the memory optimization node, so as to optimize the memory corresponding to the memory optimization node through the memory optimization strategy.

[0091] According to the technical solution of the embodiment of the present invention, the node attributes corresponding to each node in the abstract syntax tree can be determined, and the memory optimization nodes that can be optimized (may have memory leaks) under each attribute can be determined from multiple nodes, and the memory optimization strategy can be inserted accordingly, so that when the corresponding operation is triggered according to the memory optimization strategy, the memory optimization node is automatically optimized and the memory is released, thereby ensuring the stability of the storage space in the memory and improving the overall performance of the system.

[0092] The above specific implementations do not constitute a limitation on the protection scope 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 modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A memory optimization method, characterized in that: include: Get the abstract syntax tree in memory; Determine node attributes of the one or more nodes according to expressions corresponding to the one or more nodes in the abstract syntax tree; Determining a memory optimized node from the one or more nodes according to the node attributes; A memory optimization policy corresponding to the memory optimization node is generated, and the memory optimization policy is inserted into the scope corresponding to the memory optimization node, so as to optimize the memory corresponding to the memory optimization node through the memory optimization policy.

2. The method according to claim 1, characterized in that: Determine the callee of the node according to the calling expression corresponding to the one or more nodes; When the callee of the node is a timer, the node is determined to be a memory optimization node.

3. The method according to claim 2, characterized in that The generating the memory optimization strategy corresponding to the memory optimization node includes: A memory optimization strategy of the memory optimization node is generated according to the variable name declared by the parent node of the node, so as to release the memory occupied by the timer when the page is deactivated by using the memory optimization strategy.

4. The method according to claim 3, characterized in that: Also includes: When it is detected that the page is reactivated, the timer corresponding to the node is re-added according to the variable name declared by the parent node.

5. The method according to claim 1, characterized in that Determine the attribute name of the node according to the member expression corresponding to the one or more nodes; When it is determined according to the attribute name that the node is an event listening node, the event listening node is determined to be a memory optimization node.

6. The method according to claim 5, characterized in that The generating the memory optimization strategy corresponding to the memory optimization node includes: Detect whether the DOM tree corresponding to the event listening node is broken; In the case where the DOM tree is broken, a memory optimization policy corresponding to the memory optimization node is generated, and the memory optimization policy indicates to remove the event listening node and the corresponding DOM tree.

7. The method according to claim 1, characterized in that Determine, according to the assignment expressions of the one or more nodes, variable names corresponding to the assignment expressions; Querying whether there is a declaration keyword corresponding to the variable name in the scope corresponding to the assignment expression; If not, the node is determined to be a memory optimized node.

8. The method according to claim 7, characterized in that The generating the memory optimization strategy corresponding to the memory optimization node includes: The declaration keyword is added to the variable name to convert the variable in the assignment expression into a local variable.

9. A memory optimization device, characterized in that: include: The acquisition module is used to obtain the abstract syntax tree in memory; A determination module, configured to determine node attributes of the one or more nodes in the abstract syntax tree according to expressions corresponding to the one or more nodes; Determining a memory optimized node from the one or more nodes according to the node attributes; An optimization module is used to generate a memory optimization strategy corresponding to the memory optimization node, and insert the memory optimization strategy into the scope corresponding to the memory optimization node, so as to optimize the memory corresponding to the memory optimization node through the memory optimization strategy.

10. An electronic device for memory optimization, 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 8.

11. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.