In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of the JVM3 of the Java interview questions. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Interview question 1: how to judge whether the object is alive or not
For judging whether an object is alive or not, there are mainly two basic algorithms, reference counting and reachability analysis. At present, java mainly adopts reachability analysis algorithm.
1. Citation counting algorithm
The way to judge whether an object is alive or not is to add a reference counter to the object so that the counter value is increased by one whenever a place references it; when the reference fails, the counter value is reduced by one; an object whose counter is zero at any time can no longer be used.
Its advantage is simple implementation and high decision efficiency; the disadvantage is that the algorithm has many exceptions to consider, must cooperate with a lot of additional processing to ensure that it works correctly, and there is a basic problem, that is, it is difficult to deal with circular reference relations.
two。 Reachability analysis algorithm
To put it simply, the principle is to regard the object and its reference relationship as a graph, through a series of root objects called GC Roots as the starting node set, from these nodes, through the reference relationship to search downward, the path of the search process is called reference chain (Reference Chain). If an object and GC Roots are unreachable, that is, there is no reference chain, then it is considered to be a recyclable object. JVM takes the objects being referenced in the virtual machine stack and the local method stack, objects referenced by static properties, and constants as GC Roots.
As shown in the figure, objects object 5, object 6, and object 7 are related to each other, but they are not reachable to GC Roots, so they will be determined to be recyclable objects.
Interview question 2: which objects can be used as GC Roots?
In the Java technology system, the fixed objects that can be used as GC (Garbage Collector) Roots include the following:
The so-called GC (Garbage Collector) roots specifically refers to the root object (GC Root Object) of the garbage collector, also known as the GC garbage collection root (GC Root). GC collects objects that are not GC roots and are not referenced by GC roots.
An object can belong to more than one root,GC root and there are several kinds:
Objects referenced in the virtual machine stack
For example, the parameters, local variables, temporary variables used in the method stack that each thread is called.
Objects referenced by class static properties in the method area
For example, the reference type static variable of the Java class
Objects referenced by constants in the method area
Such as references in string constant pool (String Table)
Objects referenced by JNI (commonly referred to as local methods) in the local method stack
All objects held by the synchronization lock (synchronized keyword)
References within the Java virtual machine
Such as Class objects corresponding to basic data types, some resident exception objects (such as NullPointExcepiton, OutOfMemoryError), system class loaders, and so on.
JMXBean that reflects the internal situation of the Java virtual machine, callbacks registered in JVMTI, local code caching, and so on.
In addition to the fixed GC Roots collection, other objects can be temporarily added to form a complete GC Roots collection according to the garbage collector chosen by the user and the current reclaimed memory area.
Interview question 3: what kind of object references do you know?
The answer comes from the comments section of "Java Core Technology interview lecture 4" at geek time.
In Java language, except for basic data types, all the other references are object references to all kinds of objects. According to the length of their life cycle, Java classifies references into four categories: strong reference, weak reference, soft reference and virtual reference.
1 strong citation
The obj in our typical code Object obj = new Object () is a strong reference. The reference associated with an object created with the keyword new is a strong reference. When JVM runs out of memory, JVM would rather throw an OutOfMemoryError runtime error (OOM) to cause the program to terminate abnormally, rather than solve the problem of running out of memory by randomly recycling living objects with strong references. For an ordinary object, if there is no other reference relationship, as long as it exceeds the scope of the reference or explicitly assigns the corresponding (strong) reference to null, it can be garbage collected. The specific time of collection depends on the garbage collection strategy.
2 soft reference
Soft references are implemented through the SoftReference class.
The lifetime of soft references is shorter than that of strong references. Only when JVM thinks there is not enough memory will try to recycle the object that the soft reference points to: that is, JVM ensures that the object pointed to by the soft reference is cleaned up before throwing the OutOfMemoryError. A soft reference can be used in conjunction with a reference queue (ReferenceQueue), and if the object referenced by the soft reference is reclaimed by the garbage collector, the Java virtual machine will add the soft reference to the reference queue associated with it. Later, we can call the ReferenceQueue.poll () method to check if any of the objects it cares about have been recycled. If the queue is empty, a null is returned, otherwise the method returns a Reference object that precedes the queue.
Application scenarios: soft references are often used to implement memory-sensitive caching. If you still have free memory, you can keep the cache temporarily and clean it up when you run out of memory, thus ensuring that you will not run out of memory while using the cache.
3 weak citation
Weak references are implemented through the WeakReference class.
The lifetime of weak references is shorter than that of soft references. In the process of scanning the memory area under its jurisdiction by the garbage collector thread, once an object with weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient or not. Because the garbage collector is a low-priority thread, weakly referenced objects may not be recycled quickly. A weak reference can be used in conjunction with a reference queue (ReferenceQueue), and if the object referenced by the weak reference is garbage collected, the Java virtual machine adds the weak reference to the reference queue associated with it.
Application scenarios: weak references can also be used for memory-sensitive caching.
4 false quotation
Virtual references, also known as phantom references, are implemented through the PhantomReference class.
You cannot access any properties or functions of an object through a virtual reference. Phantom references simply provide a mechanism to ensure that objects do something after they are finalize. If an object holds only a virtual reference, it can be reclaimed by the garbage collector at any time as if it had no reference at all. Virtual references must be used in conjunction with reference queues (ReferenceQueue). When the garbage collector is ready to recycle an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory.
ReferenceQueue queue = new ReferenceQueue (); PhantomReference pr = new PhantomReference (object, queue)
The program can know whether the referenced object is going to be garbage collected by determining whether a virtual reference has been added to the reference queue. If the program finds that a virtual reference has been added to the reference queue, it can take some program action before the memory of the referenced object is reclaimed.
Application scenario: can be used to track the activity of an object being reclaimed by the garbage collector. A system notification is received when an object associated with a false reference is reclaimed by the garbage collector.
This is the end of the article on "sample Analysis of JVM3 of Java interview questions". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.