5.6 Data types and applications

Source: Internet
Author: User

Basic types and reference types, strong references, and weak references

Data types in Java can be categorized into two main categories: reference type (Reference) and base type (primitive).

1) Basic types are 8 kinds: boolean, char, Byte, short, int, long, float, double.

They all have corresponding wrapper classes , which are reference types , which are: Boolean, Character, Byte, short, Integer, Long, Float, Double.

2) Reference type : All Java objects are accessed through object References, similar to a pointer in C + +, which points to an area in the heap heap where the actual object exists in the heap. For example, declare the following code:

int prim = tennew Integer (10);

The two in-memory layouts are as follows:

java/android There are four types of references, namely: 1. Strong reference- Strong References

One of the most common reference types in actual coding. Common forms such as: a A = new A (); The strong reference itself is stored in the stack memory , and its storage points to the address of the object in memory. In general, when a strong reference to an in-memory object no longer points to it, the garbage collection machine begins to consider a possible garbage collection of this memory. For example, when encoding: a = null, at this point, there is no other reference to the new a object that has just been assigned an address in the heap, and the heap memory is garbage collected when the system is garbage collected.

SoftReference, WeakReference, phantomreference are subclasses of class java.lang.ref.Reference. Reference, as an abstract base class, defines the basic operations of its subclass objects. Reference subclasses have the following characteristics:

1) Reference subclasses cannot be directly created without a parameter, and must create their own subclass objects with at least the strong reference object as the construction parameter ;

2) Because the object is created in 1 with a strong reference object as a construction parameter, the object in the heap memory pointed to by the original strong reference will no longer be directly associated with the strong reference itself, but also with reference to the subclass object of the reference. This link may affect the object's garbage collection. According to different sub-class objects, they have different effects on garbage collection, which indicates that the object (the object in the heap in which the strong reference is pointing), respectively, formed three subclasses, namely SoftReference, WeakReference and Phantomreference.

2. Soft Reference-Soft reference
New A (); SoftReferenceNew softreference<a> (A);

A SoftReference object is created with a strong reference to the object, and the WRA in the stack memory points to the object.

at this point, encode the following: a = null , for the original a The point of A What is the impact of garbage collection on objects?

When a = NULL, the A object in heap memory will no longer have any strong references to it, but at this point the object that exists in the SRA reference points to the A object. This is well understood when the first call to the Sra.get () method returns this indicator object, because the garbage collector is likely to have not yet been garbage collected, at which point the get () is a result. When the program executes System.GC (); After forcing the garbage collection, through Sra.get (), it is found that the indicated a object is still available, stating that the A object is not garbage collected.

So, when does the object indicated by the soft reference start being garbage collected? The following two conditions need to be met:

1. When its indicated object does not have any strong reference to the object pointing to it;

2. When the virtual machine is low on memory.

therefore, a soft reference in disguise extends the time it indicates that the object occupies heap memory until the garbage collector reclaims this heap memory space until the virtual machine has insufficient memory.

3. Weak Reference- Weak references

Similarly, the general use of weak references is as follows:

New A (); WeakReferenceNew weakreference<a> (A);

When no strong references point to this object, What are the characteristics of its garbage collection?

When a = NULL, the A object in heap memory will no longer have any strong references to it, but at this point the object referenced by WRA points to the A object. When the first call to the Sra.get () method returns this indicator object, get () has a result when the garbage collector is most likely not yet garbage collected, and when garbage collection occurs, wra.get () returns NULL, indicating that the object has entered the garbage collection process. Therefore, the characteristics of weak references are summarized as:

  WeakReference does not change the garbage collection time of the original strongly referenced object, and once it indicates that the object does not have any strongly referenced objects, the object enters the normal garbage collection process .

Well, according to this characteristic, there is probably doubt: what is the meaning of weakreference existence?

Its main usage scenario is that there are currently strong references to strong reference objects, and because of the business need to increase the reference to this object, but also do not want to change the garbage collection time of this reference, at this time WeakReference exactly meet the requirements, common in some and life cycle scenarios.

For example: combining static internal classes and WeakReference to address possible Handler in Activity Memory leak issues.

Defining Handler often receives a warning message: ThisHandler class should is static or leaks might occur ...

In general, it is recommended to define handler as an internal static class, and define a reference to the weakreference in this static inner class, because it indicates an external activity object.

Problem Analysis: Activity has its own life cycle, the activity of the new open thread in the process, the user may have pressed the back key, or the system memory is not enough to want to reclaim this activity, due to activity The new thread does not follow What cycle of activity itself, that is, when activity executes OnDestroy, due to the existence of threads and handler Handlemessage, Makes the system want to do this activity memory recycling cannot be implemented, because non-static inner classes implicitly hold the reference to the external class , resulting in a possible memory leak problem.

therefore , when using handler in Activity, it is necessary to define it as a static inner class form, which allows it to decouple from the external class (Activity) and no longer hold references to external classes. At the same time, since Handlermessage in handler generally needs to access or modify the properties of the activity, it is necessary to define a weakreference within handler that points to this activity. So that it does not affect the activity's memory collection at the same time, the properties of the activity can be accessed under normal circumstances.

4.PhantomReference

For SoftReference and WeakReference, there is also a constructor parameter of Referencequeue<t>, when the object indicated by SoftReference or weakreference is indeed garbage collected, Its references will be placed in the referencequeue. Note that, when the get () method of softreference or WeakReference returns NULL, only the object that indicates it has entered the garbage collection process, the object is not necessarily garbage collected. And only if the acknowledgment is garbage collected, if referencequeue, its reference will be placed in Referencequeue.

Compared with softreference or WeakReference, the main differences of Phantomreference are as follows:

    • Phantomreference has only one constructor phantomreference (T referent, referencequeue<? super T> Q), so phantomreference uses Must be combined with referencequeue;
    • The Get () method of phantomreference Returns the result null regardless of whether there is a strong reference to the Phantomreference object.

Virtual references are primarily used to track activities that objects are reclaimed by the garbage collector. One difference between a virtual reference and a soft reference and a weak reference is that the virtual reference must be used in conjunction with the reference queue (Referencequeue). When the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory.

5.6 Data types and applications

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.