CN111258926B - Memory recovery method and device, storage medium and computer equipment - Google Patents

Memory recovery method and device, storage medium and computer equipment Download PDF

Info

Publication number
CN111258926B
CN111258926B CN202010120300.9A CN202010120300A CN111258926B CN 111258926 B CN111258926 B CN 111258926B CN 202010120300 A CN202010120300 A CN 202010120300A CN 111258926 B CN111258926 B CN 111258926B
Authority
CN
China
Prior art keywords
traversal
array
objects
target application
current
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010120300.9A
Other languages
Chinese (zh)
Other versions
CN111258926A (en
Inventor
何欢
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tencent Technology Shenzhen Co Ltd
Original Assignee
Tencent Technology Shenzhen Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tencent Technology Shenzhen Co Ltd filed Critical Tencent Technology Shenzhen Co Ltd
Priority to CN202010120300.9A priority Critical patent/CN111258926B/en
Publication of CN111258926A publication Critical patent/CN111258926A/en
Application granted granted Critical
Publication of CN111258926B publication Critical patent/CN111258926B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F12/00Accessing, addressing or allocating within memory systems or architectures
    • G06F12/02Addressing or allocation; Relocation
    • G06F12/0223User address space allocation, e.g. contiguous or non contiguous base addressing
    • G06F12/023Free address space management
    • G06F12/0253Garbage collection, i.e. reclamation of unreferenced memory

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Processing Or Creating Images (AREA)

Abstract

The application relates to a memory recovery method, a memory recovery device, a storage medium and computer equipment, wherein the method comprises the following steps: when the memory recovery condition is met, the target application is paused to run, and the traversal marks of each object which is not deleted after the target application is created until the current time are cleared; traversing whether each object is the object necessary for the running of the target application or not in sequence from the position of the object in the target sequence, adding a traversal mark to the traversed necessary object, and stopping traversing when a preset single recovery time limit is reached or the traversal is finished; when the traversal is not finished, continuing to operate the target application for unit time and then pausing the operation again, determining the position of the paused traversal as the position of the target sequential object, and returning to the step of the traversal; and when the traversal is finished, deleting the objects without the traversal marks which are not deleted after the target application is created until the current time, and continuously operating the target application to wait for the next memory recovery. The scheme can eliminate page jamming caused by memory recovery.

Description

Memory recovery method and device, storage medium and computer equipment
Technical Field
The present application relates to the field of internal management technologies, and in particular, to a method and an apparatus for memory recovery, a storage medium, and a computer device.
Background
Garbage Collection (GC) is an automatic memory management mechanism that can automatically search and delete useless objects in a specified memory area, thereby releasing the memory. A commonly used GC algorithm is Mark-Sweep, and the main mechanism of the algorithm is to suspend the operation of the target application when garbage collection is needed, search through objects still needed by the target application in the memory and Mark the objects, and then delete the unmarked objects as garbage.
However, the tag elimination algorithm needs to complete traversal of all objects in the memory at one time, so that the target application is suspended for a long time, which causes obvious "stuck", and cannot meet the requirements of some scenes with high requirements on smoothness of operation, such as game scenes, particularly the phenomenon that the content of the current game is more and more complex, objects are dynamically created and the reference relationship between the objects is changed frequently.
Disclosure of Invention
In view of the foregoing, it is necessary to provide a memory recycling method, device, storage medium, and computer apparatus for solving the technical problem of scattered note content of a video production operator.
A memory reclamation method, comprising:
when the memory recovery condition is met, the target application is paused to run, and the traversal marks of each object which is not deleted after the target application is created until the current time are cleared;
traversing whether each object is the object necessary for the running of the target application or not in sequence from the position of the object in the target sequence, adding a traversal mark to the traversed necessary object, and stopping traversing when a preset single recovery time limit is reached or the traversal is finished;
when the traversal is paused but not completed, continuing to operate the target application for unit time and pausing the operation again, determining the position of the last traversal paused as the position of the target sequential objects, and returning to the step of sequentially traversing whether each object is the object necessary for the operation of the target application from the position of the target sequential objects;
and when the traversal is finished, deleting the objects which are not deleted after the target application is created until the current time and have no traversal mark, and continuously operating the target application to wait for next memory recovery.
A memory reclamation apparatus, the apparatus comprising:
the preparation module is used for suspending the running of the target application when the memory recovery condition is met, and clearing the traversal marks of each object which is not deleted after the target application is created until the current time;
the marking module is used for traversing whether each object is the object required by the running of the target application or not in sequence from the position of the object in the target sequence, adding a traversal mark to the traversed required object, and stopping traversal when a preset single recovery time limit is reached or the traversal is finished; when the traversal is paused but not completed, continuing to operate the target application for unit time and pausing the operation again, determining the position of the last traversal paused as the position of the target sequential objects, and returning to the step of sequentially traversing whether each object is the object necessary for the operation of the target application from the position of the target sequential objects;
and the deleting module is used for deleting the objects which are not deleted after the target application is created until the current time and have no traversal mark when the traversal is finished, and continuously operating the target application to wait for next memory recovery.
A computer-readable storage medium, storing a computer program which, when executed by a processor, causes the processor to perform the steps of the memory reclamation method described above.
A computer device comprising a memory and a processor, the memory storing a computer program that, when executed by the processor, causes the processor to perform the steps of the memory reclamation method described above.
According to the memory recovery method, the device, the computer readable storage medium and the computer equipment, in the internal recovery marking stage, the target application pauses once per running unit time to complete one marking, so that a large marking task in one round of memory recovery is divided into a plurality of small marking tasks to be inserted into the running process of the target application to be executed, each small marking task has a corresponding single recovery time limit, the required time is very short, meanwhile, the light recovery mechanism improves the memory recovery frequency, greatly reduces the internal expense of the target application, shows page refreshing from the front-end page display to be smoother, and eliminates the phenomenon of page blocking.
Drawings
FIG. 1 is a diagram of an exemplary embodiment of an application environment for a memory reclamation method;
FIG. 2 is a flow chart illustrating a memory reclamation method according to an embodiment;
FIG. 3 is a schematic diagram illustrating a memory reclamation method in accordance with an embodiment;
FIG. 4 is a flow diagram illustrating the initial preparation steps traversed in one embodiment;
FIG. 5 is a flowchart illustrating the step of traversing objects in an exemplary embodiment;
FIG. 6 is a flowchart illustrating a step of searching for an object to be deleted in an exemplary embodiment;
FIG. 7 is a flowchart illustrating a step of deleting an object to be deleted in an exemplary embodiment;
FIG. 8 is a schematic diagram illustrating a memory reclamation method in accordance with another embodiment;
FIG. 9 is a block diagram illustrating an exemplary memory reclamation apparatus;
FIG. 10 is a block diagram of a memory reclamation apparatus in another embodiment;
FIG. 11 is a block diagram showing the structure of a computer device in one embodiment;
FIG. 12 is a block diagram of a computer device in one embodiment.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of and not restrictive on the broad application.
Fig. 1 is a diagram illustrating an application environment of a memory reclamation method according to an embodiment. The memory recovery method is applied to a memory recovery system. The memory recycling system includes a terminal 110 and a server 120. The terminal 110 and the server 120 are connected through a network. The terminal 110 may run a target application such as a gaming application, a web browsing application, a social application, a video application, and the like. Server 120 is a background server that serves the target application. The user uses the target application at the terminal 110, and the target application loads the local cache or the data resource pulled from the server 120 to realize page display. The terminal 110 may be a desktop terminal or a mobile terminal, and the mobile terminal may be at least one of a mobile phone, a tablet computer, a notebook computer, and the like. In some embodiments, the terminal 110 may also be a vending machine, gaming machine, or the like. The server 120 may be implemented as a stand-alone server or a server cluster composed of a plurality of servers.
In one embodiment, as shown in FIG. 2, a method for memory reclamation is provided. The embodiment is mainly exemplified by applying the method to a computer device, and the computer device may specifically be the terminal 110 or the server 120 in fig. 1. Referring to fig. 2, the memory recovery method specifically includes the following steps:
s202, when the memory recovery condition is met, the target application is paused to run, and the traversal marks of each object which is not deleted after the target application is created until the current time are cleared.
The memory recovery condition refers to a trigger condition for starting running the GC thread. A target application running on a computer device has a corresponding application thread and GC thread. The target application runs based on the application thread. And the GC algorithm based on GC thread operation is used for recycling memory garbage generated by the operation of the target application. The memory recycling condition may specifically be that a preset memory recycling period is reached or that the memory occupancy rate reaches a threshold value. The memory recycling period refers to a time period for performing memory recycling, such as 30 seconds or 1 minute, that is, memory recycling is performed every half minute or 1 minute. The memory recovery period needs to be set reasonably according to the complexity of the data resource loaded by the target application, the target application is interrupted frequently when the memory recovery period is too short, and a large amount of memory is occupied continuously when the memory recovery period is too long, so that the target application is stuck or even dead. The threshold of the memory occupancy rate is a preset maximum value of the memory occupancy rate, such as 60%.
In a target application, an object is an instantiation of a class. Classes are abstractions of entities with the same characteristics, and do not need to occupy memory, while objects are concrete entities and need to occupy memory space. In a Java-based target application, a New object can be created by the keyword New. For example, in a game scene, the objects involved may include buildings, flowers and other objects in the virtual scene, virtual objects, and equipment such as a torpedo, a pistol and the like for the virtual objects. Objects can refer to each other. If the object is not used for a long time after being created, the object is regarded as garbage and deleted so as to recycle the occupied memory.
The traversal mark is a mark added to the object in the traversal process of the object created by the target application for representing that the object has been traversed, and may specifically be an identification code, such as a preset character string. Objects that are necessary for the target application that have been traversed have corresponding identification codes, objects that are not traversed or that have been traversed but are not necessary for the target application do not have corresponding identification codes, and the corresponding clear traversal marker may be a delete identification code. The traversal flag may also be a state code, such as "1" indicating traversed, "0" indicating not traversed, etc., and a corresponding clearing of the traversal flag may be a resetting of "1" to "0".
The target application may be viewed as one run (including a background run) from launch to exit. A target application may require multiple rounds of memory reclamation during a run. The current time is the time for starting the memory recovery of the current round and can be recorded as Ti, where i is the round of memory recovery performed during the current running period of the target application. For example, T1 represents the first memory reclamation round during the current run of the target application. The following description will take the memory recycling of the current round as the internal memory recycling of the first round as an example.
Specifically, when the computer device is implemented as a terminal, the terminal renders a page resource cached locally or pulled from a server based on a rendering engine in a running target application, and displays an application page. The rendering engine is a core component in the target application, and is used for rendering the page resource, and specifically may be Unity, UE4(unreal engine 4), and the like. The user can browse the page content of the application page or perform control operation on the page content in the application page. For example, in a game scene, a user can perform interactive control on the position, the action, the adopted equipment and the like of a virtual object in the virtual scene on a game page. When the computer device is implemented as a server, the server renders the page resources based on a rendering engine in the running target application, and returns the rendered page to the terminal.
During the running of the target application, the computer device monitors whether the memory recovery condition is currently met. When the memory reclamation condition is not met, the computer device continues to run the target application. And when the memory recovery condition is met, the computer equipment controls to suspend the running of the target application by suspending the application thread, and starts the GC thread to start memory recovery. The main mechanism for memory recovery based on the GC algorithm operated by the GC thread comprises a marking stage and a deleting stage, and specifically, after the addition of the traversal marks is completed in the marking stage, the deletion stage is carried out to delete the objects without the traversal marks. When the memory recovery of the current round is started, the computer equipment records the current time and prepares before marking, namely, the traversal mark added for the object in the last memory recovery is cleared firstly.
And S204, traversing whether each object is the object required by the running of the target application or not in sequence from the position of the object in the target sequence, adding a traversal mark to the traversed object, and stopping traversing when a preset single recovery time limit is reached or the traversal is finished.
The objects necessary for the target application to run are objects on which the target application normally runs within the target duration after and at the current time T1, and include a Root Object (Root Object) and objects to which the Root Object directly or indirectly refers. The root object is the object on which the target application is dependent to function normally at the current time T1. For example, in the game scenario, the objects necessary for the game application at time T1 include a global manager, a spreadsheet configuration object, a current map level object, and the like. The object to which the root object points, i.e., the object directly or indirectly applied by the root object, is an object that the target application may need after the current time T1.
As above, the memory reclamation mechanism of the GC algorithm includes a marking process and a deleting process. The marking process in one round of memory recovery is completed for multiple times, and a part of marking tasks are completed each time. The single recycling time limit is the longest time limit preset for performing one-time marking, such as 1 millisecond. In other words, the time tij taken to actually perform the marking once does not exceed the single recovery time limit. And tij is the time of starting the jth mark in the ith round of memory recovery in the current running period of the target application.
The position of the object in target order is the position where each marker starts to traverse. One round of memory recovery needs to be marked for multiple times, namely, multiple traversals are needed, so that the positions of the objects in the target sequence are different when the objects are marked each time. When the traversal is performed directly based on the program code of the target application, the position of the object of the target sequence corresponding to the first traversal is the position of the first undeleted object in the program code. The position of the object whose current mark corresponds to the target order is the position at which the previous mark was paused. For example, assuming that the plurality of objects in the ordered arrangement to be traversed includes a1-a10, the first time the label traverses A3, the object in the target order corresponding to the second time label is A3.
Specifically, the computer device, upon starting a round of memory recovery, determines the undeleted objects created by the target application running to the current time T1, i.e., locates all undeleted objects of the target application at time T1. During the memory recovery round, the target application can create a new object at any time, and the new object is arranged to traverse in the next memory recovery round. And the computer equipment starts the marking at this time, and sequentially traverses whether each object belongs to the root object or the object directly or indirectly referenced by the root object from the position of the object in the target sequence. If so, adding a traversal mark for the object by the computer equipment; otherwise, no traversal marker is added.
The computer device counts time each time a marker is started, and traverses as many objects as possible based on the processing capacity of a current Central Processing Unit (CPU) and the like within a single recycling time limit. And when the single recovery time limit is reached and the traversal of the traversal array is not finished, the computer equipment suspends the traversal. And when the single recovery time limit is not reached but the determined objects are traversed completely, the computer equipment finishes the traversal and finishes all marking processes of the memory recovery.
In one embodiment, to improve traversal efficiency, the computer device, upon starting a round of memory retrieval, determines objects created by the target application running to the current time T1 that have not been deleted, and may construct a sequence of objects based on the determined objects. The sequence of objects can then be traversed directly without the program code being traversed line by line.
S206, when the traversal is paused but not completed, the target application is paused again after unit time of operation, the position of the last pause of traversal is determined as the position of the target sequential object, and the steps of sequentially traversing each object from the position of the target sequential object to determine whether the object is the object necessary for the operation of the target application are returned.
The unit duration t is a duration that the target application is specified in advance to be a duration of the marking phase of the memory recovery, such as 30 milliseconds. When the target application is a game application, a video application, or the like, that requires presentation of a video picture, the unit time length may be determined according to the frame rate of the video. The video comprises a plurality of video frames, and the frame rate is the number of frames per second of the refreshed video frames, such as 30 frames/second. Taking a frame rate of 30 frames/second as an example, if framing marking is desired, i.e. marking every time a frame of video is played, the unit duration may be 33 milliseconds, which is the time required to play a frame. If it is desired to mark every two consecutive frames of video, the unit duration may be 66 milliseconds of the time required to play the two frames. And so on.
Specifically, if the traversal is suspended but the traversal is not completed, the computer device starts to run the target application, suspends the running again after the target application runs for a unit time, and starts to mark for the next time. Each mark is continuously traversed at the position of the last traversal stop until the last object traversal is completed. That is, one round of memory recovery actually performs one round of complete traversal on the object to be traversed, but the complete traversal is completed in a relay manner for multiple times.
In one embodiment, the target application is used to play a video; the video comprises a plurality of video frames; when the memory recovery condition is met, the suspending the running of the target application comprises the following steps: when the memory recovery condition is met, pausing playing the video when the playing of the current sequence video frame is finished; when the traversal is paused but not completed, the operation is paused again after the target application is continuously operated for a unit time, which comprises the following steps: when the traversal is paused but not completed, the next sequential video frame is continuously played; and when the next sequential video frame is played, pausing the playing of the video.
In a video playing scene (such as a game scene), a framing mark is taken as an example, when the pause of a target application is finished when one video frame played by a video page is loaded, namely, the mark is finished once the pause of one video frame is finished, so that a large marking task in one round of memory recovery is divided into a plurality of small marking tasks to be inserted into the video playing process for execution, each small marking task has a corresponding single recovery time limit, the required time is very short, and the page refreshing is shown to be smoother from the front-end page display.
And S208, when the traversal is finished, deleting the object without the traversal mark created by the target application until the current time, and continuing to run the target application to wait for the next memory recovery.
When all the determined objects are completed when the memory recovery of the current round is started, the marking task of the memory recovery of the current round is completed, and the task of deleting can be started to be executed. Specifically, the computer device performs a new round of traversal on the object to be traversed determined when the memory recovery of the current round starts again to determine the object without the traversal mark therein, and deletes the found object without the traversal mark as a useless object. The process of deleting the useless objects can also be carried out in the running period of the target application according to the logic of the marking process, namely, the target application pauses to delete a part of useless objects in each running unit time length until all the useless objects are deleted, and the memory recovery of the current round is finished.
It should be noted that, as described above, during the memory reclamation of the current round, the target application may create a new object at any time, in the marking stage, the object that is frozen at the moment when the memory reclamation of the current round is started is traversed, and the deletion is only to delete the object that is frozen at the moment when the memory reclamation of the current round is started, and the new object created during the current round is arranged to be marked when the memory reclamation of the next round is performed, and is not deleted.
In an embodiment, the objects to be traversed determined when the memory recovery of the current round starts can also be split into multiple groups, and the multiple groups of objects to be traversed are synchronously traversed based on multiple threads, so that the times marked during each round of memory recovery can be reduced, the times of pause of the target application can be reduced, and the running smoothness of the target application can be further improved.
According to the memory recovery method, in the internal recovery marking stage, the target application pauses once per unit time length of operation to complete one marking, so that a large marking task in one round of memory recovery is divided into a plurality of small marking tasks to be inserted into the target application operation process to be executed, each small marking task has a corresponding single recovery time limit, the required time is very short, meanwhile, the light recovery mechanism improves the memory recovery frequency, the internal expense of the target application is greatly reduced, page refreshing is more smooth from the front-end page display, and the phenomenon of page blocking is eliminated.
In one embodiment, the memory reclamation method further includes: establishing a traversal array and a corresponding array pointer based on an object required by the running of the target application; pointing an array pointer to an object traversing a first sequence in an array; traversing whether each object is an object necessary for the running of the target application or not in sequence from the position of the object in the target sequence, wherein adding a traversal mark to the traversed necessary object comprises: adding a traversal mark to a current object pointed by the group pointer within a preset single recovery time limit, and adding the pointed object to a traversal array to wait for traversal when the current object points to other objects; and pointing the array pointer to the next object in the traversal array to continue traversing until the single recovery time limit reaches or the traversal of the traversal array is finished, and pausing the traversal.
The traversal array may be an array created when the memory is recycled, and the array elements are continuously spread in the traversal process. The initial traversal array CheckArray created when starting this memory recovery contains the root objects necessary to record the target application. In the marking phase, objects directly or indirectly referenced by the root object are continuously added to the traversal array CheckArray.
The array pointer ItPtr is a pointer defined concurrently with the traversal of the array CheckArray to point to a different array element of the traversal array. The initial array pointer ItPtr points to the array element in the first order in the traversal array CheckArray. When the traversal is performed based on the traversal array, the positions of the objects in the target sequence are pointer addresses pointing to objects in different sequences in the traversal array.
Specifically, referring to fig. 3, fig. 3 is a schematic diagram illustrating a memory reclamation round in one embodiment. As shown in fig. 3, when the memory reclamation condition is met, the computer device first performs a traversal initial preparation. In the initial preparation stage of traversal, the computer device mainly clears the traversal mark and initializes the traversal array. The computer device initializes the traversal array based on the root object necessary for the target application and initializes the array pointer ItPtr to point to the first array element of the traversal array.
The computer device starts this marking and first checks whether the current object already has a traversal mark. If yes, skipping directly, and starting to traverse the next sequential object. If not, the computer equipment adds a traversal mark to the current object and confirms whether the current object directly or indirectly applies other objects. If the current object directly or indirectly applies other objects, the computer device appends the object applied by the current object to the tail of the traversal array, points the array pointer ItPtr to the next sequential object in the traversal array, and continues to traverse by taking the next sequential object as the current object according to the logic. If the current object does not apply other objects, the computer device directly points the array pointer ItPtr to the next sequential object in the traversal array, and continues traversing the next sequential object as the current object according to the logic.
In this embodiment, object marking is performed based on the traversal array, and object marking efficiency can be improved.
In one embodiment, building a traversal array and corresponding array pointers based on objects necessary for the target application to run includes: acquiring a root array used for recording objects necessary for running of a target application at the current time; initializing a traversal array, and copying an object in the root array as an array element of the traversal array; an array pointer is initialized for pointing to different array elements in the traversal array.
Among other things, the target application provides two important arrays based on the rendering engine: the system comprises a full array AllObjs for recording all objects which are created by the target application during the running process and are not deleted, and a root array RootObjs for recording root objects required by the running of the target application. The initial traversal array CheckArray may be obtained by copying the root array RootObjs.
Notably, the objects created by the target application at different times during runtime, and the necessary root objects, are different. That is, the array elements recorded by the full array and the root array at different times are dynamically changed. And each round of memory recovery recreates a traversal array CheckArray used for the round. Specifically, the snapshot of the full array AllObjs and the root array RootObjs may be obtained when a round of internal memory recovery is started, so as to freeze the array elements of the full array AllObjs and the root array RootObjs to the state when the round of memory recovery is started (i.e., the current time), and obtain the initial traversal array by copying the snapshot of the root array RootObjs.
Specifically, the computer device initializes the traversal array based on the root array RootObjs of the current time and initializes the array pointer ItPtr to point to the first array element of the traversal array when starting a round of memory recovery.
In the embodiment, the traversal array is initialized based on the root array, and the initialization efficiency of the traversal array is improved.
In one embodiment, copying objects in the root array as array elements of the traversal array comprises: removing the permanent objects which do not point to other objects in the root array; adding traversal marks to the permanent objects; and adding the objects in the root array with the permanent objects removed to the traversal array.
In practical applications, there may be some objects that are needed by the target application most of the time. For example, in a game scene, flowers and plants in a virtual scene may need to be displayed on a game page for a long time, and thus the "flowers and plants" are used as root objects and need to be traversed in each round of memory recycling. The persistent object is a root object that the target application needs for a long time and can be predicted in advance that it does not reference other objects. The persistent object may be pre-specified.
Specifically, in order to improve traversal efficiency, in this embodiment, when the traversal array is initialized according to the root array, the permanent objects in the root array are filtered, and in the initial preparation stage of traversal, a traversal mark is added to the filtered permanent objects. The computer device only needs to initialize the traversal array according to the root array which filters out the permanent objects, thereby avoiding the traversal of the permanent objects.
In this embodiment, traversing the traversal array is to add a traversal flag to the root object, and on the other hand, more importantly, to find an object that may be referenced by the root object, the referenced object is also included in the traversal input to add the traversal flag, and for a permanent object, it may be predicted in advance that it does not reference other objects, so that it is only necessary to add the traversal flag to itself in advance to avoid deletion. The traversal efficiency can be improved by filtering the permanent objects, and the memory recovery efficiency is further improved.
In one embodiment, copying objects in the root array as array elements of the traversal array comprises: acquiring a temporary array for recording an object newly created by a target application; copying objects in the root array and the temporary array as array elements of the traversal array; the memory recovery method further comprises: recording the times of adding the objects in the temporary array to the traversal array; and deleting the old objects which are added to the traversal array and the times of which reach the preset value from the temporary array.
In practice, some objects may not belong to the root object, but cannot be deleted, such as a newly created or created object. To ensure that objects that are newly created or are being created are not deleted, the computer device also establishes a temporary array TempRootobjs for recording objects created by the target application at the current time and within a specified time period before. The array elements in the temporary array TempRootobjs are dynamically changed, newly created objects which meet the time regulation are continuously added along with the time, and objects which continuously participate in continuous multi-round memory recovery are continuously removed.
Specifically, the computer device copies array elements in both the root array and the temporary array into the traversal array in the initial preparation phase of traversal, that is, initializes the traversal array based on the root array and the temporary array. The newly created object may or may not belong to the root object. The newly created root object appears in both the root array and the temporary array. Therefore, when the array elements in the root array and the temporary array are initialized and traversed, the duplication removal of the array elements can be preferentially carried out.
When the array elements of the temporary array are added to the traversal array to participate in memory recovery, the times of adding each array element of the temporary array to the traversal array, namely the times of participating in memory recovery, are recorded. And deleting the objects which participate in the memory recovery and have the times reaching the preset value from the temporary array. The preset value can be freely set according to requirements, such as 2, and the like, so that the new object in the TempRootobjs array can be ensured not to be deleted in at least 2 consecutive rounds of memory recovery.
In this embodiment, based on the temporary array, it may be ensured that the newly created or created object is not deleted in the memory recovery process for the specified number of times.
In one embodiment, deleting from the temporary array the old objects added to the traversal array a predetermined number of times comprises: determining the object added to the traversal array with the number reaching a preset value as an old object; determining whether an old object has an object that is directly or indirectly referenced; if so, judging whether the object directly or indirectly referenced by the old object is loaded completely; if so, the old object is deleted from the temporary array.
In actual applications, there may be asynchronously loaded objects. The traversal array is initially established based on the object to be traversed determined by the initial frame for starting the memory recovery of the current round, that is, the traversal array is established based on the root array and the temporary array at the moment of starting the memory recovery of the current round. After the memory reclamation is actually started, i.e., during the memory reclamation, the target application may continue to create new objects, which may be objects that are directly or indirectly applied by objects in the temporary array. Thus, during memory reclamation, the reference relationships of objects in the temporary array to other objects change.
In order to avoid the influence of the change of the reference relationship on the accuracy of the marking result, for the objects asynchronously loaded in the temporary array, the embodiment removes the objects from the temporary array only when the objects are completely loaded. Specifically, for an object whose number of times that has been added to the traversal array reaches a preset value, the temporary array should be removed according to the above principle, but at this time, the computer device determines the object as an old object, and further confirms whether an object asynchronously loaded based on the old object, that is, an object directly or indirectly referenced by the old object, is generated. When the object exists, the computer equipment judges whether the object directly or indirectly referenced by the old object is loaded completely, and deletes the old object from the temporary array only after the loading is completed. And if the object directly or indirectly referenced by the old object is not loaded completely, keeping the old object in a temporary array to prepare for participating in the next round of memory recovery.
In this embodiment, for an asynchronously loaded object, only when it is completely loaded, that is, when its associated child object is also loaded, it is removed from the TempRootObjs, so that the object deletion threshold is increased, and the object deletion error can be effectively reduced.
In one embodiment, adding a traversal marker to the current object pointed to by the array pointer within a preset single reclamation time limit, and adding the pointed object to the traversal array waiting for traversal when the current object points to other objects comprises: when the current object pointed by the group pointer has no traversal mark, adding a traversal mark for the current object; determining whether the current object points to other objects or not according to the reference relation information of the objects; and if so, adding the object pointed by the current object to the last position in the traversal array.
As shown in FIG. 3, after completing the initial preparation of the traversal, the computer device begins traversing the traversal array. And in the stage of traversing the traversal array, the computer equipment mainly performs traversal mark check, traversal mark addition and referred object addition. Specifically, the computer device starts this marking, and traverses the current object pointed to by the array pointer ItPtr in the traversal array. As above, traversing an array is a process in which elements of the array are continually expanded. The newly added array element may be the object that the memory reclamation of this round has already traversed. To avoid repeated traversals, the computer device first checks whether the current object already has a traversal flag. If yes, directly skipping, and starting to traverse the next sequential object.
When the current object does not have the traversal mark, the computer device adds the traversal mark to the current object and confirms whether the current object directly or indirectly applies other objects. The target application provides Meta information, i.e., reference relationship information, of each object based on the rendering engine. The Meta information includes attribute information such as a reference relationship between the object and another object. And the computer equipment judges whether the current object refers to other objects or not according to the Meta information. If yes, the computer device adds a traversal mark to the current object, adds the applied object to the tail of the traversal array to wait for traversal, and points the array pointer ItPtr to the next sequential object in the traversal array. If not, the computer device adds a traversal mark to the current object, points the array pointer ItPtr to the next sequential object in the traversal array, and continues to traverse the next sequential object as the current object according to the logic.
In an embodiment, the memory reclamation method further includes: when the current object pointed by the group pointer has no traversal mark, adding a traversal mark for the current object; determining whether a variable corresponding to the current object is a variable of a target type; and if so, adding all the objects of the target type to the last position in the traversal array.
The object of the target type may specifically be an object of the array type. In the traversing process, the computer equipment also adds all the objects of the target type in the full array AllObjs to the traversing array for traversing.
In this embodiment, the root object in the initial traversal array is used as a leader, potential objects required by the operation of the target application, that is, objects directly or indirectly referenced by the root object, are gradually found, the potential objects are also included in the traversal array for traversal, and a traversal mark is added to ensure that the potential objects are not deleted, so that it can be ensured that the target application normally operates to determine that the required root object and the potential objects which may be required are not deleted, and the operation effect of the target application when the memory is recovered is ensured.
In one embodiment, adding the object pointed to by the current object to the last position in the traversal array comprises: judging whether the reference relationship between the current object and the pointed object belongs to a weak reference relationship or not; if not, adding the object pointed by the current object to the last position in the traversal array.
Wherein, the reference relationship between the objects can be divided into strong reference and weak reference according to the reachability strength. Only the weakly referenced object has a shorter life cycle than the strongly referenced object, and once the weakly referenced object is found in the process of scanning the memory area of the target application by the GC thread, the memory occupied by the weakly referenced object can be recycled regardless of whether the current memory space is enough or not.
Specifically, the computer device filters the referenced objects before appending the objects referenced by the current object to the end of the traversal array. And the computer equipment judges whether the reference relation between the referenced object and the current object is weakly referenced or not according to the Meta information. If so, the computer equipment does not add the weakly referenced object referenced by the current object to the traversal array any more; otherwise, the object referenced by the current object is added to the traversal array.
In the embodiment, before the object referenced by the current object is added to the tail of the traversal array, the referenced object is filtered, the traversal range can be accurately limited, and the traversal efficiency is further improved.
In one embodiment, when the traversal is completed, deleting the object created by the target application up to the current time without the traversal marker comprises: acquiring a full array for recording all objects which are not deleted and are created by the running of the target application until the current time; when the traversal is finished, determining the object without the traversal mark in the full array as a useless object; the garbage object is deleted.
The useless object is an object that is not needed for the target reference to run currently, and may be determined according to the existence of the traversal mark. And adding a traversal mark to the root object and the referenced object in the traversal array according to the traversal stage, wherein the useless object is an object without the traversal mark.
Specifically, as shown in FIG. 3, upon completion of the traversal, the computer device begins to find garbage objects. In the stage of searching the useless objects, the computer equipment acquires the full array ALLobjs and traverses whether each object in the full array ALLobjs has a traversal mark or not. For objects that do not have a traversal marker, the computer device determines it as a garbage object. And in the stage of deleting the useless objects, the computer equipment deletes and destructs the searched objects.
In the embodiment, the useless object can be rapidly checked by means of the full array and the traversal mark, and compared with a mode of traversing program codes of target application, the useless object searching efficiency is greatly improved, and further the memory recovery efficiency is improved.
In one embodiment, deleting the garbage object includes: determining whether the objects determined as the useless objects in the current round of memory recovery are also determined as the useless objects in the previous target round of memory recovery; if yes, the useless objects are deleted.
The "target round + 1" is a preset minimum number of times that an object is continuously determined as a useless object when the object is added to the to-be-deleted queue ToDelObjs, such as 2 or 3. When the target round is 0, it indicates that an object is deleted as long as it is determined to be a useless object in a round of memory recovery. When the target round is 1, it means that an object is deleted only if it is determined as a useless object in two consecutive rounds of memory reclamation. And so on. The queue to be deleted ToDelObjs may be an array initially established in a deletion stage of a round of memory reclamation for recording objects determined to need to be deleted.
Specifically, each round of traversal screens out "garbage objects", and corresponding numerical value accumulation is carried out. An object is determined to be a garbage object for the first time, a garbage count value is generated for the object, and the garbage count value is accumulated as the number of times of determining as a garbage object increases. In the process of traversing the full array, the computer device judges whether the frequency of continuously judging the useless objects as the useless objects reaches a target round +1 according to the useless counting value of the useless objects, namely, whether the objects are also judged as the useless objects in the previous target round memory recovery. If yes, the computer device adds the object to a to-be-deleted queue ToDelObjs to be deleted, and the object is waited to be deleted. If not, the computer device temporarily does not add the object to the to-be-deleted queue ToDelObjs.
In the embodiment, the objects which are judged as useless objects in the memory recovery of the continuous target times can be deleted, so that the problem of object mistaken deletion caused by the change of the reference relation among the objects in the process of marking for multiple times can be well solved while the GC frequency is improved and the memory overhead of the target application is reduced, and the objects required by the normal operation of the target application can not be mistakenly deleted, so that the page display effect and the operation fluency of the target application are well considered.
In one embodiment, deleting objects created by the target application as of the current time that are not deleted without the traversal marker comprises: initializing a queue to be deleted; adding objects without traversal marks which are not deleted after the target application is created until the current time to a queue to be deleted; sorting the objects in the queue to be deleted according to the reference relation information of the objects; executing pre-analysis operation and pre-memory release operation on the objects in the queue to be deleted; and executing the destruct operation on the object which completes the operation before destruct and the operation before memory release according to the sequence.
As shown in fig. 3, after finding the garbage object, i.e. determining the queue to be deleted, the garbage object in the queue to be deleted is deleted. The process of deleting the useless objects can also be completed in multiple times according to the logic of marking in multiple times. Each time a portion of the delete task is completed, the delete garbage stage also has a corresponding longest time-consuming limit (hereinafter referred to as a single delete deadline). The single deletion time limit and the single recovery time limit may be the same or different.
The process of deleting useless objects comprises 4 sub-processes of sorting, operation before deconstruction, operation before release and operation after fiction. As above, there are reference relations between some objects, there are dependency relations between the objects in the deletion order, and it is necessary to wait for another object that depends on to delete first to destruct the object itself. The sequencing task is a task which determines the dependency relationship between the objects according to the Meta information of the objects and then sequences according to the dependency relationship between the objects. The pre-destruct operation is an additional analysis operation which needs to be completed on some objects before the useless objects are really deleted, and can be realized by a PreDestroy function provided by a Spring framework. The pre-release operation is an additional analysis operation which is further completed on the basis of performing the pre-reconstruction operation on some objects, and can be realized by a Destroy function provided by a Spring framework. The destruct operation is a real useless object deletion operation, and the memory occupied by the object is released.
The same sub-process may be completed in multiple times, and the switching of two sub-tasks may also occur within a single recovery time limit. Specifically, the computer device first performs the sequencing task. And if the sequencing of all the objects in the task queue to be deleted is finished within a single deletion time limit, continuously executing the operation before reconstruction on all the objects in the task queue to be deleted within the single deletion time limit. If the deleting task is not completed within a single deleting time limit, continuing to sort within the next single deleting time limit until the next subprocess is switched to be completed, and so on until the deleting is completed.
In the embodiment, before deletion, the objects to be deleted are sorted according to the reference relationship between the objects, so that the influence of mistaken deletion of the objects to be deleted on the normal operation of the target application can be avoided; further, the destruct is performed after the pre-destruct operation and the pre-release operation are performed.
In a specific embodiment, the memory recovery method provided by the present application specifically includes the following processes:
(1) traverse initial preparation
Assuming that the starting time of memory recovery in the current round is T1, acquiring a full quantity array AllObjs of the target application provided by the rendering engine at the time T1, and deleting the traversal mark of the undeleted object created by the target application at the time T1 according to the full quantity array AllObjs. And initializing the traversal array CheckArray, and putting the array elements in the root array RootObjs provided by the rendering engine into the traversal array CheckArray. The initialization array pointer ItPtr points to the first array element of the traversal array CheckArray. The time taken to traverse the initial preparation phase t0 is typically short, requiring no multiple passes or multiple threads. Of course, the process of traversing the initial preparation may be divided into multiple times or multi-threaded.
As shown in fig. 4, the process of traversing the initial preparation specifically includes the following steps:
s402, when the memory recovery condition is met, the target application is paused to run, and the traversal marks of each object which is not deleted after the target application is created until the current time are cleared.
S404, acquiring a root array for recording objects necessary for running the target application at the current time.
S406, the permanent objects in the root array which do not point to other objects are eliminated.
S408, adding a traversal mark for the permanent object.
S410, acquiring a temporary array for recording the newly created object of the target application.
S412, initializing a traversal array, and copying the objects in the root array from which the permanent objects are removed and the objects in the temporary array as array elements of the traversal array.
And S414, recording the times of the objects in the temporary array being added to the traversal array.
And S416, deleting the old objects which are added to the traversal array and reach the preset value from the temporary array.
S418, initializing an array pointer for pointing to different array elements in the traversal array.
S420, the array pointer points to the objects in the first sequence in the traversal array.
(2) Traversing objects
And when the target application finishes updating each frame of video frame, calling a Tick function of the GC thread to mark the time. If the state of the GC thread is in the "traversal object", then the subsequent objects of the CheckArray will be continuously traversed from the position of the array pointer ItPtr of which the last traversal is stopped according to the single recycle implementation, and ItPtr is incremented until the tail of the CheckArray is reached. The Tick function will traverse at least one object at a time. The time spent traversing a single object is typically very low and no jamming occurs. The traversal process of the single object comprises the following steps: A. and checking the traversal mark, and directly skipping the object if the traversal mark exists. B. If the traversal mark is not available, the traversal mark is added, and repeated traversal in the later stage is avoided. C. And traversing all 'object pointer' variables of the object according to the Meta information of the object, and judging whether other objects are pointed. D. If the variable points to other objects, the pointed object has no traversal marker, then the pointed object is added to the CheckArray tail. E. For object array class variables, the objects of the array class are also added to the CheckArray tail.
The traversal process is paused every unit time length of the target application, and the target application executes one point of traversal every pause until the ItPtr reaches the tail of the CheckArray. Since the number of CheckArray elements is also increased gradually during the traversal process, the process of traversing the object takes up most of the time of the whole round of memory recovery, which is also the time of the mortgage of the conventional GC method. The method and the device can evenly finish the process of traversing the object for multiple times, and eliminate the phenomenon of blockage.
As shown in fig. 5, the process of traversing the object specifically includes the following steps:
s502, traversing the current object pointed by the group pointer in a preset single recovery time limit.
S504, when the current object pointed by the group pointer has no traversal mark, adding a traversal mark to the current object.
S506, determining whether the current object points to other objects according to the reference relation information of the objects.
And S508, if yes, judging whether the reference relationship between the current object and the pointed object belongs to a weak reference relationship.
And S510, if the current object does not belong to the weak reference relationship, adding the object pointed by the current object to the last position in the traversal array to wait for traversal, and stopping the traversal until the single recovery time limit is reached or the traversal of the traversal array is finished.
S512, when the traversal is paused but not completed, the target application is paused again after the unit time length of the target application is continuously operated, the array pointer points to the next sequential object in the traversal array, and the step of traversing the current object pointed by the array pointer in the preset single recovery time limit is returned.
(3) Finding objects to be deleted
After the traversal is finished, objects directly or indirectly referenced by the root object are added with traversal marks, and other objects without traversal marks in the full array AllObjs are objects which are not needed in the current round of memory recovery traversal. For the objects which are not needed in the current round, whether the objects are not believed to be used any more is judged, and if the objects are believed to be not used any more, the objects are added into the queue to be deleted ToDelObjs.
In the process of traversing objects, reference relations among the objects may change, and in order to reduce the influence of the reference relation change on the accuracy of adding the traversal marks, the following optimization processing is performed on the object management in the aspect of implementation: A. when creating synchronous or asynchronous objects based on a uniform object creation interface provided by a rendering engine, the created objects are placed into a temporary array TempRootObjs, which is also treated as the same as the root array RootObjs. B. By default, an object added to the temporary array TempRootObjs will maintain at least 2 cycles of object traversal, and will always be used as a root object in this time, and therefore will not be deleted. C. For an asynchronously loaded object, it is only possible to purge it from the TempRootoObjs when it is completely loaded, i.e., its associated child objects are also loaded. D. When assigning the 'object variable', adding the related object into TempRootoObjs
The 'useless objects' screened out by each round of memory recycling have numerical value accumulation, and only when the 'useless' objects are judged to be 'useless' for continuous (target round +1), the 'useless objects' enter the real ToDelObjs. The larger the target round +1 is theoretically the safer. The 'continuous 3 rounds' belongs to empirical values, and theoretically, 3 rounds can be considered that the situation of mistaken deletion is impossible.
As shown in fig. 6, the process of searching for an object to be deleted specifically includes the following steps:
s602, when the traversal is finished, acquiring a full array for recording all objects which are not deleted and are created by the target application running until the current time.
S604, determining the object without the traversal mark in the full array as a useless object.
S606 determines whether or not the object identified as a garbage object in the current round of memory collection is also identified as a garbage object in the previous round of memory collection.
And S608, if yes, deleting the useless objects.
(4) Deleting objects to be deleted
Deleting all objects in the queue to be deleted ToDelObjs, setting time-consuming limit, completing deletion for multiple times, and ensuring that the time consumption does not exceed the set value each time. The deletion process is divided into four sub-processes: A. objects in ToDelObjs are sorted. B. A PreDestroy function is called to the object in ToDelObjs to execute a pre-destruct operation. C. And calling a Destroy function to the object in ToDelObjs to execute a pre-release operation, D and a deletion and reconstruction operation to the object in ToDelObjs.
As shown in fig. 7, the process of deleting the object to be deleted specifically includes the following steps:
s702, initializing a queue to be deleted.
S704, adding the objects without the traversal marks which are not deleted after the target application is created until the current time to the queue to be deleted.
S706, sorting the objects in the queue to be deleted according to the reference relation information of the objects.
S708, executing the operation before the destruct and the operation before the memory release on the object in the queue to be deleted.
And S710, performing the destruct operation on the objects which finish the operation before the destruct operation and the operation before the memory release according to the sequence.
Assuming that the time required for preparation for deleting traversal marks before starting the memory recovery of the current round is t0, the memory recovery of the current round is marked m times in total, and the time required for reconstruction and deletion of objects without traversal marks after completing the memory recovery of the current round is td, the total time Tx of the memory recovery of the current round is t0+ t11+ t12+. + t1m + td. Assuming that the preset unit time length of the target application running is t, and the memory recovery of the current round is deleted n times in total, the duration from the beginning to the end of the memory recovery of the round is t0+ t11+ t12+. + t1m + td + (m + n) × t.
Assuming that the deletion stage is also completed in multiple times, and the single deletion time limit is equal to the single recovery time limit, td is equal to (t1m +1) + (t1m +2) + … + (t1m + n). Assuming that the starting time of memory reclamation in the current round is T1, and the unit time length of the target application operation is T, the operation states of the target application at different times in the memory reclamation process are shown in fig. 8. According to actual test statistics, the memory recovery mechanism provided by the application can realize the GC frequency (memory recovery frequency) of 2-3 seconds per time, while the GC frequency which can be realized by the traditional mode is only 1 minute per time. After one round of memory is finished, the next round of memory recovery may be started immediately, or a period of time may be required to wait until the memory recovery condition is met again, and the next round of memory recovery is started. If the next memory recovery is started immediately, the start time T2 of the next memory recovery is T1+ Tx.
According to the memory recovery method, in the internal recovery marking stage, the target application pauses once per unit time length of operation to complete one-time marking, so that a large marking task in one round of memory recovery is divided into a plurality of small marking tasks to be inserted into the target application to be executed in the operation process, each small marking task has a corresponding single recovery time limit, the required time is very short, meanwhile, the light recovery mechanism improves the memory recovery frequency, the internal expense of the target application is greatly reduced, page refreshing is smoother from the display of a front-end page, and the phenomenon of page blocking is eliminated.
In a specific embodiment, the memory recovery method provided by the application can be applied to a game scene. The current online game generally relates to a large virtual scene map and a long sight distance, the virtual scene has abundant details such as buildings, flowers and plants, and virtual objects also have a large amount of clothes, equipment, sitting and riding, wugong and the like, and meanwhile, the current online game also provides a very gorgeous special effect. The good game experience of the game application benefits from the asynchronous Streaming loading function, and frequent loading and unloading of the block map inevitably causes frequent object creation and destruction. Based on the traditional GC algorithm, all objects are traversed when the loading of a centralized frame of game page is finished, and obvious blockage is caused. Based on the memory recycling scheme provided by the application, the marking process is dispersedly and alternately completed among a plurality of game pages, each game page only needs to pause for a short time, and the loading and unloading of the block maps become unaware. Meanwhile, compared with the traditional GC algorithm, the memory recovery scheme provided by the application improves the GC frequency by dozens of times, so that the memory of the game can be kept at a very low level by improving the GC frequency, and the memory recovery scheme can support the operation on computer equipment based on a 32-bit windows operating system.
Fig. 2, 4, 5, 6, and 7 are schematic flow charts illustrating a memory reclamation method according to an embodiment. It should be understood that although the steps in the flowcharts of fig. 2, 4, 5, 6, and 7 are shown in order as indicated by the arrows, the steps are not necessarily performed in order as indicated by the arrows. The steps are not performed in the exact order shown and described, and may be performed in other orders, unless explicitly stated otherwise. Moreover, at least some of the steps in fig. 2, 4, 5, 6, and 7 may include multiple sub-steps or multiple stages, which are not necessarily performed at the same time, but may be performed at different times, and the order of performing the sub-steps or stages is not necessarily sequential, but may be performed alternately or alternatingly with other steps or at least some of the sub-steps or stages of other steps.
In one embodiment, as shown in fig. 9, a memory reclamation apparatus 900 is provided, the apparatus comprising a preparation module 902, a marking module 904, and a deletion module 906, wherein:
a preparing module 902, configured to suspend running the target application when the memory recovery condition is met, and clear the traversal flag of each object that is not deleted until the target application is created at the current time.
A marking module 904, configured to sequentially traverse, starting from the position of the target sequential objects, whether each object is an object necessary for the target application to run, add a traversal mark to the traversed necessary object, and suspend traversal when a preset single recovery time limit is reached or traversal is completed; and when the traversal is paused but not completed, continuing to operate the target application for unit time, pausing the operation again, determining the position of the last traversal pause as the position of the target sequential objects, and returning to the step of sequentially traversing whether each object is the object necessary for the operation of the target application from the position of the target sequential objects.
And a deleting module 906, configured to delete, when traversal is completed, an object without a traversal flag that is not deleted until the current time after the target application is created, and continue to run the target application to wait for next memory recovery.
In one embodiment, the preparation module 902 is also used to establish a traversal array and corresponding array pointers based on the objects necessary for the target application to run; pointing an array pointer to an object traversing a first sequence in an array; the marking module 904 is further configured to add a traversal mark to the current object pointed by the array pointer within a preset single recycling time limit, and add the pointed object to a traversal array to wait for traversal when the current object points to another object; and pointing the array pointer to the next object in the traversal array to continue traversing until the single recovery time limit reaches or the traversal of the traversal array is finished, and stopping the traversal.
In one embodiment, the preparation module 902 is further configured to obtain a root array for recording objects necessary for the target application to run at the current time; initializing a traversal array, and copying an object in the root array as an array element of the traversal array; an array pointer is initialized for pointing to different array elements in the traversal array.
In one embodiment, preparation module 902 is further configured to eliminate persistent objects in the root array that do not point to other objects; adding traversal marks to the permanent objects; and adding the objects in the root array with the permanent objects removed to the traversal array.
In one embodiment, as shown in fig. 10, the marking module 904 includes an object traversal module 9042, configured to add a traversal mark to the current object when the current object pointed by the group pointer has no traversal mark; determining whether the current object points to other objects or not according to the reference relation information of the objects; and if so, adding the object pointed by the current object to the last position in the traversal array.
In one embodiment, the object traversing module 9042 is further configured to determine whether a reference relationship between the current object and the pointed-to object belongs to a weak reference relationship; if not, adding the object pointed by the current object to the last position in the traversal array.
In one embodiment, the preparation module 902 is further configured to obtain a temporary array for recording newly created objects of the target application; copying objects in the root array and the temporary array as array elements of the traversal array; the object traversal module 9042 is further configured to record the number of times that an object in the temporary array is added to the traversal array; and deleting the old objects which are added to the traversal array and the times of which reach the preset value from the temporary array.
In one embodiment, the object traversal module 9042 is further configured to determine, as the old object, an object whose number of times added to the traversal array reaches a preset value; determining whether an old object has an object that is directly or indirectly referenced; if so, judging whether the object directly or indirectly referenced by the old object is loaded completely; if so, the old object is deleted from the temporary array.
In one embodiment, the target application is used to play a video; the video comprises a plurality of video frames; the preparation module 902 is further configured to pause playing the video when the playing of the current sequential video frame is completed when the memory recovery condition is met; the object traversal module 9042 is further configured to continue to play the next sequential video frame when traversal is suspended but traversal is not completed; and when the next sequential video frame is played, pausing the playing of the video.
In one embodiment, as shown in fig. 10, the marking module 904 further includes an object to be deleted searching module 9044, configured to obtain a full array for recording all objects that are not deleted and are created by the target application running until the current time; when the traversal is finished, determining the object without the traversal mark in the full array as a useless object; the garbage object is deleted.
In an embodiment, the to-be-deleted object searching module 9044 is further configured to determine whether an object determined to be a garbage object in the current round of memory reclamation is also determined to be a garbage object in the previous target round of memory reclamation; if yes, the useless objects are deleted.
In one embodiment, the delete module 906 is further configured to initialize a queue to be deleted; adding objects without traversal marks which are not deleted after the target application is created until the current time to a queue to be deleted; sorting the objects in the queue to be deleted according to the reference relation information of the objects; executing pre-analysis operation and pre-memory release operation on the objects in the queue to be deleted; and executing the destruct operation on the object which completes the operation before destruct and the operation before memory release according to the sequence.
According to the memory recovery device, in the internal recovery marking stage, the target application pauses once per unit time length of operation to complete one marking, so that a large marking task in one round of memory recovery is divided into a plurality of small marking tasks to be inserted into the target application operation process to be executed, each small marking task has a corresponding single recovery time limit, the required time is very short, meanwhile, the light recovery mechanism improves the memory recovery frequency, the internal expense of the target application is greatly reduced, page refreshing is more smooth from the front-end page display, and the phenomenon of page blocking is eliminated.
FIG. 11 is a diagram illustrating an internal structure of a computer device in one embodiment. The computer device may specifically be the terminal 110 in fig. 1. As shown in fig. 11, the computer apparatus includes a processor, a memory, a network interface, an input device, and a display screen connected through a system bus. Wherein the memory includes a non-volatile storage medium and an internal memory. The non-volatile storage medium of the computer device stores an operating system and may also store a computer program that, when executed by the processor, causes the processor to implement the memory reclamation method. The internal memory may also have a computer program stored therein, which when executed by the processor, causes the processor to perform the memory reclamation method. The display screen of the computer equipment can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer equipment can be a touch layer covered on the display screen, a key, a track ball or a touch pad arranged on the shell of the computer equipment, an external keyboard, a touch pad or a mouse and the like.
Fig. 12 is a diagram showing an internal structure of a computer device in another embodiment. The computer device may specifically be the server 120 in fig. 1. As shown in fig. 12, the computer device includes a processor, a memory, and a network interface connected by a system bus. Wherein the memory includes a non-volatile storage medium and an internal memory. The non-volatile storage medium of the computer device stores an operating system and may also store a computer program that, when executed by the processor, causes the processor to implement the memory reclamation method. The internal memory may also have a computer program stored therein, which when executed by the processor, causes the processor to perform the memory reclamation method.
Those skilled in the art will appreciate that the configurations shown in fig. 11 and 12 are merely block diagrams of portions of configurations related to aspects of the present application, and do not constitute limitations on the computing devices to which aspects of the present application may be applied, as particular computing devices may include more or less components than shown, or combine certain components, or have a different arrangement of components.
In one embodiment, the memory recycling apparatus provided in the present application may be implemented in a form of a computer program, and the computer program may be run on a computer device as shown in fig. 11 or fig. 12.
In one embodiment, a computer device is provided, comprising a memory and a processor, the memory storing a computer program that, when executed by the processor, causes the processor to perform the steps of the memory reclamation method described above. The steps of the memory reclamation method may be the steps in the memory reclamation methods in the embodiments described above.
In one embodiment, a computer-readable storage medium is provided, which stores a computer program that, when executed by a processor, causes the processor to perform the steps of the memory reclamation method described above. The steps of the memory reclamation method may be the steps in the memory reclamation methods in the embodiments described above.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by hardware related to instructions of a computer program, and the program can be stored in a non-volatile computer readable storage medium, and when executed, can include the processes of the embodiments of the methods described above. Any reference to memory, storage, database, or other medium used in the embodiments provided herein may include non-volatile and/or volatile memory, among others. Non-volatile memory can include read-only memory (ROM), Programmable ROM (PROM), Electrically Programmable ROM (EPROM), Electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDRSDRAM), Enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus Direct RAM (RDRAM), direct bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
The technical features of the above embodiments can be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the above embodiments are not described, but should be considered as the scope of the present specification as long as there is no contradiction between the combinations of the technical features.
The above examples only express several embodiments of the present application, and the description thereof is more specific and detailed, but not construed as limiting the scope of the present application. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the concept of the present application, which falls within the scope of protection of the present application. Therefore, the protection scope of the present patent shall be subject to the appended claims.

