简体   繁体   中英

Which type of reference to use in Java?

I have ready some questions here about the use of WeakReference , SoftReference etc.. However, it is still not clear to me what should I use in the following case:

Consider a master class called Manager . There is only one instance of it for the entire application and it contains a lot of other classes (call them WorkerA , WorkerB ...and so on) as members:

public class Manager {
     private final WorkerA wA;
     private final WorkerB wB;
     ...
     public Manager()
     {
         wA = new WorkerA(this);
         ...
     }
}

so the Manager can anytime reach its Workers and call their methods. However, sometimes even the workers have to communicate back to their manager, so all of them contain the same reference to the only instance of the manager:

class WorkerA {
   private final Manager mgr;

   public WorkerA(Manager mgr)
   { this.mgr = mgr; }
}

The lifetime of the Manager = lifetime of the application, it is created once at the beginning and disposed at exit. The Workers are never instantiated anywhere else than inside the manager's constructor.

How is keeping of these cross-references memory demanding? (My app is an Android service which is supposed to run for long time). Is there a better solution with the same functionality?

The cost of a field pointing holding a reference to another object is not significant.

The cost of a field holding a reference will be 64bits on a 64bit cpu per field, less on a 32 bit cpu or when compressed pointers are being used by the virtual machine. There will also be a very mild cpu cost when the GC runs. This is because the cost of a GC cycle is related to how many live objects exist and the links between them. However, the cost increases both for memory usage and cpu when using soft/weak/phantom references. This is because they require extra object allocations which are larger than a field and they require special processing during the GC loop which costs cpu time.

The time to use the special weak/soft etc references is when you intend to drop the objects at some point and the normal GC life cycle is not enough. Since you keep the objects for the life of the app, then your approach is already sound.

As well as making the Manager class a singleton class I would not use soft or weak references for the Worker objects. The last thing you want is for a Worker object's memory to be reclaimed by the garbage collector when you least expect it (ie when you invoke that object to do some work!). With ordinary Java references (ie. strong references) this will not happen. The different types of references mentioned here only determine how those reference types interact with the garbage collector: weak references are eagerly reclaimed by the garbage collector whereas soft ones are less eagerly reclaimed.

Since you have also now specified that the Manager is responsible for the lifetime of the Worker objects then using weak/soft references is even more of a mute point. You only use those type of references when you want to the GC to determine whether a particular objects memory can be reclaimed or not rather than manually trying to do determine that yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM