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

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -