Object reference method, device, equipment and storage medium
By implementing an object reference method in a Java virtual machine, the orderly release of the referenced object is solved, memory overflow caused by strong reference and program crash caused by other reference methods is solved, and memory management is simplified and program management is realized. stable operation.
Patent Information
- Application Number
- CN201910690893.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-07-29
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2039-12-01
AI Technical Summary
The objects associated with strong references in Java virtual machines cannot be recycled, resulting in memory overflow; and the unexpected release of other reference methods at the memory limit or recycling time may cause the program to crash.
Provides an object reference method. By obtaining the set of referenced objects referenced by the referenced objects running programs on the virtual machine, when the memory reaches the threshold, it is released in sequence from low to high in order of priority of the referenced objects.
It realizes orderly release of referenced objects, avoids memory overflow, and ensures regeneration of objects during subsequent use, simplifying developers' requirements for code memory management.
Smart Images

Figure CN112306894B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and relate to but are not limited to an object reference method, apparatus, device, and storage medium. Background Art
[0002] Currently, the Java Virtual Machine (JVM) has four reference modes: strong reference, soft reference, weak reference and phantom reference.
[0003] Among them, objects associated with strong references will not be recycled by the JVM unless they are released, and are non-recyclable resources; objects associated with soft references will not be recycled if there is enough memory space, but will be recycled if there is insufficient memory space; objects associated with weak references will be recycled regardless of whether there is enough memory; objects associated with phantom references may be recycled at any time.
[0004] However, in the case of strong references, since the objects associated with the strong references will never be recycled by the JVM, if the objects associated with the strong references reach the memory limit, it will directly lead to memory overflow (OOM), and the program will crash. In the other three reference cases, since the referenced objects will be released at the memory limit or at the recycling time, that is, all objects will be released unexpectedly, so when the program uses the reference method again, the objects may have been released, causing the program to crash with a null pointer exception or fail to execute according to the original logic. Summary of the invention
[0005] The embodiments of the present application provide an object reference method, apparatus, device and storage medium, which can release referenced objects in an orderly manner and prevent memory overflow caused by the referenced objects occupying memory.
[0006] The technical solution of the embodiment of the present application is implemented as follows:
[0007] The present application embodiment provides an object reference method, including:
[0008] Obtain at least one referenced object referenced by a reference object of a program running on the virtual machine to obtain a referenced object set;
[0009] When the memory occupied by the referenced object set reaches a threshold, the referenced objects in the referenced object set are released in sequence according to the priority of the referenced objects in the referenced object set from low to high.
[0010] The present application embodiment provides an object reference device, including:
[0011] An acquisition module, used to acquire at least one referenced object referenced by a reference object of a program running on the virtual machine, and obtain a referenced object set;
[0012] The release module is used to release the referenced objects in the referenced object set in sequence according to the priority 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.
[0013] The present application embodiment provides an object reference device, including:
[0014] The memory is used to store executable instructions; the processor is used to implement the above-mentioned object reference method when executing the executable instructions stored in the memory.
[0015] An embodiment of the present application provides a storage medium storing executable instructions for causing a processor to execute and implement the above-mentioned object reference method.
[0016] The embodiments of the present application have the following beneficial effects:
[0017] When the memory occupied by the referenced object set reaches the threshold, the referenced objects in the referenced object set are released in order of priority from low to high. In this way, the referenced objects can be released in an orderly manner, and all the referenced objects will not be released at once, nor will the referenced objects be kept and not released all the time, thereby preventing memory overflow caused by the referenced objects occupying memory. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 It is a schematic diagram of an application scenario of the object reference method provided in an embodiment of the present application;
[0019] Figure 2 is a schematic diagram of the structure of a physical machine provided in an embodiment of the present application;
[0020] Figure 3 This is an optional flowchart of the object reference method provided in the embodiment of the present application;
[0021] Figure 4 This is an optional flowchart of the object reference method provided in the embodiment of the present application;
[0022] Figure 5 This is an optional flowchart of the object reference method provided in the embodiment of the present application;
[0023] Fig. 6A This is an optional flowchart of the object reference method provided in the embodiment of the present application;
[0024] Figure 6B This is an optional flowchart of the object reference method provided in the embodiment of the present application;
[0025] Figure 7 is a schematic diagram of the structure of the intelligent citation device provided in an embodiment of the present application;
[0026] Figure 8 It is a schematic diagram of the implementation process of the intelligent citation method provided in the embodiment of the present application;
[0027] Fig. 9 It is a schematic diagram of the implementation process of restoring objects in the intelligent reference method provided in an embodiment of the present application. DETAILED DESCRIPTION
[0028] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings. The described embodiments should not be regarded as limiting the present application. All other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of this application.
[0029] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it will be 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.
[0030] Unless otherwise defined, all technical and scientific terms used in the embodiments of the present application have the same meaning as those commonly understood by technicians in the technical field of the embodiments of the present application. The terms used in the embodiments of the present application are only for the purpose of describing the embodiments of the present application and are not intended to limit the present application.
[0031] Before further describing the embodiments of the present application in detail, the nouns and terms involved in the embodiments of the present application are explained. The nouns and terms involved in the embodiments of the present application are subject to the following interpretations.
[0032] 1) Reference is the process of assigning an object to a variable. When it comes to reference, the first thing that comes to mind is the assignment statement, that is, a variable points to an object. This is the most common way of reference. In the process of Java memory management and memory recycling, programmers do not need to intervene. As far as the garbage collector process is concerned, the reachability analysis algorithm can be used to determine whether an object is referenced. If it is not referenced, it will be marked by the Java Virtual Machine (JVM) and is likely to be recycled.
[0033] 2) Java Virtual Machine is an abstract computer, a virtual computer that can run Java code, which is realized by simulating various computer functions on an actual computer. Among them, JVM has its own complete hardware architecture, such as processor, stack, register, etc., and also has a corresponding instruction system.
[0034] 3) Garbage Collection (GC) is a process in the Java virtual machine and is the most frequently used object destruction tool in the Java platform. The garbage collector will monitor the operation of the Java application throughout the process. When it finds that some objects have become garbage, the garbage collector will destroy these objects and release the memory space occupied by these objects. In other words, the garbage collector will periodically check whether an object is referenced by other objects. When the object is no longer referenced by other objects at a certain moment, the object can be recycled at any time, thereby clearing the memory occupied by the object.
[0035] In order to better understand the object reference method provided in the embodiments of the present application, the object reference method in the related art is first analyzed and explained below.
[0036] In Java technology, JVM has introduced four reference methods since version 1.2, including strong reference, soft reference, weak reference and phantom reference. The levels of these four reference methods are from high to low: strong reference > soft reference > weak reference > phantom reference.
[0037] Among them, a strong reference means creating an object and assigning this object to a reference variable. Then this object can be called a strong reference object. A strong reference is a reference that will not be recovered by the JVM until the object is released. A strong reference object is a non-recyclable resource and the garbage collector will never recycle it. When the memory space is insufficient, the JVM would rather throw an Out Of Memory Error to cause the program to terminate abnormally than to solve the problem of insufficient memory by recycling the strong reference object.
[0038] The nature of soft references is dispensable. For soft reference objects, if there is enough memory space at this time, the garbage collector will not recycle it. If there is insufficient memory space, the memory of these objects will be reclaimed. As long as the garbage collector does not recycle the soft reference object, the soft reference object can be used by the program. The GC mechanism will clear the soft reference object before the virtual machine throws an OutOf Memory Error. At the same time, if there is a reference queue reference, the soft reference object will be added to the queue. If an object only has soft references, it will be recycled when there is insufficient space. In this case, this object can be used to build a cache of sensitive data, such as a web page cache, an image cache, etc. Soft references can be used together with a reference queue. When the object referenced by the soft reference is recycled, the soft reference is added to the reference queue.
[0039] The nature of weak references is also an optional state. If a reference object is a weak reference object, once the garbage collector determines that the object is a weak reference object during execution, the GC mechanism will be executed regardless of whether there is a memory overflow. At the same time, if there is a reference queue that references the weak reference object, the weak reference object will be added to the queue. In other words, weak references are used to describe non-essential objects. When the JVM performs garbage collection, objects associated with weak references will be recycled regardless of whether there is sufficient memory. Weak references can be used to build caches for sensitive data, for example, in scenarios with shorter life cycles and more sensitive to memory.
[0040] It should be noted that the difference between weak references and soft references is that weak reference objects have a shorter life cycle. As long as the garbage collector scans it, its memory will be reclaimed regardless of whether there is sufficient memory space or not.
[0041] When the object referenced by a phantom reference is a virtually reachable object, if there is a reference queue, the phantom reference object will be added to the queue, but the characteristic of a phantom reference is that the life cycle of the phantom referenced object will not be affected. Phantom references are different from the previous soft references and weak references. They do not affect the life cycle of an object. If an object is associated with a phantom reference, the phantom reference object may be reclaimed by the garbage collector at any time, just like if there is no reference associated with it. That is, if an object has a phantom reference, it may be reclaimed at any time. It should be noted that phantom references must be used in conjunction with reference queues. When the referenced object is reclaimed, the phantom reference is added to the reference queue. Its main purpose is to detect the garbage collection process.
[0042] Currently, all reference methods of JVM cannot avoid memory leaks due to developer negligence. Once some strong references are not released properly, when Java memory continues to increase, OOM is likely to occur on mobile devices with smaller memory, causing the program to crash.
[0043] Then, for the four types of references of the JVM virtual machine: strong references, weak references, soft references and phantom references, strong references are references that will not be reclaimed by the JVM until they are released. If the strong reference object associated with this reference reaches the memory limit, it will directly cause OOM and program crash. For other reference methods, objects in memory will be cleared when the memory limit or reclaim time is reached, but this unexpected clearing time may cause the object associated with the reference to be released when the program uses the reference again, causing the program to crash due to a null pointer exception or fail to execute according to the original logic.
[0044] In order to solve the defects of several reference methods in the related technology, the embodiments of the present application propose an object reference method, device, equipment and storage medium. By introducing an intelligent reference method, some low-priority referenced objects can be intelligently released when the memory reaches the upper limit, while ensuring that the referenced objects will be regenerated when they are subsequently used, so that developers can avoid memory leaks and simplify developers' requirements for code memory management.
[0045] The following describes an exemplary application of the object reference device provided in the embodiment of the present application. The device provided in the embodiment of the present application can be implemented as a control program in a physical machine. The following describes an exemplary application when the device is implemented as a control program in a physical machine.
[0046] See also Figure 1 , Figure 1 1 is a schematic diagram of an application scenario of the object reference method provided in an embodiment of the present application. A developer 101 develops a program 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 operation. The virtual machine 102 can be run on the physical machine 103 as a process of the physical machine 103, thereby realizing the operation of the program through the virtual machine 102 on the physical machine 103, and displaying the operation result of the program to a user 105 (exemplarily showing user 105-1 and user 105-2) on a client 104 (exemplarily showing client 104-1 and client 104-2) connected to the physical machine 103, thereby realizing the contact between the developed APP and the user.
[0047] The physical machine 103 also includes an object reference device 106 for implementing the method provided in the embodiment of the present application. The object reference device 106 may be a control program, or the object reference device may be software in the form of a program and a plug-in. The object reference device is parallel to the virtual machine and can obtain at least one referenced object referenced by a reference object of a program running on the virtual machine to obtain a referenced object set; and when the memory occupied by the referenced object set reaches a threshold, an object release instruction is sent to the virtual machine in the order of the priority of the referenced objects in the referenced object set from low to high, so as to release the referenced objects in the referenced object set in sequence.
[0048] See also Figure 2 , Figure 2 1 is a schematic diagram of the structure of the physical machine 103 provided in the embodiment of the present application, through which the object reference method provided in the embodiment of the present application is implemented, wherein the physical machine 103 includes: at least one processor 210, a memory 250, at least one network interface 220 and a user interface 230. The various components in the physical machine 103 are coupled together through a bus system 240. It can be understood that the bus system 240 is used to realize the connection and communication between these components. In addition to the data bus, the bus system 240 also includes a power bus, a control bus and a status signal bus. However, for the sake of clarity, in Figure 2 Various buses are labeled as bus system 240 .
[0049] The processor 210 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc., where the general-purpose processor can be a microprocessor or any conventional processor, etc.
[0050] The user interface 230 includes one or more output devices 231 that enable presentation of media content, including one or more speakers and / or one or more visual display screens. 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.
[0051] 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, etc. The memory 250 may optionally include one or more storage devices that are physically remote from the processor 210.
[0052] The memory 250 includes a volatile memory or a non-volatile memory, and may also include both volatile and non-volatile memories. The non-volatile memory may be a read-only memory (ROM), and the volatile memory may be a random access memory (RAM). The memory 250 described in the embodiments of the present application is intended to include any suitable type of memory.
[0053] In some embodiments, memory 250 can store data to support various operations, examples of which include programs, modules, and data structures, or a subset or superset thereof, as exemplarily described below.
[0054] 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;
[0055] A network communication module 252, used to reach other computing devices via one or more (wired or wireless) network interfaces 220, exemplary network interfaces 220 include: Bluetooth, Wireless Compatibility Certification (WiFi), and Universal Serial Bus (USB);
[0056] a display module 253 for enabling presentation of information via one or more output devices 231 (e.g., display screen, speaker, etc.) associated with the user interface 230 (e.g., a user interface for operating peripherals and displaying content and information);
[0057] The input processing module 254 is used to detect one or more user inputs or interactions from one of the one or more input devices 232 and translate the detected inputs or interactions.
[0058] In some embodiments, the device provided in the embodiments of the present application can be implemented in software. Figure 2 The object reference device 106 stored in the memory 250 is shown, which can be software in the form of a program and a plug-in, etc., and includes the following software modules: an acquisition module 1061 and a release module 1062. These modules are logical, so they can be arbitrarily combined or further split according to the functions implemented. The functions of each module will be explained below.
[0059] In other embodiments, the device provided in the embodiments of the present application can be implemented in hardware. As an example, the device provided in the embodiments of the present application can be a processor in the form of a hardware decoding processor, which is programmed to execute the object reference method provided in the embodiments of the present application. For example, the processor in the form of a hardware decoding processor can adopt 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.
[0060] The object reference method provided in the embodiment of the present application will be described below in conjunction with the exemplary application and implementation of the physical machine provided in the embodiment of the present application.
[0061] See also Figure 3 , Figure 3 is an optional flow chart of the object reference method provided in the embodiment of the present application, which will be combined with Figure 3 The steps shown are explained.
[0062] Step S301, obtaining at least one referenced object referenced by a reference object of a program running on a virtual machine, and obtaining a referenced object set.
[0063] Here, the virtual machine runs on the physical machine, and on the physical machine, a control program or software that executes the object reference method of the embodiment of the present application is also running. The control program or software is implemented as an object reference device, and the object reference method of the embodiment of the present application is implemented through the object reference device.
[0064] In the embodiment of the present application, the reference object may be any object of the program running on the virtual machine that can reference other objects. For example, in a program, the reference object may be a variable (referred to as the first variable), and the first variable points to another variable (referred to as the second variable) through an assignment statement, and the second variable is the referenced object.
[0065] The referenced object is an object referenced by the referenced object. For example, in a program, the referenced object may also be a variable, that is, a second variable, which may be referenced by the first variable through an assignment statement.
[0066] 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 can be considered as a reference object. Correspondingly, as long as a variable is referenced by the reference object, the variable can be considered as a referenced object.
[0067] The referenced object set includes at least one referenced object. When references between variables occur in the process, the referenced object set is formed to store at least one referenced object.
[0068] In an embodiment of the present application, the reference object can use a preset reference method to reference the referenced object. The preset reference method is different from any one of the four reference methods in the related technology. The preset reference method can implement the reference process and the release process of the referenced object that cannot be implemented by the four reference methods in the related technology.
[0069] It should be noted that the preset reference method and the four reference methods in the related art can coexist, that is, in one program, it can include both the referenced objects referenced by the reference method in the related art and the referenced objects referenced by the preset reference method.
[0070] Step S302: when the memory occupied by the referenced object set reaches a threshold, the referenced objects in the referenced object set are released in sequence according to the priority of the referenced objects in the referenced object set from low to high.
[0071] Here, since the referenced objects in the referenced object set are all objects referenced by a preset reference method, a storage space for the user to cache the referenced object set will be allocated in the memory of the physical machine. At the same time, a maximum value of the memory occupied by the referenced object set, i.e., the threshold, will also be set. In some embodiments, the threshold can be set by a developer through a physical machine or by an object reference device.
[0072] When the memory occupied by the referenced object set reaches a threshold, it indicates that a large number of referenced objects are currently stored in the memory. If the referenced objects exceed the upper limit of the memory that can store the referenced objects, and the excess referenced objects are not released and recycled, memory overflow will occur, thereby causing the program running on the virtual machine to crash.
[0073] Therefore, in an embodiment of the present application, when the memory occupied by the referenced object set reaches a threshold, some of the referenced objects need to be released. During the implementation process, they can be released in sequence according to the priority of each referenced object, in order from low to high, so that the referenced objects can be reclaimed by the garbage collector.
[0074] It should be noted that, in the embodiment of the present application, when releasing the referenced objects referenced by the preset reference method, they are not all released at once, but are released in order of priority, that is, the process of releasing the referenced objects in the referenced object set is carried out step by step, so that the effective release of the memory can be guaranteed. After each release of a referenced object, the size of the memory occupied by the currently remaining referenced objects can be re-detected. When the size of the memory occupied by the currently remaining referenced objects meets the preset conditions, the release of the referenced objects in the referenced object set can be stopped. In other words, after releasing the preset number of referenced objects, and when the memory occupied by the remaining referenced objects that have not been released is less than or equal to a certain memory threshold, the release of the referenced objects is stopped.
[0075] When the memory occupied by the referenced objects in the referenced object set increases to the threshold again during the subsequent program execution, the release process will continue. This cycle is executed in a loop to achieve real-time detection and dynamic adjustment of memory, ensuring that the memory usage is always kept within a reasonable range, thereby ensuring that the program can be executed in an orderly manner and preventing the program from crashing.
[0076] For example, the referenced objects referenced by the referenced object D1 of the program running on the virtual machine include A1, B1 and C1, so the referenced objects A1, B1 and C1 form the referenced object set. If it is detected that the memory occupied by the current referenced object set reaches the threshold value K1, that is, when the total memory occupied by the referenced objects A1, B1 and C1 reaches the threshold value K1, it is necessary to release some of the referenced objects A1, B1 and C1 in the referenced object set. Among them, the priority of the referenced objects A1, B1 and C1 is in the order from high to low: A1>B1>C1, then, when releasing the referenced objects in the referenced object set, 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 less than the memory threshold K2, it is considered that the memory size occupied by the remaining referenced objects that have not been released meets the preset condition, so 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, it is considered that the memory occupied by the remaining referenced objects that have not been released does not meet the preset condition, so the referenced object B1 continues to be released. If a referenced object E1 is newly added to the referenced object set during the further running of the subsequent program, it is determined whether the memory occupied by the referenced objects A1 and E1 reaches the threshold K1, and the subsequent release process continues.
[0077] The object reference method provided by the embodiment of the present application releases the referenced objects in the referenced object set in sequence according to the priority 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. In this way, the referenced objects can be released in an orderly manner, and all the referenced objects will not be released at once, nor will the referenced objects be kept and not released all the time, thereby preventing memory overflow caused by the referenced objects occupying memory.
[0078] In some embodiments, based on Figure 3 Before releasing the referenced objects in the referenced object set in sequence in step S302, the method further includes:
[0079] Step S310: determining the priority of the corresponding referenced object according to the reference frequency of each referenced object.
[0080] Here, after obtaining at least one referenced object referenced by a reference object of a program running on a virtual machine and obtaining a referenced object set, the priority of each referenced object may be determined according to the frequency of reference of the referenced object.
[0081] The frequency of the referenced object can be the number of times or frequencies that different referenced objects reference the referenced object in the program, or the number of times or frequencies that the same referenced object references the referenced object. The higher the frequency of a referenced object, the higher the priority of the referenced object. Conversely, the lower the frequency of a referenced object, the lower the priority of the referenced object.
[0082] After the priority of each referenced object is determined, the priority of the corresponding referenced object can be used as the attribute information of the referenced object together with the referenced object to form the referenced object set, and stored in the memory of the electronic device (i.e. the above-mentioned physical machine). In this way, when the referenced object is subsequently released, the priority of the referenced object can be obtained at the same time when the referenced object is obtained, and then the release process based on the priority order of each referenced object is performed.
[0083] The object reference method provided in the embodiment of the present application determines the priority of the corresponding referenced object according to the frequency of reference of each referenced object. In this way, for a referenced object with a low reference frequency, it indicates that the probability that the referenced object will be referenced again is low. Therefore, the referenced object with a low reference frequency is determined as a referenced object with a lower priority. Therefore, when the memory usage reaches a threshold, the referenced object with a low reference frequency is released first, ensuring that the referenced object may be restored less frequently in the subsequent reference process, thereby improving the efficiency of program execution and reducing the delay of program running.
[0084] In some embodiments, Figure 4 As shown, based on Figure 3 After step S302, the following may also be performed:
[0085] Step S401, storing the released referenced object into a preset storage unit through serialization processing.
[0086] Here, after the referenced object is released, the referenced object will be backed up and stored in a preset storage unit to ensure that during the subsequent program running process, if the released referenced object needs to be referenced again, the referenced object can be restored from the preset storage unit to achieve the re-reference of the released referenced object, thereby ensuring the normal operation of the program.
[0087] In an embodiment of the present application, before storing the released referenced objects, these referenced objects can be serialized first, wherein serialization refers to the process of converting the 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 states to a temporary or persistent storage area (i.e., the preset storage unit). Later, the state of the released referenced objects can be read from the storage area or deserialized to recreate the released referenced objects.
[0088] In some embodiments, Figure 4 As shown, the method may further include the following steps:
[0089] Step S402: when the released referenced object is referenced, the released referenced object is restored to the memory of the electronic device.
[0090] Here, when the released referenced object is referenced again, the released referenced object can be restored from the preset storage unit to the memory of the electronic device.
[0091] It should be noted that, in the embodiment of the present application, there are two methods for recovering the released referenced object:
[0092] The first one is to restore only the objects that are referenced again, that is, to restore only the released referenced objects that are referenced again and stored in the preset storage unit.
[0093] The second type is to restore some other objects while restoring the referenced object again, that is, while restoring the released referenced object that is referenced again and stored in the preset storage unit, also restore other released referenced objects stored in the preset storage unit.
[0094] In some embodiments, Figure 5 The figure is an optional flow chart of the object reference method provided in the embodiment of the present application. Figure 4 and Figure 5 As shown, step S402 can also be implemented by the following steps:
[0095] Step S4021: when referencing the released referenced object, determining an object to be restored from the preset storage unit.
[0096] Here, the released referenced object is stored in a preset storage unit. When the object stored in the preset storage unit is referenced, it indicates that there is currently a reference object that references the released referenced object. Therefore, the object to be restored is determined from the preset storage unit.
[0097] There are one or more objects to be restored, and the objects to be restored may include the referenced objects to be referenced by the current referencing object, or, in addition to the referenced objects to be referenced by the current referencing object, the objects to be restored may also include other objects that are not to be referenced by the current referencing object.
[0098] Step S4022: Deserialize the object to be restored.
[0099] After the objects to be restored are determined, each object to be restored is subjected to deserialization processing. The deserialization processing here is a process completely opposite to the above-mentioned serialization processing. Through the deserialization processing, the state of the object to be restored can be processed, thereby recreating the released referenced object.
[0100] Step S4023, storing the deserialized object to be restored in the referenced object set, so as to restore the released referenced object to the memory of the electronic device.
[0101] Here, after the object to be restored is deserialized, the deserialized object to be restored is stored in the referenced object set, thereby realizing the restoration process of the object to be restored, that is, restoring the released referenced object to the memory of the electronic device.
[0102] In the embodiment of the present application, since the released referenced object is restored and stored again in the referenced object set, the released referenced object can continue to be referenced by the referenced object during the subsequent program execution process.
[0103] In some embodiments, since the object to be restored may be one, the object to be restored includes the referenced object to be referenced by the current referencing object.
[0104] So, if Fig. 6A The figure is an optional flow chart of the object reference method provided in the embodiment of the present application. Figure 5 and Fig. 6A As shown, step S4021 can also be implemented by the following steps:
[0105] Step S601, determining whether the object to be referenced by the current reference object is a released referenced object in the preset storage unit.
[0106] Step S602: when referencing the released referenced object, determining from the preset storage unit that the referenced released referenced object is the object to be restored.
[0107] Here, since there is only one object to be restored, when restoring the released referenced object, only the referenced object to be referenced by the current reference object is restored. Then, when determining the object to be restored, the referenced released referenced object is determined as the object to be restored. In this way, when restoring the object to be restored, only the referenced released referenced object is restored.
[0108] For example, the preset storage unit includes the released referenced objects A2, B2, and C2. If there is currently a referenced object D2 that references the referenced object B2, it is necessary to restore the released referenced object B2 from the preset storage unit, that is, when the released referenced object is restored, only the released referenced object B2 is restored.
[0109] In an embodiment of the present application, by determining the referenced released referenced object as the object to be restored, only the released referenced object referenced by the referencing object is restored, thereby realizing the above-mentioned first recovery method for restoring the released referenced object: that is, only the released referenced object that is referenced again and stored in the preset storage unit is restored.
[0110] In some embodiments, since there may be multiple objects to be restored, the objects to be restored may include, in addition to the referenced objects to be referenced by the current referencing object, other objects that are not to be referenced by the current referencing object.
[0111] So, if Figure 6B The figure is an optional flow chart of the object reference method provided in the embodiment of the present application. Figure 5 and Figure 6B As shown, step S4021 can also be implemented by the following steps:
[0112] Step S611, determining whether the object to be referenced by the current reference object is a released referenced object in the preset storage unit.
[0113] Step S612: when the released referenced object is referenced, the referenced released referenced object and the released referenced object with a preset priority are determined as the objects to be restored from the preset storage unit.
[0114] Here, since there are multiple objects to be restored, when restoring the released referenced objects, not only the referenced objects to be referenced by the current referenced object are restored, but also other objects not to be referenced by the current referenced object are restored. Then, when determining the objects to be restored, the referenced released referenced objects and other released referenced objects not referenced by the current referenced object are determined as the objects to be restored. In this way, when restoring the objects to be restored, the referenced released referenced objects and the unreferenced released referenced objects can be restored at the same time.
[0115] In some embodiments, the other objects that are not to be referenced by the current reference object may have a preset priority, for example, the priority of the other objects that are not to be referenced by the current reference object is higher than the referenced object to be referenced by the current reference object. That is to say, while restoring the referenced object to be referenced by the current reference object, other released referenced objects with a higher priority than the referenced object itself are also restored at the same time.
[0116] For example, the preset storage unit includes the released referenced objects A3, B3 and C3, wherein the priority of the released referenced objects A3, B3 and C3 is in the order of A3>B3>C3 from high to low. If there is currently a referenced object D3 that wants to reference the referenced object B3, it is necessary to restore the released referenced object B3 from the preset storage unit, but while restoring B3, A3, which has a higher priority than B3, will be restored at the same time, that is, when the released referenced objects are restored, A3 and B3 are restored at the same time.
[0117] In the embodiment of the present application, by determining the referenced released referenced object and other released referenced objects with higher priority than the referenced object itself as the objects to be restored, not only the referenced object itself to be referenced by the current referenced object is restored, but also other released referenced objects with higher priority than the referenced object itself are restored at the same time, that is, the above-mentioned second recovery method for restoring the released referenced objects is realized: while restoring the object that is referenced again, some other objects are also restored.
[0118] The following is an explanation of an exemplary application of the embodiments of the present application in a practical application scenario.
[0119] Based on the problems existing in the object reference method in the related art, the embodiment of the present application provides another object reference method, which introduces an intelligent reference method. As a new reference method, the intelligent reference method can intelligently release some low-priority referenced objects when the memory reaches the upper limit, and at the same time ensure that the referenced objects will be regenerated when used later, so that developers can avoid memory leaks and simplify their requirements for code memory management. Developers can debug user local applications in the cloud.
[0120] The key technologies to be solved in the embodiments of the present application include: adding application programming interfaces (APIs) and Class classes related to smart references to the expansion library of the open jdk software through a smart reference library to implement a callable smart reference class; improving the current mainstream JVM virtual machine through an intelligent garbage collector to increase its judgment on the cleaning of objects with smart references.
[0121] The key technologies of the embodiments of the present application are described in detail below.
[0122] When developers are faced with tight memory conditions or when memory is difficult to manage, they can change the references of some objects from strong references to smart references in the embodiments of this application, without having to worry about the life cycle management of the referenced objects. When the number of smart reference objects (i.e., referenced objects) referenced by the smart reference method reaches the upper limit of memory, the GC of the smart garbage collector will be triggered, and the smart garbage collector will release these smart reference objects according to their priority until the memory returns to a reasonable range. In other words, the smart reference method does not take into account the life cycle of the object. When the memory reaches the upper limit, it is only recycled in sequence according to the priority of the smart reference object.
[0123] When releasing a smart reference object, it will be backed up, that is, serialized to the hard disk. If the program uses the smart reference object again later, the JVM will read the serialized file in the hard disk and generate the smart reference object through deserialization, so that the program can continue to use the smart reference object seamlessly.
[0124] Developers no longer need to worry about the life cycle of objects and check for memory leaks. They only need to use smart references and set recycling priorities for smart reference objects. There is no need to set priorities, and the smart recycling algorithm can dynamically adjust the priority to make recycling more efficient.
[0125] Figure 7is a schematic diagram of the structure of the intelligent reference device provided in an embodiment of the present application, wherein the object reference method may be an intelligent reference method, and the object reference device may be an intelligent reference device, such as Figure 7 As shown, the intelligent reference device 700 includes: a Java intelligent reference library 701 and a Java intelligent garbage collector 702.
[0126] based on Figure 7 Intelligent citation devices such as Figure 8 FIG. 1 is a schematic diagram of an implementation flow of an intelligent citation method provided in an embodiment of the present application, wherein the method comprises the following steps:
[0127] In step S801 , the developer 80 develops a program and writes a Java program with a smart reference method to obtain an application 81 .
[0128] Step S802, when smart reference is executed in the application, the Java smart reference library 70 is called to store the smart reference object.
[0129] Step S803, passing the ID of the smart reference object to the JVM virtual machine.
[0130] Step S804, when the memory reaches the upper limit, the Java intelligent garbage collector 702 recycles and cleans the memory 82 and recycles the intelligent reference objects.
[0131] Figure 8 As shown in FIG. 1 , for smart reference objects A, B and C, the Java smart garbage collector recycles smart reference objects A and B and retains smart reference object C.
[0132] Step S805 , the Java intelligent garbage collector serializes the recovered intelligent reference objects A and B and stores them in the hard disk 83 .
[0133] The intelligent reference method provided by the embodiment of the present application assumes that there are three intelligent reference objects A, B and C in the application. When the memory is full, the Java intelligent garbage collector uses an intelligent algorithm to calculate which intelligent reference objects in the latest program have a high probability of use and which have a low probability of use, that is, the cleaning priority of hot objects is lower than that of objects that have not been used recently. Assuming that the Java intelligent garbage collector obtains a higher cleaning priority for A and B than C, and cleaning A and B at the same time can meet normal memory usage, A and B are serialized to the hard disk, and the intelligent reference objects A and B are cleaned up in the memory at the same time.
[0134] When the application executes A or B again, the Java intelligent garbage collector will be called to recover A or B. Fig. 9FIG. 1 is a schematic diagram of an implementation process of restoring an object in the intelligent reference method provided in an embodiment of the present application, wherein the restoration process includes the following steps:
[0135] Step S901 , the developer 80 develops a program and writes a Java program with a smart reference method to obtain an application 81 .
[0136] Step S902, when the smart reference is executed again, the Java smart reference library 701 is called to obtain the smart reference object.
[0137] Step S903, passing the ID of the smart reference object to the JVM virtual machine.
[0138] Step S904 , if the referenced smart reference object is an object recycled by the Java smart garbage collector 702 , then the serialization information of the smart reference object is read from the hard disk 83 .
[0139] Step S905 , the Java intelligent garbage collector 702 deserializes the intelligent reference object and generates a referenced object in the memory 82 .
[0140] In the embodiment of the present application, when the application executes to the released smart object, the Java smart garbage collector will be called to recover the smart reference object. In some embodiments, the recovery can be not to recover what is missing, but to use an intelligent algorithm to estimate the hotspot smart references that will be used in the recent code area, and deserialize the objects that have been released by these smart references, thereby avoiding the inefficient process of needing to recover once every execution.
[0141] The object reference method provided in the embodiment of the present application can reduce the developer's work on memory management, so that the application developer can devote more wholeheartedly to business development. Compared with the problem that the pointer in the related art needs to be released manually, the related art increases the workload of programmers and the difficulty of code maintenance. In addition, the Java technology in the related art adopts a garbage collection mechanism to alleviate the programmer's management of memory to a certain extent, but in some scenarios, the programmer's erroneous strong reference will still cause the memory to be unable to be released. In mobile applications, due to limited memory, there are still many requirements for Java programmers to manage memory. The object reference method in the embodiment of the present application can completely liberate the programmer's management of memory through intelligent reference, reducing the development threshold and code maintenance.
[0142] In addition, the object reference method provided in the embodiment of the present application can also solve the memory leak problem that occurs in some applications, because memory leaks are sometimes particularly hidden and difficult to find. Through the smart reference method, even if a memory leak occurs, it will not cause the program to crash, because the smart reference object will be released when the memory is too high.
[0143] The following is a description of an exemplary structure of the object reference device 255 provided in the present application as a software module. In some embodiments, Figure 2 As shown, the software modules stored in the object reference device 106 of the memory 250 may include:
[0144] The acquisition module 1061 is used to acquire at least one referenced object referenced by a reference object of a program running on the virtual machine, and obtain a referenced object set;
[0145] The release module 1062 is used to release the referenced objects in the referenced object set in order of priority from low to high when the memory occupied by the referenced object set reaches a threshold.
[0146] In some embodiments, the apparatus further comprises:
[0147] The determination module is used to determine the priority of the corresponding referenced object according to the frequency of reference of each referenced object.
[0148] In some embodiments, the apparatus further comprises:
[0149] The storage module is used for storing the released referenced objects in the referenced object set into a preset storage unit through serialization processing after the referenced objects in the referenced object set are released.
[0150] In some embodiments, the apparatus further comprises:
[0151] The recovery module is used to restore the released referenced object to the memory of the electronic device when the released referenced object is referenced.
[0152] In some embodiments, the recovery module is further configured to:
[0153] When the released referenced object is referenced, the object to be restored is determined from the preset storage unit; the object to be restored is deserialized; and the deserialized object to be restored is stored in the referenced object set, so as to restore the released referenced object to the memory of the electronic device.
[0154] In some embodiments, the recovery module is further configured to:
[0155] When the released referenced object is referenced, the referenced released referenced object is determined from the preset storage unit as the object to be restored.
[0156] In some embodiments, the recovery module is further configured to:
[0157] When the released referenced object is referenced, the referenced released referenced object and the released referenced object with a preset priority are determined as the to-be-restored objects from the preset storage unit.
[0158] It should be noted that the description of the device of the embodiment of the present application is similar to the description of the above method embodiment, and has similar beneficial effects as the method embodiment, so it is not repeated. For technical details not disclosed in the embodiment of the device, please refer to the description of the method embodiment of the present application for understanding.
[0159] The present application embodiment provides a storage medium storing executable instructions, wherein the executable instructions are stored. When the executable instructions are executed by a processor, the processor will execute the method provided by the present application embodiment, for example, Figure 3 The method shown.
[0160] In some embodiments, the storage medium can be a ferroelectric random access memory (FRAM), a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a flash memory, a magnetic surface memory, an optical disk, or a compact disk read-only memory (CD-ROM); it can also be various devices including one or any combination of the above memories.
[0161] In some embodiments, executable instructions may be in the form of a program, software, software module, script or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), 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.
[0162] As an example, executable instructions may, but need not, correspond to a file in a file system, may be stored as part of a file that stores 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 storing one or more modules, subroutines, or code portions).
[0163] By way of example, executable instructions may be deployed to be executed on one computing device, or on multiple computing devices located at one site, or on multiple computing devices distributed across multiple sites and interconnected by a communication network.
[0164] The above is only an embodiment of the present application and is not intended to limit the protection scope of the present application. Any modifications, equivalent substitutions and improvements made within the spirit and scope of the present application are included in the protection scope of the present application.
Claims
1. An object reference method, characterized in that: include: Obtain at least one referenced object referenced by a reference object of a program running on the virtual machine to obtain a referenced object set; When the memory occupied by the referenced object set reaches a threshold, the referenced objects in the referenced object set are released in order of priority from low to high; When the released referenced object is referenced, the referenced released referenced object and the released referenced object with a preset priority are determined as objects to be restored from a preset storage unit; the released referenced object with the preset priority is an object that is not to be referenced by the current referenced object, and the priority of the object that is not to be referenced by the current referenced object is higher than the priority of the referenced object to be referenced by the current referenced object; Deserializing the object to be restored; The deserialized object to be restored is stored in the referenced object set, so as to restore the released referenced object to the memory of the electronic device.
2. The method according to claim 1, characterized in that: The method further comprises: According to the frequency of each referenced object being referenced, the priority of the corresponding referenced object is determined.
3. The method according to claim 1, characterized in that The method further comprises: After the referenced objects in the referenced object set are released, the released referenced objects are stored in a preset storage unit through serialization processing.
4. The method according to claim 1, characterized in that: The method further comprises: When the released referenced object is referenced, the referenced released referenced object is determined from the preset storage unit as the object to be restored.
5. An object reference device, characterized in that: include: An acquisition module, used to acquire at least one referenced object referenced by a reference object of a program running on the virtual machine, and obtain a referenced object set; A release module, used for releasing the referenced objects in the referenced object set in order of priority from low to high when the memory occupied by the referenced object set reaches a threshold; A recovery module, used for, when referencing the released referenced object, determining the referenced released referenced object and the released referenced object with a preset priority from a preset storage unit as objects to be recovered; the released referenced object with a preset priority is an object that is not to be referenced by the current referenced object, and the priority of the object that is not to be referenced by the current referenced object is higher than the priority of the referenced object to be referenced by the current referenced object; Deserialize the object to be restored; store the deserialized object to be restored in the referenced object set, so as to restore the released referenced object to the memory of the electronic device.
6. An object reference device, characterized in that: include: A memory for storing executable instructions; A processor, configured to implement the method according to any one of claims 1 to 4 when executing the executable instructions stored in the memory.
7. A storage medium, characterized in that: Executable instructions are stored, which are used to cause a processor to execute and implement the method described in any one of claims 1 to 4.
Citation Information
Patent Citations
Abstracted managed code execution
US20060101410A1
Apparatus and method for managing resource on java environment
US20070226282A1