In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to quote objects in JVM. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
The size of the 1.Java object
The size of the type of basic data is fixed, so I won't say much here. For non-basic types of Java objects, their size is open to question. In Java, the size of an empty Object object is 8byte, which is simply the size of an object in the heap that does not have any properties. Look.
The following statement:
Object ob = new Object ()
This completes the life of a Java object in the program, but the space it occupies is: 4byte+8byte. 4byte is the space needed to hold references in the Java stack mentioned in the above section. And that 8byte is the information about the objects in the Java heap. Because all Java non-primitive objects need to inherit Object objects by default, no matter what kind of Java object they are, they must be larger than 8byte.
With the size of the Object object, we can calculate the size of other objects.
Class NewObject {
Int count
Boolean flag
Object ob
}
Its size is: empty object size (8byte) + int size (4byte) + Boolean size (1byte) + empty Object reference size (4byte) = 17byte. But because Java allocates memory to an object as an integer multiple of 8, the nearest integer multiple of 8 of 17byte is 24, so the size of this object is 24byte.
You need to pay attention to the size of the basic types of wrappers here. Because this type of wrapper has become an object, they need to be treated as objects. The size of the wrapper type is at least 12byte (declaring at least the space required for an empty Object), and the 12byte does not contain any valid information, and because the size of the Java object is an integral multiple of 8, the size of a base type wrapper class is at least 16byte. This memory footprint is scary, it is N times that of the basic type (N > 2), and some types of memory footprint is even more exaggerated (just think about it). Therefore, wrapper classes should be used as little as possible. After JDK5.0, because automatic type swapping was added, the Java virtual machine optimized its storage accordingly.
two。 Reference type
Object reference types are divided into: strong reference, soft reference, weak reference, virtual reference.
Strong reference: it is the reference generated by the virtual machine when we generally declare that the object is a strong reference. In a strong reference environment, garbage collection needs to strictly determine whether the current object is strongly referenced. If it is strongly referenced, it will not be garbage collected.
Soft references: soft references are generally used as caches. The difference between a soft reference and a strong reference is that when a soft reference is garbage collected, the virtual machine decides whether to reclaim the soft reference based on the remaining memory of the current system. If the remaining memory is tight, the virtual machine reclaims the space referenced by the soft reference; if the remaining memory is relatively rich, it will not be reclaimed. In other words, when an OutOfMemory occurs in a virtual machine, there must be no soft references.
Weak references: weak references are similar to soft references and are used as caches. But unlike soft references, weak references are bound to be recycled when garbage collection is carried out, so their life cycle only exists in one garbage collection cycle.
Virtual references: unlike several other references, virtual references do not determine the life cycle of an object. 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.
As you can see from the above definition, reference types are only related to garbage collector collection rules and jvm memory. When there is enough memory space, you can keep it in memory; if the memory space is still very tight after garbage collection, you can discard these objects. How to judge object collection rules in java virtual machine
3. Citation counting method
Principle: add a reference counter to an object. Whenever there is a reference to it, the counter value is increased by 1; when the reference fails, the counter value is subtracted by 1; an object with a counter of 0 at any time can no longer be used.
Advantages: simple realization and high judgment efficiency.
Disadvantages: it is difficult to solve the problem of circular references between objects. (this method is not selected in mainstream Java virtual machines).
4. Reachability algorithm (root search algorithm)
Principle: through a series of objects called "GC Roots" as the starting point, the search starts from these nodes, and the path taken by the search is called reference chain. When an object is not connected to GC Roots with any reference chain (in graph theory, from GC Roots to this object is unreachable), it is proved that the object is unavailable.
In the figure above, the GC Root reference is not reachable, which represents the object that can be recycled. If the GC Root reference is reachable, the object can still survive.
Objects that can be used as GC Roots include the following:
The object referenced in the virtual machine stack (the local variable table in the stack frame).
The object referenced by the class static property in the method area.
An object referenced by a constant in the method area.
Objects referenced by JNI (commonly known as Native methods) in the local method stack
No matter whether the reference number of the object is judged by the reference counting algorithm, or whether the reference chain of the object is reachable by the root search algorithm, the survival of the object is related to the "reference".
So references to objects are recyclable rules: strong references
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.