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

Talk to the interviewer about JVM, and 99% of him will let you talk about it!

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

When asked about JVM (Java virtual machine), there is usually a 99% chance of asking: how to judge the state of life and death of an object in JVM?

In this paper, let's talk about this problem. There are several algorithms to judge the state of life and death of an object.

1. Reference counter algorithm

The algorithm for determining the survival of an object by referencing the calculator is like this: set a reference counter for each object, and whenever the object is referenced in a place, the counter is increased by 1, on the contrary, minus 1 every time the reference expires.

Advantages: simple implementation and high performance.

Disadvantages: increase and decrease processing frequently consumes cpu calculation, counters take up a lot of bits and waste space, the most important disadvantage is that it can not solve the problem of circular reference.

Because the reference counter algorithm is difficult to solve the problem of circular references, the mainstream Java virtual machines do not use the reference counter algorithm to manage memory.

The result of the operation:

Start: 117m

Running: 96m

End: 119 M

From the results, we can see that the virtual machine does not recycle them because of mutual reference, and it also shows that the virtual machine is not implemented using reference counters.

2. Reachability analysis algorithm

In the mainstream implementations of mainstream languages, such as Java, C #, and even the ancient Lisp, reachability analysis algorithms are used to determine whether an object is alive or not.

The core idea of this algorithm is to search down through a series of "GC Roots" objects as a starting point, and the search path is called "reference chain".

When an object is not linked to the GC Roots with any reference chain, it is proved that the object can be recycled. As shown in the following figure:

Cdn.xitu.io/2019/5/16/16abf61294833294?imageView2/0/w/1280/h/960/format/webp/ignore-error/1 ">

In Java, you can serve as a list of GC Roots objects:

Reference objects in the Java virtual machine stack.

An object referenced by JNI (commonly known as the Native method) in the local method stack.

A reference object to a class static constant in the method area.

A reference object to a constant in the method area.

The relationship between the life and death of objects and references

From the above two algorithms, both the reference counting method and the reachability analysis algorithm are related to the "reference" of the object, which shows that the reference of the object determines the life and death of the object.

What are the references to the object?

Before JDK1.2, the definition of a reference was traditional: if the value stored in data of type reference represents the starting address of another piece of memory, that piece of memory represents a reference.

This definition is pure, but also narrow, in which case an object is either referenced or unreferenced, and there is nothing you can do about the object in between.

After JDK1.2, the reference is extended to divide it into:

Strong reference (Strong Reference)

Soft reference (Soft Reference)

Weak reference (Weak Reference)

Virtual reference (Phantom Reference)

This is the answer to the first question at the beginning of the article. Objects are not born or dead, and they can be retained when there is enough space.

If there is not enough space, discard these objects. The implementation of many caching functions is also consistent with such a scenario.

The strength of strong reference, soft reference, weak reference and virtual reference decreases in turn.

Strong references: references like "Object obj = new Object ()" are ubiquitous in code, and the garbage collector will never recycle the referenced object as long as the strong reference is there.

Soft reference: a reference that is relatively strong and weakens, allows objects to be exempted from garbage collection, and tries to recycle the object that the soft reference points to only when jvm thinks it is out of memory. Jvm ensures that the object pointed to by the soft reference is cleaned up before throwing the OutOfMemoryError.

Weak reference: an unnecessary object, but it is weaker than a soft reference, and the object associated with the weak reference can only survive until the next garbage collection occurs.

Virtual reference: also known as ghost reference or phantom reference, is the weakest reference relationship. It is impossible to obtain an object instance through a virtual reference. There is only one purpose for setting a virtual reference for an object, that is, to receive a system notification when an object is reclaimed by the collector.

Death Mark and Salvation

The unreachable object in the reachability algorithm is not "must die". In order to really declare the death of an object, we have to go through the process of marking at least twice.

If an object does not have a chain of references connected to GC Roots after reachability analysis, it is marked for the first time and filtered if it is necessary for the object to execute the finalize () method.

There are two conditions for executing the finalize () method:

1. The finalize () method is overridden.

2. The finalize () method has not been called before, because the object's finalize () method can only be executed once.

If the above two conditions are met, the object will be placed in the queue of F-Queue and later executed by a virtual machine-built, low-priority Finalizer thread.

The object's "self-salvation"

The finalize () method is the last chance for an object to escape from death, if the object is re-associated with any object on the reference chain in the finalize () method, such as assigning itself (the this keyword) to a class variable or a member variable of an object.

Let's look at the specific implementation code:

Result of execution:

Execute the finalize method

I am Alive

I'm already dead.

As you can see from the result, the finalize () method of any object will only be called by the system once.

The finalize () method is not recommended to save the object for the following reasons:

1. The finalize () of the object can only be executed once.

2. It is expensive to run.

3. Great uncertainty.

4. The calling order of each object cannot be guaranteed.

The following is to share the knowledge content that the architect must know.

The following is only a partial list, and there are ways to obtain the architect's data at the end of the article.

Ali P7 architect: message queuing for core components of distributed architecture

Ali P7 architect: cross-domain single sign-on analysis and project practice

Ali P7 architect: thinking about the design principle of Synchronized from the designer's point of view

Ali P7 architect: interpretation of NIO principle of Communication Foundation of distributed Architecture

Ali P7 architect: the technical master takes you to play with the core idea of Tomcat

Ali P7 architect: handwritten Dubbo Framework based on Netty

Ali P7 architect: the Application and principle of in-depth Analysis of concurrent programming

Ali P7 architect talk: HashMap1.8 source code analysis

Ali P7 architect: exploring the Secret of MySQL Index Mechanism

Ali P7 architect: summary of Redis Internet Application scenarios

Ali P7 architect: talking about micro-service architecture and agile development

Ali P7 architect said: if you want to see the source code always get carsick, methods and skills are the key.

Ali P 7 architect: distributed RPC handwritten implementation.

Ali P7 architect talk: Ali interview p6 must ask concurrent programming principle analysis

Ali P7 architect talk: the interview must be asked JVM

Ali P7 architect talk: summary of Spring essence of P7 interview

Receiving method

The above technologies can be added to the JAVA advanced architecture: in the 705127209 group, there are Ali, Baidu, Dianping, architect Live Guide, Java programmers working in 1, jvm, handwritten springmvc, distributed, high availability, high performance, data structures, and springcloud,springboot access.

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

Internet Technology

Wechat

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

12
Report