473,791 Members | 3,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3789
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***@discussi ons.microsoft.c omwrote >
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***@discussi ons.microsoft.c omwrote >
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******** ************@k7 0g2000cwa.googl egroups.com...
>
Chris Mullins wrote:
>"Macca" <Ma***@discussi ons.microsoft.c omwrote >
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
8717
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 class Event {
5
2832
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 like it to return an XML serialized version of an object.
5
15584
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. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include adding/modifying/deleting. To solve the synchronization issues I am doing two things: 1. Lock...
33
3324
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 used before adding, removing or iterating the Hashtable. The program runs in a high workload environment. After running a few days, now it suddenly catchs this Exception when inserting a pair of key and object, stacktrace =...
5
1509
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 myHash.Keys. How can I do that?
2
5545
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 alphabetical order, but the dropDownList renders them in a non-order list. code snippet: Dim state as New hashTable state.Add("Alabama", "AL") state.Add("Alaska", "AK")
8
1691
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 on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of...
2
3154
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 need to write to it, but now it occurred to me that maybe I could just lock one of the Hashtables inside without locking the entire object, but then I thought maybe some thread could instruct the outside Hashtable to remove an inside Hashtable...
2
3881
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 function. Then i am setting this hash table to hashtable which i am passing as ref. So my question is how scope is mention when i am assigning local
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
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 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.