Home  >  Article  >  Java  >  What is the difference between weak reference and soft reference in java

What is the difference between weak reference and soft reference in java

PHPz
PHPzforward
2023-05-03 23:46:051115browse

Difference

1. Only objects with weak references have a shorter life cycle.

2. During the process of scanning the storage area under the jurisdiction of the garbage collector thread, if an object with only weak references is found, the storage will be recycled regardless of whether the current storage space is sufficient. However, the garbage collector is a low-priority thread and will not necessarily find objects with only weak references quickly.

Example

    String str = new String("abc");
    WeakReference<String> weakReference = new WeakReference<>(str);
    str = null;

Note: If the object is used occasionally (rarely) and you want to obtain it at any time during use, but you don’t want to affect the garbage collection of the object, then WeakReference should be used to remember objects.

What are the basic data types of java

The basic data types of Java are divided into:

1. Integer type, used to represent the data type of integer.

2. Floating point type, a data type used to represent decimals.

3. Character type. The keyword of character type is "char".

4. Boolean type is the basic data type that represents logical values.

The above is the detailed content of What is the difference between weak reference and soft reference in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete