GithubHelp home page GithubHelp logo

mtumilowicz / phantom-reference Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 55 KB

Basic example of phantom reference in Java.

Java 100.00%
phantom-references references java java-core java-concepts

phantom-reference's Introduction

phantom-reference

The main goal of this project is to give simple example of Phantom References in java.

reference types

  1. Strong Reference - any object with an active strong reference to it, will never be garbage collected.

  2. Soft Reference - the garbage collector may optionally choose to reclaim the memory occupied by the employee object.

    SoftReference<X> ex = new SoftReference<>(x);
    

    Remark: Garbage collector will always reclaim the memory of objects that have only soft references pointing to them before it throws an OutOfMemory Error.

  3. Weak Reference - an object that only has a week reference is eligible for garbage collection.

    WeakReference<X> ex = new WeakReference<>(x);
    

    Remark: WeakHashMap
    Remark: Lapsed listener problem

  4. Phantom Reference - phantom reference objects are enqueued after the collector determines that their referents may otherwise be reclaimed.

    final ReferenceQueue<X> queue = new ReferenceQueue<>();
    PhantomReference<X> ex = new PhantomReference<>(x, queue);
    

    Remark: It is used to determine when an object was removed from the memory which helps to schedule memory-sensitive tasks. For example, we can wait for a large object to be removed (reference will appear on the queue) before loading another one.
    Remark: Calling a get() on the Phantom reference always returns null.

description

Take a look at comments in Main:

  1. We create list of large objects.
  2. We create list of phantom references of large objects (with shared queue).
  3. Then we make large objects eligible for garbage collecting, by nulling reference to list:
    largeObjects = null;
    
  4. Then we notify garbage collection:
    System.gc();
    
  5. We wait for garbage collector to remove all large objects - we are reading list of phantom references till all are queued:
    reference.isEnqueued()
    
  6. At the end we are reading the queue, printing all references and clearing them:
    referenceFromQueue.clear();
    

phantom-reference's People

Contributors

mtumilowicz avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.