CN112306894A - Object reference method, device, equipment and storage medium - Google Patents

Object reference method, device, equipment and storage medium Download PDF

Info

Publication number
CN112306894A
CN112306894A CN201910690893.XA CN201910690893A CN112306894A CN 112306894 A CN112306894 A CN 112306894A CN 201910690893 A CN201910690893 A CN 201910690893A CN 112306894 A CN112306894 A CN 112306894A
Authority
CN
China
Prior art keywords
referenced
referred
released
objects
memory
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.)
Pending
Application number
CN201910690893.XA
Other languages
Chinese (zh)
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 CN201910690893.XA priority Critical patent/CN112306894A/en
Publication of CN112306894A publication Critical patent/CN112306894A/en
Pending legal-status Critical Current

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
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/455Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
    • G06F9/45504Abstract machines for programme code execution, e.g. Java virtual machine [JVM], interpreters, emulators

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)

Abstract

The embodiment of the application provides an object reference method, an object reference device and a storage medium, wherein the object reference method comprises the following steps: acquiring at least one referred object referred by a reference object of a program running on a virtual machine to obtain a referred object set; and when the memory occupied by the referred object set reaches a threshold value, releasing the referred objects in the referred object set in sequence according to the sequence of the priority of the referred objects in the referred object set from low to high. By the method and the device, memory overflow caused by memory occupation of the referenced object can be prevented.

Description

