Java learning strong references, weak references, soft references and JVMs

Source: Internet
Author: User

1, Java Memory management is divided into memory allocation and memory recovery, do not need the programmer's responsibility.

2, garbage collection mechanism is mainly to see if the object has a reference to the object.
References to Java objects include
Strong references
Soft references
Weak references
Virtual reference

3. Strong references
is to create an object and assign the object to a reference variable.
Strong references have reference variables that are never garbage collected when they are pointed to. Even when the memory is low.
4. Soft reference
Soft references are implemented through the SoftReference class.

Soft-referenced objects are not much different from strong references when the system has sufficient memory, but the soft-referenced objects are reclaimed when memory is low.

Many objects, but only one reference points to them (can be specified separately)

public static void Main (string[] args) {

//Create soft reference array
softreference<person> [] p = new softreference[100];//100000
//Assigned value
for (int i = 0; i< p.length; i++) {
P[i] = new softreference<person> (New person ("name" +i, i));
}
  
//Test
System.out.println (P[1].get ());
System.out.println (P[4].get ());
//Notification system for recycling
System.GC ();
system.runfinalization ();
  
System.out.println ("---------------");
System.out.println (P[1].get ());
System.out.println (P[4].get ());
}
 

When the system memory is sufficient, the system does not make a soft reference memory recovery,
Soft-referenced objects are reclaimed when the system is low.

Soft reference Reference object when forcing heap memory to 1m with java-xmx1m-xms1m softreferencetest command
will be recycled. (10,000 objects can be created)


5, WeakReference
Weak references are implemented by the WeakReference class.

Public static void Main (string[] args) {
string str = new String ("Java handout");
//String str = "Java handout"; This creation is in a constant pool
//Create a pointer to str object such as Reference object
weakreference<string> WR = new weakreference<string> (str);
  
str =null;
//Output
System.out.println (Wr.get ());//java Handout
//Mandatory garbage collection
System.GC ();
System.out.println (Wr.get ());//null
 }

Weak references have very strong uncertainties. Because garbage collection reclaims weakly referenced objects every time.

6. Virtual Reference
Soft and weak references can be used alone, virtual references cannot be used alone, and virtual references are garbage-collected for tracking objects.
State, the program can detect if the virtual reference queue associated with a virtual reference already contains the specified virtual reference, thereby understanding
Whether the virtual reference object is about to be reclaimed.
Phantomreference Object Implementation

Virtual references are implemented by the Phantomrefence class, which itself has no effect on the object, like and no application, and the object does not even feel
The existence of a virtual reference, if only one virtual reference exists for an object, then he is similar to no application exists.

Public static void Main (string[] args) {
//Create an object
string str = new String ("Java handout");
//Create a reference queue
referencequeue<string> RQ = new referencequeue<string> ();
//Create a virtual reference that specifies the reference object. Cannot be used alone must associate a reference queue
phantomreference PR = new phantomreference (STR,RQ);
//Cut off strong references
str =null;
//Try to get a virtual reference object
System.out.println (Pr.get ());
  
//garbage collection
System.GC ();
system.runfinalization ();
//Take out the first entry in the queue and compare it to the PR
System.out.println (Rq.poll () ==PR);
  
 }
//null
//true
when a program enforces garbage collection, only the virtual reference reference string object will be garbage collected, and when the referenced object is recycled,
the corresponding reference is added to the associated reference queue.

7, if you use soft references, weak references, virtual references to reference the object, garbage collection can freely release these objects,
You can use these references flexibly if you want to minimize the amount of memory the program occupies in the declaration cycle.

If you use these references, you cannot keep strong references to these objects (forcing references should be null), or you will waste any of the benefits provided by these classes.

Java learning strong references, weak references, soft references and JVMs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.