Thursday, March 31, 2011

Velocity CTP2 Serious Memory Bug

When you create an instance of the cachefactory and then don’t use it anymore the memory that was used during the creation of the object is not released. This will have a substantial effect on all web apps or scenarios where a cachfactory might be created multiple times. The symptoms of this will be unusually high memory use one the process and in IIS this will most likely result in your app having to recycle more often since it with overrun its allocated memory more quickly.

The following code will show an increase of about 500MB yes I mean MegaBytes of memory usage!

To duplicate put the following code into your app:

Dim CacheFactory1 As CacheFactory = New CacheFactory()
For i As Int32 = 1 To 1 * (10 ^ 4)
    CacheFactory1 = New CacheFactory()
    CacheFactory1 = Nothing
Next

There are only two workarounds for this.

  1. Velocity team fixes the bug (and I’m sure they will)
  2. You need to use the same cachefactory object on a static method in your app and reference it every time you want to use the cache. (this works but isn’t optimal in my opinion.)

I also have a cachingscope that can be used to wrap your caching methods and will post this on codeplex soon. You can wrap it around your caching methods just like a transaction scope and it will manage the locking and connection for you.

From stackoverflow
  • So where is the question? You should file the bug, and not post this here, as the Velocity team is more than likely monitoring Microsoft Connect for bugs.

  • We have strange situation when periodically (every 3 mins) Put portions of data to Velocity cache - by separate windows service app and detect that in several hours (near 5-10 hours) Memory Usage of DistributedCache.exe is about 2-3 Gb in spite of not big portions of data is put there. Therewith get-cachestatistics returns max used memory size 19 Mb. Cluster ttl =86400, cache has only one host with secondaries=0. Did anyone have such a problem and found solution? Take to account that we always update data in cache via Put storing Cache as static property of DistrCache class.

    Any ideas??

    Thanks.

    Middletone : it's related to what I wrote above. They have a memory leak which occurs when the cache factory doesn't dispose. Toy correct this you have to ensure that only one cachefactory is active and created. once this is done always refer to that static member variable.
    Middletone : see http://www.codeplex.com/CacheScope
  • I've build a scope provider for resolving this issue. You can get the code here.

    http://www.codeplex.com/CacheScope

0 comments:

Post a Comment