Object reference method, device, equipment and storage medium
Technical Field
The embodiment of the application relates to the technical field of computers, and relates to but is not limited to an object reference method, an object reference device, object reference equipment and a storage medium.
Background
Currently, a Java Virtual Machine (JVM) has four reference ways: strong references, soft references, weak references, and virtual references.
The object associated with the strong reference is not released and can not be recycled by the JVM all the time, and belongs to unrecoverable resources; the object associated with the soft reference is not recycled if the memory space is enough, but is recycled if the memory space is insufficient; the object associated with the weak reference is recycled no matter whether the memory is sufficient or not; the object associated with the virtual reference may be reclaimed at any time.
However, in the case of a strong reference, since the object associated with the strong reference is not recycled by the JVM all the time, if the object associated with the strong reference reaches the upper Memory limit, an Out of Memory (OOM) is directly caused, and the program crashes. In the other three referencing cases, since the referenced object is released at the upper limit of the memory or at the time of a recycling opportunity, that is, all the objects are not expected to be released, the object may be released already when the program uses the referencing method again, so that the null pointer of the program is abnormally crashed or cannot be executed according to the original logic.
Disclosure of Invention
The embodiment of the application provides an object reference method, device, equipment and storage medium, which can orderly release referenced objects and prevent memory overflow caused by memory occupied by the referenced objects.
The technical scheme of the embodiment of the application is realized as follows:
the embodiment of the application provides an object reference method, which comprises the following steps:
acquiring at least one referred object referred by a reference object of a program running on a virtual machine to obtain a referred object set;
and when the memory occupied by the referred object set reaches a threshold value, releasing the referred objects in the referred object set in sequence according to the sequence of the priority of the referred objects in the referred object set from low to high.
An embodiment of the present application provides an object referencing apparatus, including:
the acquisition module is used for acquiring at least one referred object referred by a reference object of a program running on the virtual machine to obtain a referred object set;
and the releasing module is used for sequentially releasing the referenced objects in the referenced object set according to the sequence of the priorities of the referenced objects in the referenced object set from low to high when the memory occupied by the referenced object set reaches a threshold value.
An embodiment of the present application provides an object referencing apparatus, including:
a memory for storing executable instructions; and the processor is used for realizing the object reference method when executing the executable instructions stored in the memory.
The embodiment of the application provides a storage medium, which stores executable instructions and is used for causing a processor to implement the object reference method when the processor executes the executable instructions.
The embodiment of the application has the following beneficial effects:
when the memory occupied by the referenced object set reaches a threshold value, the referenced objects in the referenced object set are released in sequence according to the sequence of the priorities of the referenced objects in the referenced object set from low to high, so that the referenced objects can be released in order, all the referenced objects cannot be released at one time, the referenced objects cannot be stored all the time and cannot be released, and memory overflow caused by the memory occupied by the referenced objects can be prevented.
Drawings
Fig. 1 is a schematic view of an application scenario of an object referencing method provided in an embodiment of the present application;
FIG. 2 is a schematic diagram of a physical machine according to an embodiment of the present disclosure;
FIG. 3 is an alternative flow chart diagram of an object referencing method provided in the embodiments of the present application;
FIG. 4 is an alternative flow chart diagram of an object referencing method provided in the embodiments of the present application;
FIG. 5 is an alternative flow chart diagram of an object referencing method provided in the embodiments of the present application;
FIG. 6A is a schematic flow chart of an alternative object referencing method provided in the embodiments of the present application;
FIG. 6B is a schematic flow chart of an alternative object referencing method according to an embodiment of the present disclosure;
FIG. 7 is a schematic structural diagram of an intelligent citation device provided in an embodiment of the present application;
FIG. 8 is a schematic flow chart illustrating an implementation of an intelligent citation method provided by an embodiment of the present application;
fig. 9 is a schematic flow chart of implementing recovery of an object in the intelligent citation method provided by the embodiment of the application.
Detailed Description
In order to make the objectives, technical solutions and advantages of the present application clearer, the present application will be described in further detail with reference to the attached drawings, the described embodiments should not be considered as limiting the present application, and all other embodiments obtained by a person of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.
In the following description, reference is made to "some embodiments" which describe a subset of all possible embodiments, but it is understood that "some embodiments" may be the same subset or different subsets of all possible embodiments, and may be combined with each other without conflict.
Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which the embodiments of the present application belong. The terminology used in the embodiments of the present application is for the purpose of describing the embodiments of the present application only and is not intended to be limiting of the present application.
Before further detailed description of the embodiments of the present application, terms and expressions referred to in the embodiments of the present application will be described, and the terms and expressions referred to in the embodiments of the present application will be used for the following explanation.
1) Reference is the process of assigning an object to a variable. Referring to references, what is first thought of is an assignment statement, i.e., a variable points to an object, which is the most common way of referencing. In the memory management and memory recycling process of Java, a programmer does not need to perform plug-in, and as for the garbage recycler process, whether an object is referenced or not can be determined through a reachability analysis algorithm, and if the object is not referenced, the object is marked by a Java Virtual Machine (JVM), so that the object is highly likely to be recycled.
2) The Java virtual machine is an abstract computer, which is a virtual computer capable of running Java code, and is realized by simulating various computer functions on an actual computer. The JVM has its own complete hardware architecture, such as processor, stack, register, etc., and has a corresponding instruction system.
3) A Garbage Collector (GC), which is a process in a Java virtual machine, is an object destruction tool used most frequently in a Java platform. The garbage collector can detect the running condition of the Java application program in the whole process. When some objects are found to be garbage, the garbage collector destroys the objects and releases the memory space occupied by the objects. That is to say, the garbage collector checks whether a certain object is referred by other objects at regular time, and when the certain object is not referred by other objects at a certain time, the object can be collected at any time, so as to clean the memory occupied by the object.
In order to better understand the object referencing method provided in the embodiments of the present application, an analysis description of the object referencing method in the related art is first described below.
In Java technology, JVM introduces four referencing methods, starting from version 1.2, including: strong references, soft references, weak references, and virtual references. The levels of the four reference modes are from high to low: strong references > soft references > weak references > virtual references.
The strong reference refers to creating an object and assigning the object to a reference variable, so the object can be called a strong reference object, the strong reference is a reference which cannot be recycled by the JVM all the time without releasing the object, the strong reference object belongs to an unrecoverable resource, and the garbage collector never recycles the strong reference object. When the Memory space is insufficient, the JVM prefers to throw Out an Out Of Memory Error (Out Of Memory Error), so that the program is terminated abnormally, and the problem Of insufficient Memory can not be solved by recovering a strong reference object.
The nature of soft references is of an optional nature, and for soft reference objects, if the memory space is sufficient at the moment, the garbage collector cannot recycle the objects, and if the memory space is insufficient, the memory of the objects is recycled. The soft reference object can be used by the program as long as the garbage collector does not reclaim the soft reference object. The GC mechanism will clear the soft reference object before ensuring that the virtual machine throws Out the Out Of Memory Error, and at the same time, if there is a reference queue reference, the soft reference object will be added to the queue. If an object has only soft references, it will be reclaimed when there is insufficient space, and the object can be used to build a cache for sensitive data, e.g., a web page cache, a picture cache, etc. The soft references may be used with a reference queue, and when objects referenced by the soft references are reclaimed, the soft references are added to the reference queue.
The nature of the weak reference is also an optional state. If a reference object is a weakly referenced object, once the garbage collector determines that the object is a weakly referenced object during execution, the GC mechanism is executed regardless of memory overflow, and if a reference queue references the weakly referenced object, the weakly referenced object is enqueued. That is, the weak references are used to describe the non-essential objects, and when the JVM performs garbage collection, the objects associated with the weak references are collected regardless of the amount of memory. Weak references may be used to build a cache of sensitive data, for example, in a scenario where the lifecycle is shorter and memory is more sensitive.
It should be noted that the difference between the weak reference and the soft reference is that the weak reference object has a shorter life cycle, and as long as the garbage collector scans it, the memory of the weak reference object is recycled regardless of whether the memory space is sufficient or not.
When the object referenced by the virtual reference belongs to the virtual reachable object, if a reference queue exists, the virtual reference object is added into the queue, but the virtual reference is characterized in that the life cycle of the virtual referenced object is not influenced. The dummy reference is different from the previous soft reference, the weak reference, and it does not affect the life cycle of the object. If an object is associated with a virtual reference, the virtual reference object may be reclaimed by the garbage collector at any time as if no reference is associated with it. That is, if an object has a virtual reference, it may be reclaimed at any time. It should be noted that the virtual references must be used in conjunction with the reference queue, and when the referenced object is recycled, the virtual references are added to the reference queue, which is mainly used to track the garbage recycling process.
At present, various reference modes of the JVM cannot avoid the problem of memory leakage caused by negligence of developers, once some strong references are not reasonably released, OOM is easy to occur on mobile equipment with a small memory when Java memory is continuously increased, and program crash is caused.
Then, for the JVM virtual machine, four references: strong references, weak references, soft references, and virtual references, where a strong reference is a reference that is not always recycled by the JVM without releasing, and a strong reference object associated with such a reference directly causes the OOM if it reaches the upper memory limit, and the program crashes. For other references, the object in the memory is cleared when the upper limit or the recycle timing arrives, but such unexpected clearing timing may cause the object associated with the reference to be already released when the program reuses the reference, resulting in the null pointer of the program being abnormally crashed or unable to be executed according to the original logic.
In order to solve the defects of several reference modes in the related art, embodiments of the present application provide an object reference method, apparatus, device, and storage medium, which can intelligently release some referenced objects with low priority when the memory reaches the upper limit by introducing an intelligent reference mode, and simultaneously ensure that the referenced objects are regenerated in the subsequent use, so that a developer can avoid the problem of memory leakage, and simplify the requirement of the developer on code memory management.
An exemplary application of the object referencing device provided in the embodiment of the present application is described below, and the device provided in the embodiment of the present application may be implemented as a control program in a physical machine. In the following, an exemplary application will be explained when the apparatus is implemented as a control program in a physical machine.
Referring to fig. 1, fig. 1 is a schematic view of an application scenario of the object referencing method provided in the embodiment of the present application, where a developer 101 performs program development through a development environment 121 provided by a virtual machine 102 (for example, a Java virtual machine), tests the developed program through a test environment 122 provided by the virtual machine 102, and submits the developed program to an online environment 123 of the virtual machine 102 for execution. The virtual machine 102 may run on the physical machine 103 as a process of the physical machine 103, so as to implement running the program through the virtual machine 102 on the physical machine 103, and present a running result of the program to a user 105 (an example shows a user 105-1 and a user 105-2) on a client 104 (an example shows a client 104-1 and a client 104-2) connected to the physical machine 103, so as to implement contacting the developed APP with the user.
The physical machine 103 further includes an object referencing device 106 for implementing the method provided in the embodiment of the present application, where the object referencing device 106 may be a control program, or the object referencing device may also be software in the form of a program, a plug-in, and the like, and the object referencing device is parallel to the virtual machine and is capable of acquiring at least one referenced object referenced by a referencing object of a program running on the virtual machine to obtain a referenced object set; and when the memory occupied by the referred object set reaches a threshold value, sending an object release instruction to the virtual machine according to the sequence of the priority of the referred objects in the referred object set from low to high so as to sequentially release the referred objects in the referred object set.
Referring to fig. 2, fig. 2 is a schematic structural diagram of a physical machine 103 provided in the embodiment of the present application, and the physical machine is used to implement the object referencing method provided in the embodiment of the present application, where the physical machine 103 includes: at least one processor 210, memory 250, at least one network interface 220, and a user interface 230. The various components in physical machine 103 are coupled together by a bus system 240. It is understood that the bus system 240 is used to enable communications among the components. The bus system 240 includes a power bus, a control bus, and a status signal bus in addition to a data bus. For clarity of illustration, however, the various buses are labeled as bus system 240 in fig. 2.
The Processor 210 may be an integrated circuit chip having Signal processing capabilities, such as a general purpose Processor, a Digital Signal Processor (DSP), or other programmable logic device, discrete gate or transistor logic device, discrete hardware components, or the like, wherein the general purpose Processor may be a microprocessor or any conventional Processor, or the like.
The user interface 230 includes one or more output devices 231, including one or more speakers and/or one or more visual display screens, that enable the presentation of media content. The user interface 230 also includes one or more input devices 232, including user interface components that facilitate user input, such as a keyboard, mouse, microphone, touch screen display, camera, other input buttons and controls.
The memory 250 may be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid state memory, hard disk drives, optical disk drives, and the like. Memory 250 optionally includes one or more storage devices physically located remotely from processor 210.
The memory 250 includes volatile memory or nonvolatile memory, and may include both volatile and nonvolatile memory. The nonvolatile Memory may be a Read Only Memory (ROM), and the volatile Memory may be a Random Access Memory (RAM). The memory 250 described in embodiments herein is intended to comprise any suitable type of memory.
In some embodiments, memory 250 is capable of storing data, examples of which include programs, modules, and data structures, or a subset or superset thereof, to support various operations, as exemplified below.
An operating system 251 including system programs for processing various basic system services and performing hardware-related tasks, such as a framework layer, a core library layer, a driver layer, etc., for implementing various basic services and processing hardware-based tasks;
a network communication module 252 for communicating to other computing devices via one or more (wired or wireless) network interfaces 220, exemplary network interfaces 220 including: bluetooth, wireless compatibility authentication (WiFi), and Universal Serial Bus (USB), etc.;
a display module 253 to enable presentation of information (e.g., a user interface for operating peripherals and displaying content and information) via one or more output devices 231 (e.g., a display screen, speakers, etc.) associated with the user interface 230;
an input processing module 254 for detecting one or more user inputs or interactions from one of the one or more input devices 232 and translating the detected inputs or interactions.
In some embodiments, the apparatus provided by the embodiments of the present application may be implemented in software, and fig. 2 illustrates the object referencing apparatus 106 stored in the memory 250, which may be software in the form of programs and plug-ins, and the like, and includes the following software modules: an acquisition module 1061 and a release module 1062, which are logical and therefore arbitrarily combined or further split depending on the functions implemented. The functions of the respective modules will be explained below.
In other embodiments, the apparatus provided in the embodiments of the present Application may be implemented in hardware, and for example, the apparatus provided in the embodiments of the present Application may be a processor in the form of a hardware decoding processor, which is programmed to execute the object referencing method provided in the embodiments of the present Application, for example, the processor in the form of the hardware decoding processor may be one or more Application Specific Integrated Circuits (ASICs), DSPs, Programmable Logic Devices (PLDs), Complex Programmable Logic Devices (CPLDs), Field Programmable Gate Arrays (FPGAs), or other electronic components.
The object referencing method provided by the embodiments of the present application will be described below in conjunction with exemplary applications and implementations of physical machines provided by the embodiments of the present application.
Referring to fig. 3, fig. 3 is an alternative flowchart of an object referencing method provided in an embodiment of the present application, and will be described with reference to the steps shown in fig. 3.
Step S301, at least one referred object referred to by the reference object of the program running on the virtual machine is obtained, and a referred object set is obtained.
Here, the virtual machine runs on the physical machine, and on the physical machine, a control program or software for executing the object referencing method according to the embodiment of the present application runs, and the control program or software is implemented as an object referencing apparatus, and the object referencing method according to the embodiment of the present application is implemented by the object referencing apparatus.
In this embodiment of the present application, the reference object may be any object that can refer to another object of the program running on the virtual machine. For example, in a program, the reference object may be a variable (denoted as a first variable) which points to another variable (denoted as a second variable) through an assignment statement, and the second variable is the referenced object.
The referenced object is an object referenced by the referencing object. For example, in a program, the referenced object may also be a variable, i.e., a second variable, which may be referenced by the first variable through an assignment statement.
It should be noted that in the embodiment of the present application, the reference object may be at least one, that is, the reference object may be any one or more variables of the program, and as long as the variable references other objects, the variable may be regarded as a reference object. Correspondingly, a variable can be considered as a referenced object as long as the variable is referenced by the referencing object.
The set of referenced objects includes at least one referenced object, and the set of referenced objects is formed to store at least one referenced object when a reference between variables occurs in the process.
In the embodiment of the present application, the referencing object may use a preset referencing manner, where the preset referencing manner is different from any one of four referencing manners in the related art, and the preset referencing manner may implement a referencing process and a referenced object release process that cannot be implemented by the four referencing manners in the related art.
It should be noted that the preset referencing method and four referencing methods in the related art may coexist, that is, in one program, the program may include both the referenced object that is referenced by using the referencing method in the related art and the referenced object that is referenced by using the preset referencing method.
Step S302, when the memory occupied by the referenced object set reaches a threshold, sequentially releasing the referenced objects in the referenced object set according to a sequence from low to high of the priorities of the referenced objects in the referenced object set.
Here, since the referenced objects in the referenced object set are all objects referenced by adopting a preset reference manner, in the memory of the physical machine, a storage space for the user to cache the referenced object set is allocated. At the same time, the maximum value of the memory occupied by the set of referenced objects, i.e. the threshold value, is also set. In some embodiments, the threshold may be set by the developer through a physical machine or by the developer through an object referencing device.
When the memory occupied by the referred object set reaches a threshold value, the number of referred objects stored in the current memory is large, and if the referred objects exceed the upper limit of the memory capable of storing the referred objects in the memory and the excessive referred objects are not released and recycled, the memory overflow is caused, so that the program running on the virtual machine is crashed.
Therefore, in the embodiment of the present application, when the memory occupied by the set of referenced objects reaches the threshold, part of the referenced objects needs to be released, and in the implementation process, the referenced objects can be released in sequence from low to high according to the priority of each referenced object, so that the referenced objects are recovered by the garbage recoverer.
It should be noted that, in the embodiment of the present application, when releasing a referenced object referenced by a preset referencing manner, all referenced objects are not released at one time, but released sequentially according to a priority order, that is, the process of releasing referenced objects in a referenced object set is performed step by step, so that effective release of a memory can be ensured, the size of the memory occupied by the currently remaining referenced object can be detected again after each referenced object is released, and when the size of the memory occupied by the currently remaining referenced object meets a preset condition, the release of the referenced object in the referenced object set can be stopped. That is to say, after releasing the preset number of the referenced objects, and when the memory occupied by the remaining referenced objects that are not released is less than or equal to a certain memory threshold, the release of the referenced objects is stopped.
And when the memory occupied by the referenced objects in the referenced object set is increased to the threshold value again in the further running process of the subsequent program, continuing to start the releasing process. The cyclic execution realizes the real-time monitoring and dynamic adjustment of the memory, and ensures that the memory occupation is always kept within a reasonable range, thereby ensuring that the program can be executed orderly and preventing the program from running and collapsing.
For example, the referenced objects referenced by the referencing object D1 of the program running on the virtual machine include a1, B1, and C1, and thus referenced objects a1, B1, and C1 form the set of referenced objects. If it is detected that the memory occupied by the current referenced object set reaches the threshold K1, that is, the total memory occupied by the referenced objects a1, B1 and C1 reaches the threshold K1, then the release of the partial referenced objects in the referenced object sets, referenced objects a1, B1 and C1, is required. Wherein, the priority of the referenced objects A1, B1 and C1 is from high to low as: a1> B1> C1, then, when releasing the referenced objects in the set of referenced objects, the referenced object C1 is released first, and after releasing the referenced object C1, the memory size occupied by the remaining referenced objects A1 and B1 is determined. If the memory size occupied by the referenced objects A1 and B1 is smaller than the memory threshold K2, the memory size occupied by the remaining referenced objects which are not released is considered to meet the preset condition, and therefore, the release of the referenced objects A1 and B1 is stopped; if the memory size occupied by the referenced objects A1 and B1 is greater than K2, the memory occupied by the remaining referenced objects that are not released is considered to not satisfy the preset condition, and therefore the release of the referenced object B1 is continued. If the referenced object E1 is newly added to the referenced object set in the further running process of the subsequent program, it is determined whether the memory occupied by the referenced objects A1 and E1 reaches the threshold value K1, and the subsequent release process is continued.
According to the object reference method provided by the embodiment of the application, when the memory occupied by the referenced object set reaches the threshold value, the referenced objects in the referenced object set are released in sequence according to the sequence of the priorities of the referenced objects in the referenced object set from low to high, so that the referenced objects can be released in order, all the referenced objects cannot be released at one time, the referenced objects cannot be stored all the time and cannot be released, and the memory overflow caused by the memory occupied by the referenced objects can be prevented.
In some embodiments, based on fig. 3, before releasing referenced objects in the set of referenced objects in sequence in step S302, the method further includes:
step S310, according to the frequency of each referenced object being referenced, determining the priority of the corresponding referenced object.
Here, after obtaining at least one referenced object referenced by a referencing object of a program running on the virtual machine and obtaining a set of referenced objects, the priority of each referenced object may be determined according to the frequency of referencing the referenced object.
The frequency with which the referenced object is referenced may be the number of times or frequency with which different referencing objects reference the referenced object in the program, or the number of times or frequency with which the same referencing object references the referenced object. When the frequency of a certain referenced object being referenced is higher, the priority of the corresponding referenced object is higher, and conversely, when the frequency of a certain referenced object being referenced is lower, the priority of the corresponding referenced object is lower.
After the priority of each referenced object is determined, the priority corresponding to the referenced object can be used as attribute information of the referenced object to form the referenced object set together with the referenced object, and the referenced object set is stored in a memory of the electronic device (namely, the physical machine), so that when the referenced object is released subsequently, the priority of the referenced object can be simultaneously obtained when the referenced object is obtained, and then a releasing process based on the priority order is performed according to the priority of each referenced object.
According to the object referencing method provided by the embodiment of the application, the priority corresponding to the referenced object is determined according to the frequency of each referenced object being referenced, so that for the referenced object with low referencing frequency, the probability that the referenced object will be referenced again is low, and therefore the referenced object with low referencing frequency is determined as the referenced object with low priority, when the memory occupation reaches the threshold value, the referenced object with low referencing frequency is released preferentially, the referenced object with low referencing frequency is guaranteed to be recovered less possibly in the subsequent referencing process, the program execution efficiency is improved, and the time delay of program operation is reduced.
In some embodiments, as shown in fig. 4, based on fig. 3, after step S302, it may further perform:
in step S401, the released referenced object is stored in a preset storage unit through serialization processing.
Here, after the referenced object is released, the referenced object is backed up, and the released referenced object is stored in the preset storage unit, so as to ensure that, in the subsequent program running process, if the released referenced object needs to be referred again, the referenced object can be recovered from the preset storage unit, so as to realize the re-reference of the released referenced object, and thus ensure the normal running of the program.
In the embodiment of the present application, before storing the released referenced objects, a serialization process may be performed on the referenced objects, where the serialization process refers to a process of converting state information of the released referenced objects into a form that can be stored or transmitted, during the serialization process, the released referenced objects write their current state into a temporary or persistent storage area (i.e., the preset storage unit), and then, the released referenced objects may be recreated by reading or deserializing the released states of the referenced objects from the storage area.
In some embodiments, as shown in fig. 4, the method may further include the steps of:
step S402, when the released referenced object is referred to, restoring the released referenced object to the memory of the electronic device.
Here, when the released referenced object is referred to again, the released referenced object may be restored from the preset storage unit to the memory of the electronic device.
It should be noted that, in the embodiment of the present application, the following two recovery manners for the released referenced object are included:
first, only re-referenced objects, i.e., only released referenced objects that are re-referenced and stored in a preset storage unit, are restored.
Second, while restoring the re-referenced objects, other objects are restored, i.e., while restoring the released referenced objects that are re-referenced and stored in the preset storage unit, other released referenced objects stored in the preset storage unit are also restored.
In some embodiments, as shown in fig. 5, which is an optional flowchart of the object referencing method provided in the embodiment of the present application, please combine fig. 4 and fig. 5, and step S402 may also be implemented by the following steps:
step S4021, determining an object to be restored from the preset storage unit when the released referenced object is referenced.
Here, the released referenced object is stored in the preset storage unit, and when a reference is made to an object stored in the preset storage unit, that is, it indicates that there is a currently referenced object referencing the released referenced object, the object to be restored is determined from the preset storage unit.
The number of the objects to be restored is one or more, the objects to be restored may include referenced objects to be referenced by the current referencing object, or the objects to be restored may include other objects not to be referenced by the current referencing object, in addition to the referenced objects to be referenced by the current referencing object.
Step S4022, performing deserialization processing on the object to be recovered.
And after determining the objects to be recovered, performing deserialization processing on each object to be recovered. The deserialization process here is a procedure completely reverse to the serialization process described above, and by the deserialization process, the state of the object to be restored can be processed, so that the released referenced object is recreated.
Step S4023, storing the object to be restored after the deserialization process into the referred object set, so as to restore the released referred object into the memory of the electronic device.
After the object to be restored is deserialized, the deserialized object to be restored is stored in the referred object set, so that the restoration process of the object to be restored is realized, that is, the released referred object is restored into the memory of the electronic device.
In the embodiment of the application, because the released referenced object is recovered and restored to the referenced object set, the released referenced object can continue to be referenced in the subsequent program running process.
In some embodiments, since the number of the objects to be restored may be one, the objects to be restored include referenced objects to be referenced by the current referencing object.
Then, as shown in fig. 6A, it is an optional flowchart of the object referencing method provided in the embodiment of the present application, please combine fig. 5 and fig. 6A, and step S4021 may be further implemented by the following steps:
step S601, determining whether the object to be referred to by the current referring object is the released referred object in the preset storage unit.
Step S602, when the released referenced object is referred to, determining the referred referenced object as the object to be restored from the preset storage unit.
Here, since there is one object to be restored, when restoring the released referenced object, only the referenced object to be referenced by the current referencing object is restored. Then, when determining an object to be restored, the referenced released referenced object is determined as the object to be restored. In this way, it can be realized that only the referenced objects that are released are restored when the objects to be restored are restored.
For example, the released referenced objects a2, B2, and C2 are included in the preset storage unit. If there is currently a referencing object D2 to reference the referenced object B2, then the released referenced object B2 needs to be restored from the preset storage unit, i.e., when the restoration of the released referenced object is implemented, only the released referenced object B2 is restored.
In this embodiment of the present application, the released referenced object referred to by the referencing object is determined as the object to be restored, so that only the released referenced object referred to by the referencing object is restored, that is, the first restoring manner for restoring the released referenced object is implemented: i.e. only the re-referenced and released referenced objects stored in the preset memory location are restored.
In some embodiments, since the number of the objects to be restored may be multiple, the objects to be restored may include, in addition to the referenced object to be referenced by the current referencing object, other objects that are not to be referenced by the current referencing object.
Then, as shown in fig. 6B, it is an optional flowchart of the object referencing method provided in the embodiment of the present application, please combine fig. 5 and fig. 6B, and step S4021 may be further implemented by the following steps:
in step S611, it is determined whether the object to be referenced by the current referencing object is a released referenced object in the preset storage unit.
Step S612, when the released referenced object is referred to, determining the referred object that is referred to and the released referenced object having a preset priority as the object to be restored from the preset storage unit.
Here, since there are a plurality of objects to be restored, when restoring the released referenced objects, not only the referenced objects to be referenced by the current referencing object but also other objects not to be referenced by the current referencing object are restored. Then, when determining the object to be restored, the referenced released referenced object and other released referenced objects that are not referenced by the current referencing object are determined as the object to be restored. In this way, when restoring the object to be restored, the released referenced object and the unreferenced released referenced object which are referenced can be restored simultaneously.
In some embodiments, the other objects that are not to be referenced by the current referencing object may have a predetermined priority, e.g., the other objects that are not to be referenced by the current referencing object have a higher priority than the referenced objects to be referenced by the current referencing object. That is, while restoring the referenced object itself to be referenced by the current referencing object, other released referenced objects having a higher priority than the referenced object itself are restored at the same time.
For example, the released referenced objects A3, B3 and C3 are included in the preset storage unit, wherein the priority of the released referenced objects A3, B3 and C3 is in the order from high to low: a3> B3> C3. If the reference object D3 currently exists to reference the referenced object B3, the released referenced object B3 needs to be restored from the preset storage unit, but when B3 is restored, A3 with higher priority than B3 is restored at the same time, namely when restoration of the released referenced object is realized, A3 and B3 are restored at the same time.
In the embodiment of the present application, the released referenced object and other released referenced objects with higher priority than the referenced object themselves are determined as the object to be restored, so that not only the referenced object itself to be referenced by the current referencing object is restored, but also other released referenced objects with higher priority than the referenced object themselves are restored at the same time, that is, the second restoring manner for restoring the released referenced object is implemented: while restoring the re-referenced objects, some other objects are restored.
Next, an exemplary application of the embodiment of the present application in a practical application scenario will be described.
Based on the problems of the object reference method in the related art, the embodiment of the application further provides an object reference method, an intelligent reference mode is introduced, the intelligent reference mode is used as a new reference mode, some referenced objects with low priority can be intelligently released when the memory reaches the upper limit, and the referenced objects can be guaranteed to be regenerated when being used later, so that developers can avoid the problem of memory leakage, the requirements of the developers on code memory management are simplified, and the developers debug the local application programs of the users at the cloud.
The key technology to be solved by the embodiment of the application comprises the following steps: adding an intelligent reference related Application Programming Interface (API) and a Class to an open jdk software expansion library through an intelligent reference library to realize a callable intelligent reference Class; through the intelligent garbage recycling machine, the current mainstream JVM virtual machine is improved, and the judgment of the intelligent referenced object cleaning is increased.
The following is a detailed description of the key technology of the embodiments of the present application.
When some scenes with intense memory or the memory is difficult to manage, a developer can change the reference of some objects from strong reference to intelligent reference of the embodiment of the application without worrying about the life cycle management of the referenced objects. When the intelligent reference objects (namely, referenced objects) referenced by the intelligent reference mode increasingly reach the upper limit of the memory, the GC of the intelligent garbage recycling machine is triggered, and the intelligent garbage recycling machine releases the intelligent reference objects according to the priority level until the memory is restored to a reasonable interval. That is, the intelligent reference mode does not consider the life cycle of the object, and when the memory reaches the upper limit, the objects are recycled in sequence according to the priority of the intelligent reference objects.
When the intelligent reference object is released, the intelligent reference object is backed up, namely, the intelligent reference object is serialized into a hard disk. If the following program uses the intelligent reference object again, the JVM reads the serialized file in the hard disk and generates the intelligent reference object through the deserialization operation, so that the program can continue to use the intelligent reference object seamlessly.
The developer does not need to pay attention to the life cycle of the object and check the problem of memory leakage, only needs to use the intelligent reference and set the recovery priority for the intelligent reference object, the priority does not need to be set, the intelligent recovery algorithm dynamically adjusts the priority, and the recovery efficiency is higher.
Fig. 7 is a schematic structural diagram of an intelligent referencing apparatus provided in an embodiment of the present application, where an object referencing method may be an intelligent referencing method, and an object referencing apparatus may be an intelligent referencing apparatus, as shown in fig. 7, where the intelligent referencing apparatus 700 includes: a Java intelligent reference library 701 and a Java intelligent garbage collector 702.
The intelligent citation device based on fig. 7 is, as shown in fig. 8, a schematic diagram of an implementation flow of an intelligent citation method provided in an embodiment of the present application, where the method includes the following steps:
in step S801, the developer 80 performs program development to write a Java program with an intelligent reference mode, and obtains an application program 81.
Step S802, when the intelligent reference is executed in the application program, the Java intelligent reference library 70 is called to store the intelligent reference object.
Step S803, the ID of the smart referencing object is passed to the JVM virtual machine.
Step S804, when the memory reaches the upper limit, the Java intelligent garbage collector 702 performs recovery cleaning on the memory 82, and recovers the intelligent reference object.
In FIG. 8, it is shown that for smart referencing objects A, B and C, the Java Smart garbage collector reclaims smart referencing objects A and B, and the smart referencing object C is retained.
Step S805, the Java intelligent garbage collection machine serializes the collected intelligent referencing objects a and B, and stores the serialized intelligent referencing objects a and B in the hard disk 83.
According to the intelligent reference method provided by the embodiment of the application, the application program is assumed to have A, B and C intelligent reference objects, when the memory is full, the Java intelligent garbage collector calculates which intelligent reference objects in the latest program have high use probability and which intelligent reference objects have low use probability through an intelligent algorithm, namely the cleaning priority of the hot object is lower than that of the object which is not frequently used recently. And if the Java intelligent garbage collector obtains that the cleaning priority of the A and the B is higher than that of the C and the cleaning of the A and the B can meet the use requirement of a normal memory, sequencing the A and the B to the hard disk and cleaning the intelligent reference objects A and B in the memory.
When the application program is subsequently executed to a or B again, the Java intelligent garbage collection machine is invoked to recover a or B, as shown in fig. 9, which is a schematic flow diagram for implementing recovery of an object in the intelligent citation method provided by the embodiment of the present application, wherein the recovery process includes the following steps:
in step S901, the developer 80 performs program development, writes a Java program with an intelligent reference mode, and obtains an application program 81.
In step S902, when the smart reference is executed again, the Java smart reference library 701 is called to obtain the smart reference object.
And step S903, transmitting the ID of the intelligent reference object to the JVM virtual machine.
In step S904, if the referenced smart referencing object is an object that is retrieved by the Java smart garbage retriever 702, the serialization information of the smart referencing object is read in the hard disk 83.
In step S905, the Java intelligent garbage collection machine 702 performs deserialization processing on the intelligent referencing object to generate a referenced object to the memory 82.
In the embodiment of the application, when the application program is executed to the released intelligent object, the java intelligent garbage collection machine is called to recover the intelligent reference object. In some embodiments, the recovery may not be what is missing, and an intelligent algorithm is used to estimate hot intelligent references used by the most recent code region and perform deserialization generation on the objects that have been released by these intelligent references, thereby avoiding the inefficient process of needing recovery once per execution.
The object reference method provided by the embodiment of the application can reduce the work of a developer on memory management, so that the developer of the application can be put into business development more physically and mentally. Compared with the problem that the pointer in the related technology needs to be released manually, the related technology increases the workload of a programmer and the maintenance difficulty of codes, moreover, the Java technology in the related technology adopts a garbage collection mechanism to reduce the management of the programmer on the memory to a certain extent, but in some scenes, the wrong strong reference of the programmer still causes the memory to be unable to be released, and in mobile application, the memory is limited, so that the Java programmer still has many requirements on the memory management. The object reference method of the embodiment of the application can completely liberate the management of programmers on the memory through an intelligent reference mode, and reduces development threshold and code maintenance.
In addition, the object reference method provided by the embodiment of the application can solve the problem of memory leak in some applications, because the memory leak is sometimes particularly hidden and difficult to search, and through an intelligent reference mode, even if the memory leak does not cause program crash, because the intelligent reference object is released when the memory is too high.
Continuing with the exemplary structure of the object referencing device 255 implemented as a software module provided in the embodiments of the present application, in some embodiments, as shown in fig. 2, the software module stored in the object referencing device 106 of the memory 250 may include:
an obtaining module 1061, configured to obtain at least one referenced object referenced by a referencing object of a program running on a virtual machine, to obtain a set of referenced objects;
a releasing module 1062, configured to, when the memory occupied by the referenced object set reaches a threshold, sequentially release the referenced objects in the referenced object set according to an order that the priorities of the referenced objects in the referenced object set are from low to high.
In some embodiments, the apparatus further comprises:
and the determining module is used for determining the priority of the corresponding referenced object according to the referenced frequency of each referenced object.
In some embodiments, the apparatus further comprises:
and the storage module is used for storing the released referenced objects in the referenced object set to a preset storage unit through serialization processing after the referenced objects in the referenced object set are released.
In some embodiments, the apparatus further comprises:
and the recovery module is used for recovering the released referenced objects to the memory of the electronic equipment when the released referenced objects are referred.
In some embodiments, the recovery module is further to:
when the released referenced object is referenced, determining an object to be restored from the preset storage unit; performing deserialization processing on the object to be recovered; and storing the object to be restored after the deserialization processing into the referred object set so as to restore the released referred object into the memory of the electronic equipment.
In some embodiments, the recovery module is further to:
when the released referenced object is referred, determining the referred referenced object as the object to be restored from the preset storage unit.
In some embodiments, the recovery module is further to:
when the released referenced object is referred, determining the referred object and the released referenced object with preset priority as the object to be restored from the preset storage unit.
It should be noted that the description of the apparatus in the embodiment of the present application is similar to the description of the method embodiment, and has similar beneficial effects to the method embodiment, and therefore, the description is not repeated. For technical details not disclosed in the embodiments of the apparatus, reference is made to the description of the embodiments of the method of the present application for understanding.
Embodiments of the present application provide a storage medium having stored therein executable instructions, which when executed by a processor, will cause the processor to perform a method provided by embodiments of the present application, for example, the method as illustrated in fig. 3.
In some embodiments, the storage medium may be a Memory such as a Ferroelectric Random Access Memory (FRAM), a Read Only Memory (ROM), a Programmable Read Only Memory (PROM), an Erasable Programmable Read Only Memory (EPROM), a charged Erasable Programmable Read Only Memory (EEPROM), a flash Memory, a magnetic surface Memory, an optical disc, or a Compact disc Read Only Memory (CD-ROM); or may be various devices including one or any combination of the above memories.
In some embodiments, executable instructions may be written in any form of programming language (including compiled or interpreted languages), in the form of programs, software modules, scripts or code, and may be deployed in any form, including as a stand-alone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.
By way of example, executable instructions may correspond, but do not necessarily have to correspond, to files in a file system, and may be stored in a portion of a file that holds other programs or data, such as in one or more scripts in a hypertext Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple coordinated files (e.g., files that store one or more modules, sub-programs, or portions of code).
By way of example, executable instructions may be deployed to be executed on one computing device or on multiple computing devices at one site or distributed across multiple sites and interconnected by a communication network.
The above description is only an example of the present application, and is not intended to limit the scope of the present application. Any modification, equivalent replacement, and improvement made within the spirit and scope of the present application are included in the protection scope of the present application.

Claims (10)

1. An object referencing method, comprising:
acquiring at least one referred object referred by a reference object of a program running on a virtual machine to obtain a referred object set;
and when the memory occupied by the referred object set reaches a threshold value, releasing the referred objects in the referred object set in sequence according to the sequence of the priority of the referred objects in the referred object set from low to high.
2. The method of claim 1, further comprising:
and determining the priority of each referenced object according to the referenced frequency of each referenced object.
3. The method of claim 1, further comprising:
and after releasing the referenced objects in the referenced object set, storing the released referenced objects in a preset storage unit through serialization processing.
4. The method of claim 3, further comprising:
and when the released referenced object is referred, restoring the released referenced object to the memory of the electronic equipment.
5. The method of claim 4, wherein restoring the released referenced object to the memory of the electronic device when the released referenced object is referenced comprises:
when the released referenced object is referenced, determining an object to be restored from the preset storage unit;
performing deserialization processing on the object to be recovered;
and storing the object to be restored after the deserialization processing into the referred object set so as to restore the released referred object into the memory of the electronic equipment.
6. The method according to claim 5, wherein the determining an object to be restored from the preset storage unit when the released referenced object is referenced comprises:
when the released referenced object is referred, determining the referred referenced object as the object to be restored from the preset storage unit.
7. The method according to claim 5, wherein the determining an object to be restored from the preset storage unit when the released referenced object is referenced comprises:
when the released referenced object is referred, determining the referred object and the released referenced object with preset priority as the object to be restored from the preset storage unit.
8. An object referencing apparatus, comprising:
the acquisition module is used for acquiring at least one referred object referred by a reference object of a program running on the virtual machine to obtain a referred object set;
and the releasing module is used for sequentially releasing the referenced objects in the referenced object set according to the sequence of the priorities of the referenced objects in the referenced object set from low to high when the memory occupied by the referenced object set reaches a threshold value.
9. An object referencing device, comprising:
a memory for storing executable instructions; a processor for implementing the method of any one of claims 1 to 7 when executing executable instructions stored in the memory.
10. A storage medium having stored thereon executable instructions for causing a processor to perform the method of any one of claims 1 to 7 when executed.
CN201910690893.XA 2019-07-29 2019-07-29 Object reference method, device, equipment and storage medium Pending CN112306894A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910690893.XA CN112306894A (en) 2019-07-29 2019-07-29 Object reference method, device, equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910690893.XA CN112306894A (en) 2019-07-29 2019-07-29 Object reference method, device, equipment and storage medium

