java - is it good idea to use WeakReference for local reference ? -
right working on 1 java application. in creating lots of method local objects. using weakreference create objects in method below.
public void method() { while(count < 10000000) { animalbean animal = new weakreference<animalbean>(new animalbean()).get(); //----------- here logic ------------- } }
i have 1 doubts, idea use weakreference local variables ? if no why ?
thanks lot in advance.
here guide understand weak reference
you should think using 1 whenever need reference object, don't want reference protect object garbage collector. classic example cache want garbage collected when memory usage gets high (often implemented weakhashmap). ref
weak reference objects, not prevent referents being made finalizable, finalized, , reclaimed. weak references used implement canonicalizing mappings.
suppose garbage collector determines @ point in time object weakly reachable. @ time atomically clear weak references object , weak references other weakly-reachable objects object reachable through chain of strong , soft references. @ same time declare of formerly weakly-reachable objects finalizable. @ same time or @ later time enqueue newly-cleared weak references registered reference queues.ref
Comments
Post a Comment