473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

c# object caching and reference counting

Hi,
I need to cache various resource objects such as images for example,
wich might be referenced from multiple objects
(wich could be considered documents for ease of discusion)
and the idea is simply to make sure they arnt duplicated
as they take up considerable memory, so I can look up
to see if an existing copy exists.

however once they are in the cache how can I ensure they get released,
when they are no longer referenced, as now my cache list is referencing
them,
so the GC wont free them.

can I look at the underlying reference count and so release it from my cache
if the count is 1?
I cant seem to find how to do this, at least not inside c#.

I realy dont want to have to keep a track of seperate reference count as
this
is something I hoped id got away from with managed code.

or is there a way to find all existing objects of a certain class?

a couple of alternatives would be

1) to hold a wrapper wich releases the bitmap when it hasnt been used for
ages,
but can reload it when required from the source,
however this needs a reference to the source wich means that the source no
longer gets released
when its dereferenced.

2) to have a root item wich all source documents are referenced from and to
periodically
keep the cache up to date with this. this is probably much like the GC
operation.
however this is probably quite complicated and not all the source types are
under my control.

I thought I heard someone talk about such a cache, however when I tried to
look
there are so many other types of cache, relating to .net assemblies etc.

the images are actually textures for my 3d objects,
but theres other types too wich are large enought o try and avoid
duplicating.

many thanks
Colin =^.^=


Jun 27 '08 #1
5 5471
On May 22, 12:06 pm, "colin" <colin.ro...@ntworld.NOSPAM.comwrote:
Hi,
I need to cache various resource objects such as images for example,
wich might be referenced from multiple objects
(wich could be considered documents for ease of discusion)
and the idea is simply to make sure they arnt duplicated
as they take up considerable memory, so I can look up
to see if an existing copy exists.

however once they are in the cache how can I ensure they get released,
when they are no longer referenced, as now my cache list is referencing
them,
so the GC wont free them.

can I look at the underlying reference count and so release it from my cache
if the count is 1?
I cant seem to find how to do this, at least not inside c#.

I realy dont want to have to keep a track of seperate reference count as
this
is something I hoped id got away from with managed code.

or is there a way to find all existing objects of a certain class?

a couple of alternatives would be

1) to hold a wrapper wich releases the bitmap when it hasnt been used for
ages,
but can reload it when required from the source,
however this needs a reference to the source wich means that the source no
longer gets released
when its dereferenced.

2) to have a root item wich all source documents are referenced from and to
periodically
keep the cache up to date with this. this is probably much like the GC
operation.
however this is probably quite complicated and not all the source types are
under my control.

I thought I heard someone talk about such a cache, however when I tried to
look
there are so many other types of cache, relating to .net assemblies etc.

the images are actually textures for my 3d objects,
but theres other types too wich are large enought o try and avoid
duplicating.

many thanks
Colin =^.^=
You could use a WeakReference to store items in the cache, that way
the images can still be garbage collected as normal.

For example:

public class ImageCache
{
private IDictionary<string,WeakReference_cache = new
Dictionary<string, WeakReference>();
public Image GetImage(string path)
{
WeakReference reference;
if(_cache.TryGetValue(path, out reference))
{
if(reference.IsAlive)
{
return (Image) reference.Target;
}
else
{
// image has been garbage collected
// remove reference from cache
_cache.Remove(path);
}
}

// not in cache, so load
Image image = Image.FromFile(path);
reference = new WeakReference(image);
_cache.Add(path, reference);
return image;
}
}
Jun 27 '08 #2
Perhaps you could try using a WeakReference in the list; you will need
to cast it to image to extract it, but a WeakReference won't prevent GC
- if everything else has let go of the image, then it will be collected,
and the WeakReference will report this when you look at it.

Marc
Jun 27 '08 #3
aha thats super thanks and to mark too :)
Colin =^.^=

