0

I'm currently having a problem where one of my .Net based windows services is hogging up too much memory.

I'm almost positive it has to do with a caching implementation where I have decided to use a "database" caching technique and the problem is occurring with how I am initially loading up the cache values when the service starts up.

Here's the concept...

  • Class: Service Operation: Start

  • Class: Cacheloader Operation: LoadCache

  • Class: DataAccessLayer Operation: Store_Cache_in_DB

...and don't ask me why but...

  • A) Cacheloader is "newed" up as a local variable in the Service "start" method.
  • B) DataAccessLayer is static to the service (Singleton via IOC)

So the code kinda looks like this

Service:

start()
{
    _cacheLoader = new Cacheloader(_dataAccessLayer); 
    _cacheLoader.LoadCache();
}

Cacheloader:

LoadCache()
{
    var entities = _dataAccessLayer.FindEntitiesForCache(); 
    _dataAccessLayer.Store_Cache_in_DB(entities); 
}

DataAccessLayer:

Store_Cache_in_DB(List<Entity> entities)
{
    using(someConnection)
    {
        stored entities in datatable
        pass database to sproc that stores each entity
    }       
}   

So, my concern here is with the "entities" that are passed to the static DataAccessLayer object via the Store_Cache_in_DB method. I'm wondering if somehow the garbage collector will not clean these up because somehow they have been referenced with a static class? If this is the case, would it help to assign the entities to a local variable first as so...

DataAccessLayer:

Store_Cache_in_DB(List<Entity> entities)
{
    var cachedEntities = entities;

    using(someConnection)
    {
        stored cachedEntities in datatable
        pass database to sproc that stores each entity
    }       
} 

...hopefully this would solve my problem. If this isn't the reason why my memory consumption is so high, are there any other ideas? Again, I'm sure this caching technique is the perpetrator.

Thanks in advance!

matt_dev
  • 5,176
  • 5
  • 33
  • 44
  • Nope - I don't think the static method is much to do with it. What memory consumption would you consider 'high' and what are the entities? What do they contain, how big is an entity? – Charleh Jun 22 '12 at 21:56
  • My concern would be a method called LoadCache that stores it? Anyway, Hard to tell from what you've posted, but the datatable in the store_cache_in_db would be prime suspect. – Tony Hopkinson Jun 22 '12 at 22:03
  • Make sure you read this great question about DataTable's, the GC and Dispose. http://stackoverflow.com/questions/913228/should-i-dispose-dataset-and-datatable – dash Jun 22 '12 at 22:20
  • Post more code if you want more responses. From what we currently have it could really be anything. – Kenneth Ito Jun 24 '12 at 00:54

1 Answers1

1

If this is the case, would it help to assign the entities to a local variable first as so...

Having a local won't change anything - the same object instance will be reachable by user code.

The only part that might keep this from being garbage collected is what happens here:

using(someConnection)
{
    stored cachedEntities in datatable
    pass database to sproc that stores each entity
}       

If entities are kept in a variable that persists, it will prevent them from being collected later.

If this isn't the reason why my memory consumption is so high, are there any other ideas?

I would recommend running this under a memory profiler, as it will tell you exactly what's holding onto your memory.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373