views:

122

answers:

2

I saw there is a daemon thread running whenever we create a referenced object using any Reference class like

 WeakReference, 
 FinalReference, 
 SoftReference, 
 PhantomReference,
 Referemce

And if we have hierarchal thread structure then at each level there is an extra daemon thread initiated.

+2  A: 

I would expect the overhead to be very small for most applications. Unless you know it is a problem I wouldn't worry about it. I have never seen references show up as an issue in a profiler and I have been using different profilers for 10 years.

Peter Lawrey
Ok. Then I shall certainly try to optimize my apps cache thing to Wekreferences.
DKSRathore
Softreferences can show up. The are reclaimed relatively late and if you have many of them you can run into severe performance problems because they require more than one GC run to be reclaimed.
kohlerm
WeakReferences can be discard very easily and can last only a few seconds or less. SoftReference are better for this, but don't store most of your memory in them as noted by @kohlerm. If the portion of memory for these references is relatively small, so is the overhead.
Peter Lawrey
Using Lost of SoftReferences can make the system unstable as they tend to be all cleared out at once. i.e. Your system can go from running fast to suddenly having no data cached in Softreference.
Peter Lawrey
+1  A: 

The only way I see this becoming a problem is if your number of threads grows well into 2 digits and more.

Very roughly speaking:

  • 10 threads will be next to unnoticeable
  • 100 should be OK, since they're mostly just waiting and chewing up a bit of memory each
  • 1000 will give your system a headache because those resources will be missing elsewhere
  • 10000 will bring your system to its knees, if not outright kill it.
Carl Smotricz
Nice Info. Thanks carl.
DKSRathore