"Terry Rogers" <ro**********@gmail.comwrote in message
news:33**********************************@c58g2000 hsc.googlegroups.com...
On May 22, 12:06 pm, "colin" <colin.ro...@ntworld.NOSPAM.comwrote:
>Hi,
I need to cache various resource objects such as images for example,
......
>Colin =^.^=

You could use a WeakReference to store items in the cache, that way
the images can still be garbage collected as normal.

For example:

public class ImageCache
{
private IDictionary<string,WeakReference_cache = new
Dictionary<string, WeakReference>();
public Image GetImage(string path)
{
WeakReference reference;
if(_cache.TryGetValue(path, out reference))
{
if(reference.IsAlive)
{
return (Image) reference.Target;
}
else
{
// image has been garbage collected
// remove reference from cache
_cache.Remove(path);
}
}

// not in cache, so load
Image image = Image.FromFile(path);
reference = new WeakReference(image);
_cache.Add(path, reference);
return image;
}
}

Jun 27 '08 #4


colin wrote:
Hi,
I need to cache various resource objects such as images for example,
wich might be referenced from multiple objects
(wich could be considered documents for ease of discusion)
and the idea is simply to make sure they arnt duplicated
as they take up considerable memory, so I can look up
to see if an existing copy exists.

however once they are in the cache how can I ensure they get released,
when they are no longer referenced, as now my cache list is referencing
them,
so the GC wont free them.

can I look at the underlying reference count and so release it from my cache
if the count is 1?
I cant seem to find how to do this, at least not inside c#.

I realy dont want to have to keep a track of seperate reference count as
this
is something I hoped id got away from with managed code.

or is there a way to find all existing objects of a certain class?

a couple of alternatives would be

1) to hold a wrapper wich releases the bitmap when it hasnt been used for
ages,
but can reload it when required from the source,
however this needs a reference to the source wich means that the source no
longer gets released
when its dereferenced.

2) to have a root item wich all source documents are referenced from and to
periodically
keep the cache up to date with this. this is probably much like the GC
operation.
however this is probably quite complicated and not all the source types are
under my control.

I thought I heard someone talk about such a cache, however when I tried to
look
there are so many other types of cache, relating to .net assemblies etc.

the images are actually textures for my 3d objects,
but theres other types too wich are large enought o try and avoid
duplicating.

many thanks
Colin =^.^=
I wish i had known about this WeakReference class two years ago. I had
implemented a similar class for expirable objects that checked the
"time inserted" before returning the object or null.
Also used a timer to remove the objects from the hashtable based on
inserted time.

Jun 27 '08 #5
On Thu, 22 May 2008 04:47:33 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
Perhaps you could try using a WeakReference in the list; you will need
to cast it to image to extract it, but a WeakReference won't prevent GC
- if everything else has let go of the image, then it will be collected,
and the WeakReference will report this when you look at it.
The one thing I'd be worried about is using WeakReference with classes
that implement IDisposable (e.g. Bitmap). I realize that knowing that the
instance has in fact been garbage-collected goes a long way toward
alleviating that concern, but it seems like you could still theoretically
have unmanaged resources being left unreleased for some time.

Any thoughts on that?

Pete
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Google Mike | last post by:
I need an Application object replacement. I was creating my own using shared memory -- shm* API in PHP. I was doing fine for 6 months until it just got too fat, I guess, and the RH9 Linux server...
5
by: John Marshall | last post by:
Hi, Does anyone see a problem with doing: data = file("tata").read() Each time this is done, I see a new file descriptor allocated (Linux) but not released. 1) Will there ever be a point...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
9
by: Steve Richter | last post by:
I understand the IDisposable interface and how the using instruction will call the Dispose method of an object. What if a reference to an object is passed to a 2nd thread? And the object...
1
by: GeH | last post by:
Is there any possibility to detect if an object is reachable and can be collected by the GarbageCollector? I'm using a cache of weak references to persistent objects. Objects are then accessed...
4
by: Jure Bogataj | last post by:
Hello! I'm using .NET 2005 (C#) and I've come across using statement. It seems ok, I was just wondering one thing. What if I returned object inside using statement using RETURN directive. Is...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.