Publications (1)

Publication Number Publication Date
CN112306894A true CN112306894A (en) 2021-02-02

Family

ID=74329958

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910690893.XA Pending CN112306894A (en) 2019-07-29 2019-07-29 Object reference method, device, equipment and storage medium

Country Status (1)

Country Link
CN (1) CN112306894A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113778624A (en) * 2021-08-27 2021-12-10 广州虎牙科技有限公司 Object processing method and device, intelligent terminal and storage medium

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113778624A (en) * 2021-08-27 2021-12-10 广州虎牙科技有限公司 Object processing method and device, intelligent terminal and storage medium
CN113778624B (en) * 2021-08-27 2024-06-21 广州虎牙科技有限公司 Object processing method and device, intelligent terminal and storage medium

Similar Documents

Publication Publication Date Title
CN101595460B (en) Implementation for collecting unmanaged memory
KR101636870B1 (en) Method and apparatus for generating minimal boot image
JP4701321B2 (en) Memory management
US7900011B2 (en) Apparatus, system, and method for improving system performance in a large memory heap environment
US6044475A (en) Checkpoint and restoration systems for execution control
US6105148A (en) Persistent state checkpoint and restoration systems
US10621010B2 (en) Information processing apparatus and migration method
US6493730B1 (en) Efficient object faulting with generational garbage collection
CN112199162B (en) Disk snapshot method, device and medium based on virtualized disk double-active disaster tolerance
US20200104255A1 (en) Deferred destruction for efficient resource reclamation
JPH0581082A (en) Synchronous journaling system
CN103226499A (en) Method and device for restoring abnormal data in internal memory
US9575827B2 (en) Memory management program, memory management method, and memory management device
CN111966603B (en) Memory leakage detection method and device, readable storage medium and electronic equipment
US10761936B2 (en) Versioned records management using restart era
CN106776018B (en) Parallel processing method and equipment for master node and slave node of distributed system
CN115687174A (en) Method for recovering dynamic garbage of solid state disk and solid state disk
CN114765051A (en) Memory test method and device, readable storage medium and electronic equipment
US20120198184A1 (en) Memory management method, computer system and computer readable medium
US8713538B2 (en) Monitoring heap in real-time by a mobile agent to assess performance of virtual machine
CN112306894A (en) Object reference method, device, equipment and storage medium
US20160170876A1 (en) Managed runtime cache analysis
US6463515B1 (en) System and method for recovering physical memory locations in a computer system
Daoud et al. Multilevel analysis of the java virtual machine based on kernel and userspace traces
CN111435302A (en) Application processing method and device

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