473,289 Members | 1,829 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,289 software developers and data experts.

Hashtable question

Hi,

I am using a hashtable as a cache. I am only allowed for the cache to take
up a certain amount of memory.

I think I can state how large I want the hashtable to be on creation.

What I need is someway to monitor the size of the hashtable so that as it
nears the maximum size I have set, I can delete some item or empty the table
altogether.

Can anyone tell me how I can implement this?

Also what happens if I try to add items to the table when it has reached its
maximum size?

Thanks In Advance
Macca
Sep 29 '06 #1
4 3728
Hi Vadym,

I will be defining my own cache manager class which will contain a
hashtable.
The question I have is how do i find the size of the hashtable at runtime so
i can control its size, i.e stop it becoming full.

Thanks,

Macca

"Vadym Stetsyak" wrote:
Hello, Macca!

MHi,

MI am using a hashtable as a cache. I am only allowed for the cache to
Mtake
Mup a certain amount of memory.

MI think I can state how large I want the hashtable to be on creation.

MWhat I need is someway to monitor the size of the hashtable so that
Mas it
Mnears the maximum size I have set, I can delete some item or empty
Mthe table
Maltogether.

MCan anyone tell me how I can implement this?

MAlso what happens if I try to add items to the table when it has
Mreached its
Mmaximum size?

If you're under .NET 2.0 have a look at
( http://www.codeproject.com/csharp/GenericCache.asp )

Another way is to implement your own cache manager class. This class
intenally will contain Hashtable. and will control its size.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot
Sep 29 '06 #2
"Macca" <Ma***@discussions.microsoft.comwrote >
I am using a hashtable as a cache. I am only allowed for the cache to take
up a certain amount of memory.
There may be a better options for you: Weak References.

This will allow your cache to grow without bounds so long as there's no
memory pressure. As memory presure builds, the Garbage Collector will
collect the Weak References and your app keeps on going.

You can find more information at:
http://msdn2.microsoft.com/en-us/library/ms404247.aspx
http://msdn2.microsoft.com/en-us/lib...reference.aspx

--
Chris Mullns, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

Sep 29 '06 #3

Chris Mullins wrote:
"Macca" <Ma***@discussions.microsoft.comwrote >
I am using a hashtable as a cache. I am only allowed for the cache to take
up a certain amount of memory.

There may be a better options for you: Weak References.

This will allow your cache to grow without bounds so long as there's no
memory pressure. As memory presure builds, the Garbage Collector will
collect the Weak References and your app keeps on going.

You can find more information at:
http://msdn2.microsoft.com/en-us/library/ms404247.aspx
http://msdn2.microsoft.com/en-us/lib...reference.aspx

--
Chris Mullns, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
I've often contemplated this approach myself... a question though, does
the MS hashtable implementation have some sort of container object? I
suppose even the weak reference object must have some overhead. Either
way - if you use this approach, I suppose you must occaisionally go
through and prune all the empty WeakReferences out of your hashtable...
unless you can bind an event to the Weak Reference and have it remove
itself... but at this point we're getting into the sort of complicated
three-star programming that is best left to C++.

Sep 29 '06 #4

The standard generic dictionary uses KeyValuePair<as it's container.
There's no way I know of to wrap this with a weak reference and have this
"just work". You would need (as you said) some sort of monitor thread to
prune the null references out of the Dictionary.

Poking around the web, it looks like a few people have implemented the
IDictionary interface on a class that is Weak Reference aware. I haven't
used any of them, and have a healthy skepticism for people's ability to
implement complex data structures without bugs.

On the other hand, the Asp.Net Cache is weak reference aware, and it can be
used in any type of application. I've used this as a cache before and had a
good experience.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

<ma***********@gmail.comwrote in message
news:11********************@k70g2000cwa.googlegrou ps.com...
>
Chris Mullins wrote:
>"Macca" <Ma***@discussions.microsoft.comwrote >
I am using a hashtable as a cache. I am only allowed for the cache to
take
up a certain amount of memory.

There may be a better options for you: Weak References.

This will allow your cache to grow without bounds so long as there's no
memory pressure. As memory presure builds, the Garbage Collector will
collect the Weak References and your app keeps on going.

You can find more information at:
http://msdn2.microsoft.com/en-us/library/ms404247.aspx
http://msdn2.microsoft.com/en-us/lib...reference.aspx

--
Chris Mullns, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

I've often contemplated this approach myself... a question though, does
the MS hashtable implementation have some sort of container object? I
suppose even the weak reference object must have some overhead. Either
way - if you use this approach, I suppose you must occaisionally go
through and prune all the empty WeakReferences out of your hashtable...
unless you can bind an event to the Weak Reference and have it remove
itself... but at this point we're getting into the sort of complicated
three-star programming that is best left to C++.

Sep 29 '06 #5

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

Similar topics

1
by: francois | last post by:
Hi, I have a webservice that I am using and I would like it to return an XML serialized version of an object. the class of the object is defined serializable as the following: public...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
5
by: ad | last post by:
I have a hashtable name myHash. sKey is a string, I use myHash to retrieve the value of that key. But is sKey is not in the myHash.Keys, it raise an Exception. I want to dertiminate if sKey in...
2
by: Ali | last post by:
I am binding a hashTable to a dropDownList to pick a State (key: like New York) and sends the state designation (value: NY) to a filtering procedure. I have entered the states in the hashTable in...
8
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
2
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.