Claims (26)

1. A memory reclamation method, comprising:
when the memory recovery condition is met, the target application is paused to run, and the traversal marks of each object which is not deleted after the target application is created until the current time are cleared;
establishing a traversal array and a corresponding array pointer based on the objects required by the running of the target application, and pointing the array pointer to the objects in the first sequence in the traversal array;
traversing the current object pointed by the array pointer within a preset single recovery time limit, adding a traversal mark to the current object, and stopping traversal when the preset single recovery time limit is reached or the traversal is finished;
when the traversal is suspended but the traversal is not completed, continuing to operate the target application for unit time length and then suspending the operation again, enabling the array pointer to point to the next sequence of objects in the traversal array, and returning to the step of traversing the current object pointed by the array pointer within the preset single recovery time limit;
and when the traversal is finished, deleting the objects which are not deleted after the target application is created until the current time and have no traversal mark, and continuously operating the target application to wait for next memory recovery.
2. The method of claim 1, further comprising:
traversing the current object pointed by the array pointer in the preset single recovery time limit, adding a traversal mark to the current object, and stopping the traversal when the preset single recovery time limit reaches or finishes the traversal comprises:
adding a traversal mark to the current object pointed by the array pointer within a preset single recovery time limit, and adding the pointed object to a traversal array to wait for traversal when the current object points to other objects;
and pointing the array pointer to the next object in the traversal array to continue traversing until the single recovery time limit is reached or the traversal of the traversal array is finished, and stopping the traversal.
3. The method of claim 1, wherein the building a traversal array and corresponding array pointers based on the objects necessary for the target application to run comprises:
acquiring a root array used for recording objects necessary for running the target application at the current time;
initializing a traversal array, and copying an object in the root array as an array element of the traversal array;
initializing an array pointer for pointing to a different array element in the traversal array.
4. The method of claim 3, wherein copying the objects in the root array as array elements of a traversal array comprises:
removing permanent objects which do not point to other objects in the root array;
adding a traversal mark for the permanent object;
adding the objects in the root array with the permanent objects removed to the traversal array.
5. The method of claim 2, wherein adding a traversal marker to the current object pointed to by the array pointer within a preset single recycling time limit, and adding the pointed object to a traversal array waiting for traversal when the current object points to other objects comprises:
when the current object pointed by the group pointer has no traversal mark, adding a traversal mark for the current object;
determining whether the current object points to other objects or not according to the reference relation information of the objects;
and if so, adding the object pointed by the current object to the last position in the traversal array.
6. The method of claim 5, wherein adding the object pointed to by the current object to the last position in the traversal array comprises:
judging whether the reference relationship between the current object and the pointed object belongs to a weak reference relationship or not;
if not, adding the object pointed by the current object to the last position in the traversal array.
7. The method of claim 3, wherein copying the objects in the root array as array elements of a traversal array comprises:
acquiring a temporary array for recording the newly created object of the target application;
copying the objects in the root array and the temporary array as array elements of a traversal array;
the method further comprises the following steps: recording the number of times that the objects in the temporary array are added to the traversal array;
and deleting the old objects which are added to the traversal array and the times of which reach the preset value from the temporary array.
8. The method of claim 7, wherein deleting the old objects from the temporary array that have been added to the traversal array a predetermined number of times comprises:
determining the object added to the traversal array with the number of times reaching a preset value as an old object;
determining whether an object directly or indirectly referenced by the old object exists;
if so, judging whether the object directly or indirectly referred by the old object is loaded completely;
and if so, deleting the old object from the temporary array.
9. The method of claim 1, wherein the target application is configured to play a video; the video comprises a plurality of video frames; when the memory recovery condition is met, the suspending the running of the target application comprises the following steps:
when the memory recovery condition is met, pausing playing the video when the playing of the current sequence video frame is finished;
when the traversal is paused but the traversal is not completed, the step of pausing the operation again after the target application is continuously operated for the unit time comprises the following steps:
when the traversal is paused but not completed, the next sequential video frame is continuously played;
and when the playing of the next sequential video frame is finished, pausing the playing of the video.
10. The method of claim 1, wherein deleting objects created by the target application without the traversal marker up to the current time when the traversal is completed comprises:
acquiring a total array for recording all objects which are created by the operation of the target application at the current time and are not deleted;
when the traversal is finished, determining the object without the traversal mark in the full array as a useless object;
and deleting the useless objects.
11. The method of claim 10, wherein the deleting garbage objects comprises:
determining whether the objects determined as the useless objects in the current round of memory recovery are also determined as the useless objects in the previous target round of memory recovery;
and if so, deleting the useless objects.
12. The method of claim 1, wherein deleting objects created by the target application until the current time without the traversal marker comprises:
initializing a queue to be deleted;
adding objects without traversal marks which are not deleted after the target application is created until the current time to a queue to be deleted;
sequencing the objects in the queue to be deleted according to the reference relation information of the objects;
executing pre-construction operation and pre-memory release operation on the objects in the queue to be deleted;
and executing the destruct operation on the object which completes the operation before destruct and the operation before memory release according to the sequence.
13. A memory reclamation apparatus, the apparatus comprising:
the preparation module is used for suspending the running of the target application when the memory recovery condition is met, and clearing the traversal marks of each object which is not deleted after the target application is created until the current time;
the marking module is used for establishing a traversal array and a corresponding array pointer based on the object required by the running of the target application, and pointing the array pointer to the object in the first sequence in the traversal array; traversing the current object pointed by the array pointer within a preset single recovery time limit, adding a traversal mark to the current object, and stopping traversal when the preset single recovery time limit is reached or the traversal is finished; when the traversal is suspended but the traversal is not completed, continuing to operate the target application for unit time length and then suspending the operation again, enabling the array pointer to point to the next sequence of objects in the traversal array, returning to the preset single recovery time limit, and traversing the current object pointed by the array pointer;
and the deleting module is used for deleting the objects which are not deleted after the target application is created at the current time and have no traversal mark when the traversal is finished, and continuously operating the target application to wait for the next memory recovery.
14. The apparatus according to claim 13, wherein the marking module is further configured to add a traversal mark to the current object pointed by the array pointer within a preset single recycling time limit, and add the pointed object to a traversal array to wait for traversal when the current object points to other objects; and pointing the array pointer to the next object in the traversal array to continue traversing until the single recovery time limit is reached or the traversal of the traversal array is finished, and stopping the traversal.
15. The apparatus of claim 13, wherein the preparation module is further configured to obtain a root set for recording objects necessary for the target application to run at a current time; initializing a traversal array, and copying an object in the root array as an array element of the traversal array; initializing an array pointer for pointing to a different array element in the traversal array.
16. The apparatus of claim 15, wherein the preparation module is further configured to cull persistent objects in the root array that do not point to other objects; adding a traversal mark for the permanent object; adding the objects in the root array with the permanent objects removed to the traversal array.
17. The apparatus according to claim 14, wherein the marking module comprises an object traversal module configured to add a traversal mark to the current object pointed by the array pointer when the current object has no traversal mark; determining whether the current object points to other objects or not according to the reference relation information of the objects; and if so, adding the object pointed by the current object to the last position in the traversal array.
18. The apparatus of claim 17, wherein the object traversing module is further configured to determine whether the reference relationship between the current object and the pointed-to object belongs to a weak reference relationship; if not, adding the object pointed by the current object to the last position in the traversal array.
19. The apparatus of claim 15, wherein the preparation module is further configured to obtain a temporary array for recording newly created objects of the target application; copying the objects in the root array and the temporary array as array elements of a traversal array; the marking module comprises an object traversing module, and the object traversing module is further used for recording the times of adding the objects to the traversed array in the temporary array; and deleting the old objects which are added to the traversal array and the times of which reach the preset value from the temporary array.
20. The apparatus of claim 19, wherein the object traversal module is further configured to determine an object added to the traversal array with a number of times reaching a preset value as an old object; determining whether an object directly or indirectly referenced by the old object exists; if so, judging whether the object directly or indirectly referred by the old object is loaded completely; and if so, deleting the old object from the temporary array.
21. The apparatus of claim 13, wherein the target application is configured to play a video; the video comprises a plurality of video frames; the preparation module is also used for pausing the playing of the video when the playing of the current sequence video frame is finished when the memory recovery condition is met; the marking module comprises an object traversing module, and the object traversing module is also used for continuously playing the next sequential video frame when the traversal is suspended but the traversal is not finished; and when the playing of the next sequential video frame is finished, pausing the playing of the video.
22. The apparatus according to claim 13, wherein the marking module further comprises an object to be deleted searching module, configured to obtain a full array for recording all objects that are not deleted and are created by running the target application until the current time; when the traversal is finished, determining the object without the traversal mark in the full array as a useless object; and deleting the useless objects.
23. The apparatus according to claim 22, wherein the to-be-deleted object searching module is further configured to determine whether an object determined to be a garbage object in the current round of memory reclamation is also determined to be a garbage object in a previous target round of memory reclamation; and if so, deleting the useless objects.
24. The apparatus of claim 13, wherein the delete module is further configured to initialize a queue to be deleted; adding objects without traversal marks which are not deleted after the target application is created until the current time to a queue to be deleted; sequencing the objects in the queue to be deleted according to the reference relation information of the objects; executing pre-construction operation and pre-memory release operation on the objects in the queue to be deleted; and executing the destruct operation on the object which completes the operation before the destruct operation and the operation before the memory release according to the sequence.
25. A computer-readable storage medium, storing a computer program which, when executed by a processor, causes the processor to perform the steps of the method according to any one of claims 1 to 12.
26. A computer device comprising a memory and a processor, the memory storing a computer program that, when executed by the processor, causes the processor to perform the steps of the method of any one of claims 1 to 12.
CN202010120300.9A 2020-02-26 2020-02-26 Memory recovery method and device, storage medium and computer equipment Active CN111258926B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010120300.9A CN111258926B (en) 2020-02-26 2020-02-26 Memory recovery method and device, storage medium and computer equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010120300.9A CN111258926B (en) 2020-02-26 2020-02-26 Memory recovery method and device, storage medium and computer equipment

