References Role in Garbage Collection

References Role in Garbage Collection

In Java, there are four types of references differentiated by the way by which they are garbage collected.

  1. Strong References
  2. Weak References
  3. Soft References
  4. Phantom References

These references play a vital role at the time of garbage collection.

Strong Reference:

This is the default type/class of Reference Object. Any object which has an active strong reference is not eligible for garbage collection. The object is garbage collected only when the variable which was strongly referenced points to null.

For example:?Student s= new Student();

Here s is bound referenced with the new Obj of Student hence this is a strong reference so it is not eligible for garbage collection, To make it available for garbage collection we need to nullify this once our work is done.

s=null;

Now s is available for garbage collection.

=================================================================

Week References:

Weak Reference Objects are not the default type/class of Reference Objects and they should be explicitly specified while using them.

  • This type of reference is used in WeakHashMap to reference the entry objects.
  • If JVM detects an object with only weak references (i.e. no strong or soft references linked to any object), this object will be marked for garbage collection.
  • To create such references?java.lang.ref.WeakReference ?class is used.
  • These references are used in real-time applications while establishing a DBConnection which might be cleaned up by Garbage Collector when the application using the database gets closed.

For example:?Student s= new Student();

WeekReference<Student>weekref=new WeekRefernece(s);

s=null;

After this s of above will be available for the GC, But remember this will be garbage collected only when the memory needed by JVM

you can get obj back by the method

s=weekref.get();

=================================================================

Soft Reference:

In Soft reference, even if the object is free for garbage collection then it also it's not garbage collected until JVM badly needs memory. The objects get cleared from the memory when JVM runs out of memory. To create such references?java.lang.ref.SoftReference ?class is used.

=================================================================

Phantom References:

The objects which are being referenced by phantom references are eligible for garbage collection. But, before removing them from memory, JVM puts them in a queue called ‘reference queue’. They are put in a reference queue after calling finalize() method on them. To create such references?java.lang.ref.PhantomReference ?class is used.

=================================================================

要查看或添加评论,请登录

Aalishan Ansari的更多文章

  • Android Activity Frequently Asked Interview Questions Part-1

    Android Activity Frequently Asked Interview Questions Part-1

    1. What is the activity?/What do you know about the activity? Ans: Activity is one of the building blocks of Android…

    6 条评论
  • MVP Architecture with Example in Android

    MVP Architecture with Example in Android

    In this blog, we will take an example of a login screen in Android and cover up MVP very simply. As shown in the above…

    2 条评论
  • Activity lifecycle callbacks A →B and B →A

    Activity lifecycle callbacks A →B and B →A

    Suppose you are having one more activity called C If you kill Activity C from the task manager so onDestroy(C)…

    1 条评论
  • Fragment to Fragment Communication Using ViewModel

    Fragment to Fragment Communication Using ViewModel

    In MainActivity class MainActivity : AppCompatActivity() { private var _activityMainBinding: ActivityMainBinding?…

    2 条评论
  • S.O.L.I.D Principles (Examples in Kotlin)

    S.O.L.I.D Principles (Examples in Kotlin)

    Lots of developers do the code, but A good developer is one who takes care of the code with respect to many programming…

    1 条评论
  • Design Patterns (Very simple examples)

    Design Patterns (Very simple examples)

    In the software industry, most common problems have common solutions, and that solution follows common types of…

    1 条评论
  • Activity|LaunchMode|With Lifecycle

    Activity|LaunchMode|With Lifecycle

    Before starting development in any platform first, we should understand the ecosystem of the platform. If you are…

    2 条评论
  • OOPs Concepts (Examples in Kotlin)

    OOPs Concepts (Examples in Kotlin)

    Object-Oriented Programming in detail. The Oops concepts are the foundation when you want to create a good application…

  • Rx Android

    Rx Android

    Rx Android is a concept used in android app development to give app better performance. Reactive Extension is a library…

    1 条评论
  • Context/Application Context?

    Context/Application Context?

    Context is the bridge between components. You use it to communicate between components, instantiate components, and…

    1 条评论

社区洞察

其他会员也浏览了