What is weakhashmap — turn

Original address: http://laravel.iteye.com/blog/2303244

Where is the Java weakhashmap? Is it really weak? What are the applicable scenarios of weakhashmap and what should we pay attention to when using it? How do weak and strong references affect Java GC differently? This article will give a clear and concise introduction.

General introduction

At the end of the Java collection framework series, the author intends to introduce a special member: weakhashmap. From its name, it can be seen that it is some kind of map. Its special feature is that the entry in the weakhashmap may be automatically deleted by GC, even if the programmer does not call the remove () or clear () methods.

More intuitively, when using weakhashmap, even if no elements are added or deleted, the following situations may occur:

Encounter such a wonderful phenomenon, do you think users will go crazy? In fact, this feature of weekhashmap is especially suitable for scenes that need caching. In the cache scenario, due to the limited memory, all objects cannot be cached; Object cache hits can improve system efficiency, but cache miss will not cause errors, because it can be retrieved through calculation.

To understand how weekhashmap works, Another concept needs to be introduced: weak references (WeakReference). We all know that memory in Java is automatically managed by GC. GC will automatically determine which objects can be recycled during program operation and release memory at an appropriate time. GC determines whether an object can be recycled based on whether there is a valid reference to the object. If there is no valid reference to the object (basically means that there is no way to access the object), then the object is recyclable. The "valid reference" here does not include weak references. That is, although weak references can be used to access objects, weak references will not be considered during garbage collection, and only the objects pointed to by weak references will still be recycled by GC.

Weak references are used to manage entries inside the weakhashmap. What does the weak reference feature mean on the weakhashmap? Putting a pair of keys and values into the weakhashmap does not prevent the key value from being recycled by the GC, unless there is a strong reference to the key outside the weakhashmap.

The concepts of strong reference and weak reference will be explained in detail later. Here, we only need to know that references in Java are also classified, and different types of references have different effects on GC.

Concrete implementation

The storage structure of the weakhashmap is similar to that of the HashMap, and the reader can do it by himself. I won't repeat it here.

On the management of strong and weak references, the blogger will open a special topic to explain separately.

Weak HashSet?

If you read the previous explanations on map and set, you will ask: since there is weekhashmap, is there weekhashset? The answer is no: (. However, the Java collections tool class provides a solution. The collections. Newsetfrommap (map map) method can wrap any map into a set. You can quickly get a weak HashSet by the following methods:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>