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 =^.^= 5 5487
On May 22, 12:06 pm, "colin" <colin.ro...@nt world.NOSPAM.co mwrote:
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<str ing,WeakReferen ce_cache = new
Dictionary<stri ng, WeakReference>( );
public Image GetImage(string path)
{
WeakReference reference;
if(_cache.TryGe tValue(path, out reference))
{
if(reference.Is Alive)
{
return (Image) reference.Targe t;
}
else
{
// image has been garbage collected
// remove reference from cache
_cache.Remove(p ath);
}
}
// not in cache, so load
Image image = Image.FromFile( path);
reference = new WeakReference(i mage);
_cache.Add(path , reference);
return image;
}
}
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
aha thats super thanks and to mark too :)
Colin =^.^=
"Terry Rogers" <ro**********@g mail.comwrote in message
news:33******** *************** ***********@c58 g2000hsc.google groups.com...
On May 22, 12:06 pm, "colin" <colin.ro...@nt world.NOSPAM.co mwrote:
>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<str ing,WeakReferen ce_cache = new
Dictionary<stri ng, WeakReference>( );
public Image GetImage(string path)
{
WeakReference reference;
if(_cache.TryGe tValue(path, out reference))
{
if(reference.Is Alive)
{
return (Image) reference.Targe t;
}
else
{
// image has been garbage collected
// remove reference from cache
_cache.Remove(p ath);
}
}
// not in cache, so load
Image image = Image.FromFile( path);
reference = new WeakReference(i mage);
_cache.Add(path , reference);
return image;
}
}
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.
On Thu, 22 May 2008 04:47:33 -0700, Marc Gravell <ma**********@g mail.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 just started
"losing its memory". I don't know -- perhaps I'm doing something
wrong, perhaps it's my code, or perhaps there's a limitation on what...
|
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 where I
|
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 collector, such as the one used in Java, maintains a record of
whether or not
an object is currentlys being used. An unused object is tagged as garbage,
|
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:
System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
|
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.
Foo = function(type) {
this.num = 0;
this.type = type
this.trigger();
}
Foo.prototype.trigger = function() {
| |
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 contains a handle to an open file. The Dispose( ) method closes
the handle.
How would the Dispose( ) method called from the end of the using block
in...
|
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 using an ObjectID. If the object is still alive, the
object is retrieved from the cache, if not, it will be fetched from a
database and the referece...
|
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 returned object valid or not?
For example:
public FileStream Func1()
{
|
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'
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |