Strong, soft, weak and virtual references of Java objects
In versions prior to JDK1.2, when an object is not referenced by any variable, the program can no longer use this object. That is, a program can only use an object if it is reachable. This is just like in daily life, after buying an item from a store, if it is useful, you keep it, otherwise you throw it into the trash can and be collected by the cleaners. Generally speaking, if an item has been thrown into the trash can, it is impossible to pick it up and use it again.
But sometimes the situation is not that simple. You may encounter items that are tasteless and tasteless, and it is a pity to throw them away. This kind of item is no longer useful now. Keeping it will take up space, but it is not cost-effective to throw it away immediately because it may be used in the future. For such dispensable items, a compromise approach is: if there is enough space in the home, keep it at home first. If there is not enough space in the home, even if all the garbage is removed from the home, it still cannot accommodate the indispensable items. If you don’t have enough daily necessities, then throw away these dispensable items.
Starting from JDK version 1.2, object references are divided into four levels, allowing the program to more flexibly control the life cycle of the object. These four levels, from high to low, are: strong reference, soft reference, weak reference and virtual reference.
The quotations introduced earlier in this chapter are actually strong quotations, which are the most commonly used quotations. If an object has a strong reference, it is similar to an essential daily necessities, and the garbage collector will never reclaim it. When there is insufficient memory space, the Java virtual machine would rather throw an OutOfMemoryError error and cause the program to terminate abnormally, rather than arbitrarily recycling objects with strong references to solve the problem of insufficient memory.
If an object only has soft references, it is similar to disposable daily necessities. If there is enough memory space, the garbage collector will not reclaim it. If there is insufficient memory space, the memory of these objects will be reclaimed. As long as the garbage collector does not collect it, the object can be used by the program. Soft references can be used to implement memory-sensitive caching.
3. Weak Reference
Weak references can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the weak reference is garbage collected, the Java virtual machine will add the weak reference to the reference queue associated with it.
As the name suggests, "Phantom Reference" is in name only. Unlike other references, a phantom reference does not determine the life cycle of the object. If an object holds only phantom references, it is as if it had no references and may be garbage collected at any time.
Virtual references are mainly used to track the activities of objects being garbage collected. One difference between virtual references, soft references and weak references is that virtual references must be used in conjunction with a reference queue (ReferenceQueue). When the garbage collector is preparing to recycle an object, if it finds that it still has a virtual reference, it will add the virtual reference to the reference queue associated with it before recycling the object's memory. The program can determine whether the referenced object will 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 necessary actions before the memory of the referenced object is recycled.
Three classes are provided in the java.lang.ref package: SoftReference class, WeakReference class and PhantomReference class, which represent soft references, weak references and phantom references respectively. The ReferenceQueue class represents a reference queue, which can be used in conjunction with these three reference classes to track the activities of the Java virtual machine in recycling the referenced objects. The following program creates a String object, ReferenceQueue object and WeakReference object:
//Create a strong reference
String str = new String("hello");
//Create a reference queue, <String> is a generic mark, indicating that the reference to the String object is stored in the queue
ReferenceQueue<String> rq = new ReferenceQueue<String>();
//Create a weak reference, which references "hello" object, and is associated with the rq reference queue
//<String> is a generic mark, indicating that WeakReference will weakly reference the String object
WeakReference<String> wf = new WeakReference<String>(str, rq);
The above is the content of strong, soft, weak and virtual references of Java objects. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20448
7
13592
4
Recommend several apps to buy mainstream coins in 2025 latest release
Apr 21, 2025 pm 11:54 PM
APP software that can purchase mainstream coins includes: 1. Binance, the world's leading, large transaction volume and fast speed; 2. OKX, innovative products, low fees, high security; 3. Gate.io, a variety of assets and trading options, focusing on security; 4. Huobi (HTX), low fees, good user experience; 5. Coinbase, suitable for novices, high security; 6. Kraken, safe and compliant, providing a variety of services; 7. KuCoin, low fees, suitable for professional traders; 8. Gemini, emphasizes compliance, and provides custodial services; 9. Crypto.com, providing a variety of offers and services; 10. Bitstamp, an old exchange, strong liquidity,
The top ten virtual currency trading platforms (2025 currency circle app latest ranking)
Mar 11, 2025 pm 04:51 PM
Recommended virtual currency trading apps in the top ten: 1. OKX (strong functions, high liquidity); 2. Binance (global leading, huge user base); 3. Gate.io (wide currency, low fees); 4. Huobi Global (old exchange, good reputation); 5. Kraken (high security, preferred by institutional investors); 6. Coinbase (one of the largest exchanges in the United States, user-friendly); 7. KuCoin (fast development, high liquidity).
Top 10 virtual currency trading app rankings in 2025 Global top 10 currency trading platforms
Feb 12, 2025 pm 04:48 PM
The top ten most reliable virtual currency trading platforms in 2025: Binance, OKX, Gate.io, Huobi Global, Bitfinex, Kraken, Gemini, Bybit, KuCoin, Crypto.com. These platforms offer a wide range of virtual currency options, low transaction fees, advanced trading tools and excellent customer support.
Top 10 digital virtual currency trading platforms What are the easy currency trading apps?
Jun 19, 2025 pm 12:03 PM
When choosing a reliable cryptocurrency trading platform, you must give priority to security, fees, currency and functions. The top ten reliable platforms in 2025 include OKX, Binance, Huobi, Coinbase, Kraken, KuCoin, Bitfinex, Gemini, Bitstamp and Crypto.com. They each have their own characteristics. For example, OKX provides a variety of transaction methods and focuses on security; Binance is known for its low handling fees; Coinbase is suitable for beginners; Kraken and Gemini emphasize compliance and security, etc. When choosing, you should consider the following five points: 1. Security: Check whether you have dual-factor certification, cold storage and other measures; 2. Transaction fees: Compare the rates of different platforms and
What platforms can trade virtual coins?
Mar 12, 2025 pm 07:30 PM
This article introduces virtual currency trading platforms with higher comprehensive rankings in the world, including Binance, Gate.io, Ouyi OKX, Coinbase, Kraken, Bitfinex, KuCoin, Bybit and Huobi, and BitMEX. These platforms have their own advantages in terms of transaction depth, user experience, security, compliance, etc. Some focus on currency trading, while others are good at derivative trading to meet the needs of different users. Choosing a suitable platform requires comprehensive consideration based on your own trading experience, risk tolerance and investment preferences. Click to read to learn more about these top virtual currency trading platforms to help you trade digital assets safely and efficiently
Top 10 Currency Trading Platform Virtual Currency Apps Top 10 Reliable and Useful Currency Trading Apps
Feb 17, 2025 pm 03:57 PM
With the thriving virtual currency market, choosing a reliable trading platform is crucial. This article recommends 10 highly respected virtual currency trading platforms, including Binance, Coinbase, FTX, Huobi, OKX, Gemini, Kraken, Bitstamp, Bittrex and KuCoin. Factors such as security, liquidity, expenses, asset selection and user experience are crucial when choosing a trading platform. This article will provide you with authoritative reference and help you find the trading platform that best suits your needs.
Ranking of the top ten currency trading platforms in the currency circle (the top ten virtual currency trading apps recommended in 2025)
Feb 20, 2025 pm 05:57 PM
Recommended best virtual currency trading platforms: Binance: The largest transaction volume, the most money is supported, and the ecosystem is perfect; OKX: Advanced trading engine, rich trading tools, and powerful derivative trading platform; Gate.io: The largest number of cryptocurrencies, convenient user interface, and innovative functions; Bitget: Focus on derivative trading, leading social trading functions and strong liquidity; Coinbase: The largest, most compliant and suitable for beginners in the United States.
Ranking of the top ten formal virtual currency exchange apps in 2025 (digital currency trading platform app list)
Apr 30, 2025 pm 05:39 PM
Ranking of the top ten formal virtual currency exchange apps in 2025: 1. OKX, 2. Binance, 3. Huobi, 4. Coinbase, 5. Kraken, 6. Bitfinex, 7. KuCoin, 8. Gemini, 9. Bitstamp, 10. Poloniex, each performs outstandingly in terms of security, user experience, transaction fees, liquidity, currency richness, professional tools, compliance, privacy protection, leveraged trading, degree of internationalization, customer service, etc.




