Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Java object reference and JVM automatic memory Management

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to parse Java object references and JVM automatic memory management, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Parsing Java object references and JVM automatic memory management [@ more@] object references API is newly defined in JDKTM1.2. This application programming interface allows applications to interact with JVM's memory manager in the form of object references. When an application needs to manage a large number of memory objects or delete existing objects before a new Java object is created, the Java object reference application design interface has considerable use, such as:

● Web-based applications often require a large number of pictures to be displayed, and when users leave a Web page, they are often not sure whether they can be returned smoothly. In such a program, applying the Java object reference API creates an environment in which the memory manager creates objects when heap memory runs to a minimum. When the user returns, the application reloads the image that has been created.

The ● application object reference queue can create an environment where the application is notified when an object is obtained through an object reference. The application can then clear the related objects and legalize them in the memory manager.

How the memory manager works

The following first describes how the memory manager works when reference objects are not embedded, and then discusses what happens to the Java heap after reference objects are added.

The role of the memory manager is to identify objects that are no longer used in the program and reclaim their memory.

A Java application consists of a series of threads, each of which executes a series of methods, and each method references an object through parameters or local variables. These references are part of the reference collection and go directly into the application. In addition, the reference collection includes static reference variables defined in the class library, as well as references obtained through the Java local interface (JNI) API. All reference objects in the reference collection can be obtained by the current application without having to be recycled. Similarly, these objects may contain references to other objects, can also be obtained by the application, and so on. Other objects in the Java heap are considered unobtainable, and all these unobtainable objects are legal in memory management. If an unobtainable object uses the finalize () method, the task is left to the finalizer called by the object. During memory collection, unavailable objects that do not have finalizers and objects that have called finalizers are simply reclaimed.

The algorithm of memory recovery is constantly changing, and the common aspect is to identify the available objects from the reference set and to reclaim the memory space occupied by other objects.

References after the addition of reference objects differ from regular references in that references in reference objects are handled specifically by the memory manager. Reference objects encapsulate references to other objects, which we call indication objects. When the reference object is created, the indication object of the reference object is defined.

Depending on the application requirements, the object can be any combination of strong reference (strong references), secondary reference (soft references), weak reference (weak references), and virtual reference (phantom references). To determine the availability of objects, the JVM memory manager searches all paths to objects in the heap from the reference collection. When any path to an object does not contain a reference object, the object is said to have strong acquisition ability; when the path contains one or more reference objects, according to the type of reference objects queried by the memory manager, they are classified as secondary acquisition, weak acquisition and virtual acquisition, respectively.

In addition, the reference object queue (java.lang.ref.ReferenceQueue) is defined in the object reference API, which is a simple data structure for the memory manager to manage the reference object. It is worth noting that when defining reference objects, phantom reference objects are required to be generated in a queue of reference objects, while soft reference and weak reference objects do not have this restriction, such as:

ReferenceQueue queue = new ReferenceQueue ()

PhantomReference pr = new PhantomReference (object, queue)

An example of Soft References application

Let's take the use of soft references in web-based applications as an example to illustrate how Java object references interact with JVM's memory manager.

When a user opens a web page, the applet code gets the picture and displays it. If the soft references of the picture object is also created in the code, the memory manager chooses whether the memory allocated by the picture is reclaimed when the user leaves the web page. When the user returns to the web page, using the SoftReference.get method in the applet code will get a message about whether the picture still exists in memory. If the picture is not created in the memory manager, it will be quickly displayed on the web page; otherwise, the applet code will retrieve it.

The following is the complete source code for Example.java.

Import java.awt.Graphics;import java.awt.Image;import java.applet.Applet;import java.lang.ref.SoftReference;public class Example extends Applet {SoftReference sr = null; public void init () {System.out.println ("Initializing");} public void paint (Graphics g) {Image im = (sr = = null)? Null: (Image) (sr.get ()); if (im = = null) {System.out.println ("Fetching image"); im = getImage (getCodeBase (), "yundong.gif"); sr = new SoftReference (im);} System.out.println ("Painting"); g.drawImage (im, 25,25, this); g.drawString ("Beauty of Sports", 2020); im = null / * Clear the strong reference to the image * /} public void start () {System.out.println ("Starting");} public void stop () {System.out.println ("Stopping");}}

In the above code, the object image is a picture object that is passed to a SoftReference object sr. Where the image object is the indicator object of sr, and the reference range in sr is from soft reference to image.

Weak References analysis

For a stable object, such as a thread class object, it is ideal to apply weak references in your program when you need to get external data. If the weak reference of a thread is created using the reference queue, the application is notified when the thread no longer has strong acquisition capabilities, and according to this notification, the application can perform the cleanup of the relevant data objects.

When the memory manager does not find strong references and soft references, we say that the object has weak acquisition capabilities, that is, it contains at least one weak reference in the path to the object. After the weak references in the program is cleared for a period of time, the weak acquisition object is collected by the finalizer. It can also be seen that the difference between soft reference and weak reference is that when applying soft reference, the memory manager uses the algorithm to determine whether to create a weak acquisition object, while when applying weak reference, the memory manager must create a secondary acquisition object.

Reference object chain

When the path to an object contains multiple reference objects, it forms a chain of reference objects. The memory manager processes reference objects in the order from strong to weak, including Soft references, Weak references, Finalization, Phantom references and creating objects.

When the memory manager does not find the first three object references, we say that the object has the ability of virtual fetching, that is, it contains at least one phantom reference in the path to the object. The virtual reference object is collected directly by the finalizer and not recreated. When the memory manager finds that there is only phantom references, the object will be in a waiting phantom reference state, and the application notifies the reference queue, then calls the clear () method on the virtual reference object, sets its reference domain to null, and finally performs a collection cleanup task on the non-accessible object.

In general, objects have the same acquisition capabilities as the weakest connectors in the direct path of the reference object collection. From this, we can see that:

Virtual reference objects have strong acquisition ability, and other objects have virtual acquisition ability.

(B) both virtual reference objects and weak reference objects have strong acquisition ability, so secondary reference objects and object collections have the ability to obtain.

(C) the virtual reference object, the weak reference object and the secondary reference object all have strong acquisition ability, so the object collection has the secondary acquisition ability.

Using the reference object API in the program by ● can not only control the memory manager to some extent and realize automatic memory management, but also improve the stability and security of the program.

The acquisition ability of each object in the ● reference object chain is related to the whole chain.

After reading the above, do you have any further understanding of how to parse Java object references and JVM automatic memory management? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report