Friday, October 17, 2008

The cost of having a finalizer

It's well-known that having a finalizer is costly. But how much does it cost - to have a finalizer?

Allocation to nothing:
- SlimObject: Operations: 77.880 M/s.
- FinalizableSlimObject: Operations: 1.628 M/s.

Allocation to an array:
- SlimObject: Operations: 6.996 M/s.
- FinalizableSlimObject: Operations: 3.256 M/s.

"Allocation to nothing" here is allocation without storing a reference anywhere. This case must be the most frequent scenario, since most of allocated objects don't survive the next GC in generation 0.

There is no any additional GC cost for SlimObject in this case - there are no references to any of such objects, so garbage collector won't mark any of them, and consequently, won't spent any time on them at all.

But what's with FinalizeableSlimObject?
- It's allocation is costly, since it is registered for further finalization (added into a finalization queue). Registration cost must be similar to getting a lock and adding an object to Hashtable.
- All of such objects are processed (i.e. marked) during the GC
- Then they're removed from finalization queue (~ finding & removing an entry from a Hashtable)
- And added to FReachable queue (cheap, ~ adding to a list).
- They're also processed on heap compaction phase. We get an additional cost proportional to the size of an object here.
- A separate finalization thread will spend some time on them to finalize them. This must be not quite noticeable in this test, since the test have been performed on multicore CPU.
- And finally, if some of them will survive until the next GC in generation 1, we'll will add further overhead similar to described.

So as you see, allocation of short living finalizable object is 50 times more expensive than of the regular one!

Now let's turn to long-living objects: "Allocation to an array" here is allocation with storing a reference to allocated object in array. The test was running for about a second, so there must be several generation 1 GCs. Certainly SlimObject wins here as well (about 2 times), but not as mach as in above case. Why?
- Because all the objects survive many GCs, and GC overhead is the most significant factor for both types of them.
- One more factor slowing down the test with SlimObject is less efficient usage of L1\L2 caches: in first case all the data were "laying" mainly in cache, but here pages must "flow" through the caches. But I suspect this is much less important factor than the first one here.

In any case, probably it's time to finalize some finalizers ;) At least the ones that aren't really necessary.


  1. Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  2. By reading this article I get a lot of lessons and this is very useful . cara menggugurkan kandungan

  3. Good post Jual Obat Aborsi. I learn something new Obat aborsi and challenging on Obat Penggugur Kandungan blogs I stumbleupon every day of Aborsi. It will always be exciting to read content from other writers and use a jual cytotec and Jual obat aborsi cytotec little something from their Apotik aborsi You’ve written nice obat aborsi asli post , I am gonna bookmark this Cara Menghindari penipuan penjual obat aborsi Palsu page , thanks for info. I actually appreciate your own pil peluntur janin position and I will be sure to come back here. Jual aborsi his is a really good post. Must admit that you are among the best bloggers I have obat aborsi read of harga cytotec Thanks for posting this informative article of obat aborsi murah . You may havce a lot of obat aborsi, as well aborsi.
    Thank you for providing Jual obat aborsi information from your Obat Aborsi di Apotik. On of the good Obat penggugur di apotik website in search results obat telat bulan. Well, Thanks a lot of cytotec for such a wonderful post of Penggugur Kandungan, the stuff posted were really obat aborsi 2 bulan interesting and useful.

  4. We are usually available 24/7 and our fast essay writing service team can be contacted and also seek our pay for term papers at any time.

  5. تحميل العاب للكمبيوتر 2019 بروابط مباشره، العاب حرب والعاب سباق والعاب كرة قدم والعاب مغامرات وغيرها من العاب الكمبيوتر المسليه جاهزه للتحميل بشكل مجاني