[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.caching

.NET 2.0 HttpRuntime Cache Expires Objects Immediately and Induces GC

tomkaupe

7/17/2006 5:28:00 PM

With .NET 2.0, the HttpRuntime.Cache is expiring objects within seconds
or milliseconds of the objects being added to the cache with a reason
of "Underused". This occurs regardless of whether sliding or absolute
expiration is used. The system is not resource constrained (in
particular, it is not memory constrained).

I've included the source for a simple console app that can be compiled
in .NET 1.1 or 2.0. When executed, 1000 objects are instantiated and
added to the cache with a sliding expiration of 20 minutes. It includes
a CacheItemRemoved callback method and writes to the event log when an
item is removed with a reason of "Underused".

With .NET 1.1, no objects are removed during the test. The the final
count of objects in the cache is 1000.
With .NET 2.0, most objects are removed from the cache during the
test. The final count is around 125. The first object is not removed
until 30 seconds into the test, but from that point forward, objects
are continuously removed from the cache. Using PerfMon to monitor the
counter .NET CLR Memory > # Induced GC, there are about 16 induced GC
during the test. There were none with 1.1.

I posted this to the MS feedback site as well:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedbac...


<code>


using System;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace CacheTest
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.EventLog.WriteEntry("Test Start",
DateTime.Now.ToString());

CachedObject o;
for (int i = 0; i < 1000; i++)
{
o = new CachedObject();
Put(o);
System.Threading.Thread.Sleep(120);
}

System.Diagnostics.EventLog.WriteEntry("Cache Count",
HttpRuntime.Cache.Count.ToString());
System.Diagnostics.EventLog.WriteEntry("Test End",
DateTime.Now.ToString());
}

private static void OnItemRemoved(string key, object item,
CacheItemRemovedReason reason)
{
if (reason.ToString() == "Underused")
{
System.Diagnostics.EventLog.WriteEntry("Cache",
"Removed: " + key + ":" + DateTime.Now.ToString() + ":" +
HttpRuntime.Cache.Count);
}
}

public static void Put(CachedObject o)
{
CacheItemRemovedCallback callback =
new CacheItemRemovedCallback(OnItemRemoved);

DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
TimeSpan slidingExpiration = TimeSpan.FromMinutes(20);
CacheItemPriority priority = CacheItemPriority.High;

HttpRuntime.Cache.Add(o.ID + " : " +
DateTime.Now.ToString(), o, null, absoluteExpiration,
slidingExpiration, priority, callback);

//System.Diagnostics.EventLog.WriteEntry("Cache Add",
"Count: " + HttpRuntime.Cache.Count);
}

public class CachedObject
{
int id;

public CachedObject()
{
id = DateTime.Now.GetHashCode();
}

public int ID
{
get { return id; }
}
}
}
}

</code>

3 Answers

Alvin Bruney [ASP.NET MVP]

7/17/2006 11:17:00 PM

0

Yes, the cache scavenging mechanism in .NET 2.0 is overly aggressive.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/b...
-------------------------------------------------------


<tomkaupe@gmail.com> wrote in message
news:1153157284.217999.3800@p79g2000cwp.googlegroups.com...
> With .NET 2.0, the HttpRuntime.Cache is expiring objects within seconds
> or milliseconds of the objects being added to the cache with a reason
> of "Underused". This occurs regardless of whether sliding or absolute
> expiration is used. The system is not resource constrained (in
> particular, it is not memory constrained).
>
> I've included the source for a simple console app that can be compiled
> in .NET 1.1 or 2.0. When executed, 1000 objects are instantiated and
> added to the cache with a sliding expiration of 20 minutes. It includes
> a CacheItemRemoved callback method and writes to the event log when an
> item is removed with a reason of "Underused".
>
> With .NET 1.1, no objects are removed during the test. The the final
> count of objects in the cache is 1000.
> With .NET 2.0, most objects are removed from the cache during the
> test. The final count is around 125. The first object is not removed
> until 30 seconds into the test, but from that point forward, objects
> are continuously removed from the cache. Using PerfMon to monitor the
> counter .NET CLR Memory > # Induced GC, there are about 16 induced GC
> during the test. There were none with 1.1.
>
> I posted this to the MS feedback site as well:
>
> http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedbac...
>
>
> <code>
>
>
> using System;
> using System.Text;
> using System.Web;
> using System.Web.Caching;
>
> namespace CacheTest
> {
> class Program
> {
> static void Main(string[] args)
> {
> System.Diagnostics.EventLog.WriteEntry("Test Start",
> DateTime.Now.ToString());
>
> CachedObject o;
> for (int i = 0; i < 1000; i++)
> {
> o = new CachedObject();
> Put(o);
> System.Threading.Thread.Sleep(120);
> }
>
> System.Diagnostics.EventLog.WriteEntry("Cache Count",
> HttpRuntime.Cache.Count.ToString());
> System.Diagnostics.EventLog.WriteEntry("Test End",
> DateTime.Now.ToString());
> }
>
> private static void OnItemRemoved(string key, object item,
> CacheItemRemovedReason reason)
> {
> if (reason.ToString() == "Underused")
> {
> System.Diagnostics.EventLog.WriteEntry("Cache",
> "Removed: " + key + ":" + DateTime.Now.ToString() + ":" +
> HttpRuntime.Cache.Count);
> }
> }
>
> public static void Put(CachedObject o)
> {
> CacheItemRemovedCallback callback =
> new CacheItemRemovedCallback(OnItemRemoved);
>
> DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
> TimeSpan slidingExpiration = TimeSpan.FromMinutes(20);
> CacheItemPriority priority = CacheItemPriority.High;
>
> HttpRuntime.Cache.Add(o.ID + " : " +
> DateTime.Now.ToString(), o, null, absoluteExpiration,
> slidingExpiration, priority, callback);
>
> //System.Diagnostics.EventLog.WriteEntry("Cache Add",
> "Count: " + HttpRuntime.Cache.Count);
> }
>
> public class CachedObject
> {
> int id;
>
> public CachedObject()
> {
> id = DateTime.Now.GetHashCode();
> }
>
> public int ID
> {
> get { return id; }
> }
> }
> }
> }
>
> </code>
>


tomkaupe

7/18/2006 12:43:00 AM

0

It's more than overly aggressive-- it's broken! Immediately removal of
objects is the antithesis of caching behavior. And the frequent GC is
going to impact performance.

I'm hoping to get confirmation from MS of the issue and an ETA for
update. We're not eager to roll our own cache...

wyx2000

9/10/2006 9:55:00 AM

0

It is not really a good experience when I started work on 2.0 and SQL 2005.
Too many problems and very hard to get response.

I have the same problem for a year, and today I solve mine by reset service
broker

Alter database thedb set new_broker

Hope it works for you.

"tomkaupe@gmail.com" wrote:

> It's more than overly aggressive-- it's broken! Immediately removal of
> objects is the antithesis of caching behavior. And the frequent GC is
> going to impact performance.
>
> I'm hoping to get confirmation from MS of the issue and an ETA for
> update. We're not eager to roll our own cache...
>
>