Publications (2)

Publication Number Publication Date
CN111258926A CN111258926A (en) 2020-06-09
CN111258926B true CN111258926B (en) 2022-06-10

Family

ID=70954628

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010120300.9A Active CN111258926B (en) 2020-02-26 2020-02-26 Memory recovery method and device, storage medium and computer equipment

Country Status (1)

Country Link
CN (1) CN111258926B (en)

Families Citing this family (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112463626B (en) * 2020-12-10 2023-07-11 网易(杭州)网络有限公司 Memory leakage positioning method and device, computer equipment and storage medium
CN115982060B (en) * 2021-10-14 2024-07-05 华为技术有限公司 Memory recycling method and related device
CN113986559B (en) * 2021-12-24 2022-06-24 荣耀终端有限公司 Memory management method and related device
CN117234701B (en) * 2023-07-31 2024-06-07 上海数禾信息科技有限公司 Policy iteration method, device, computer equipment and storage medium
CN116737670B (en) * 2023-08-11 2023-11-17 英诺达(成都)电子科技有限公司 Method, device, equipment and storage medium for deleting UPF file
CN117648200A (en) * 2024-01-29 2024-03-05 龙芯中科技术股份有限公司 Memory management method, memory management device, electronic equipment and readable storage medium

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101046755A (en) * 2006-03-28 2007-10-03 郭明南 System and method of computer automatic memory management
CN106550408A (en) * 2015-09-23 2017-03-29 周祥宇 A kind of data object integration method based on MANET
CN110140117A (en) * 2017-11-10 2019-08-16 华为技术有限公司 Terminal is based on the automatic cleaning individual subscriber file of user's habit
CN110597738A (en) * 2019-09-19 2019-12-20 浪潮软件集团有限公司 Memory release method, terminal and computer readable storage medium

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103297272B (en) * 2013-05-29 2016-12-07 华为软件技术有限公司 Apparatus bound, solution binding method and apparatus buffering recovery pond entity device
CN105446814A (en) * 2014-09-30 2016-03-30 青岛海信移动通信技术股份有限公司 Cache recovery method and device
CN106649130A (en) * 2016-12-28 2017-05-10 乐蜜科技有限公司 Memory space recovery method and apparatus, and electronic device

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101046755A (en) * 2006-03-28 2007-10-03 郭明南 System and method of computer automatic memory management
CN106550408A (en) * 2015-09-23 2017-03-29 周祥宇 A kind of data object integration method based on MANET
CN110140117A (en) * 2017-11-10 2019-08-16 华为技术有限公司 Terminal is based on the automatic cleaning individual subscriber file of user's habit
CN110597738A (en) * 2019-09-19 2019-12-20 浪潮软件集团有限公司 Memory release method, terminal and computer readable storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
浅谈V8引擎中的垃圾回收机制;天镶;《https://segmentfault.com/a/1190000000440270》;20140320;第1-9页 *

Also Published As

Publication number Publication date
CN111258926A (en) 2020-06-09

Similar Documents

Publication Publication Date Title
CN111258926B (en) Memory recovery method and device, storage medium and computer equipment
US7117294B1 (en) Method and system for archiving and compacting data in a data storage array
KR100715638B1 (en) Memory reclamation method
US20130227127A1 (en) Schedule management method and schedule management server
US9715409B2 (en) Job delay detection method and information processing apparatus
US7536512B2 (en) Method and apparatus for space efficient identification of candidate objects for eviction from a large cache
CN105320567A (en) Deferred destruction for efficient resource reclamation
KR100442991B1 (en) Searching device using moving picture index descripter and method thereof
CN106339496A (en) Method and device for clearing photos
US7991976B2 (en) Permanent pool memory management method and system
CN112612537A (en) Configuration data caching method, device, equipment and storage medium
JP2004295889A (en) Method and device for controlling execution of processing task within data processing system
CN116976457A (en) Model loading method, reasoning system, device and computer equipment
CN115934276A (en) Memory management method and device and electronic equipment
US20100049747A1 (en) Apparatus and method for storing log in a thread oriented logging system
CN111221468A (en) Storage block data deleting method and device, electronic equipment and cloud storage system
CN116033102A (en) Video storage method and video playing method
CN115422231A (en) Data page processing method and device, electronic equipment and medium
CN115934999A (en) Video stream data storage method, device and medium based on block file
CN114595066A (en) Reserved memory processing method and device, electronic equipment and medium
CN115203130A (en) File cleaning method, device, equipment and medium for distributed file system
CN113590277A (en) Task state switching method and device and electronic system
CN114595262A (en) Method and system for quickly generating municipal road EPC project bid list
CN112749313A (en) Label labeling method and device, computer equipment and storage medium
CN116775506B (en) Memory recycling method, device, equipment and medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
REG Reference to a national code

Ref country code: HK

Ref legal event code: DE

Ref document number: 40024161

Country of ref document: HK

GR01 Patent grant
GR01 Patent grant