473,404 Members | 2,170 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,404 software developers and data experts.

Intermittent Hashtable() error: Load factor too high

Once a day or so, I receive an error on a fairly active website that
calls this StrMixed.cs method constructor. 99% of the time there is no
exception:

System.Web.HttpUnhandledException: Exception of type
System.Web.HttpUnhandledException was thrown. --->
System.InvalidOperationException: Hashtable insert failed. Load factor
too high.
at System.Collections.Hashtable.Insert(Object key, Object nvalue,
Boolean add)
at System.Collections.Hashtable.set_Item(Object key, Object value)
at AEC.StrMixed..ctor() in C:\Documents and
Settings\...\AEC\StrMixed.cs:line 97

A search on Google for "Load factor too high" and Hashtable did not
provide any insight.

The Hashtable created is fairly small and the trap occurs below:

public StrMixed()
{
Exceptions = new Hashtable();

Exceptions["3Do"] = "3DO";
Exceptions["A"] = "a";
Exceptions["Ac"] = "AC";
Exceptions["Ac-3"] = "AC-3";
Exceptions["Ac/Dc"] = "AC/DC";
Exceptions["Am/Fm"] = "AM/FM";
Exceptions["And"] = "and";
Exceptions["D.a.T"] = "D.A.T.";
Exceptions["De"] = "de";
Exceptions["Dr"] = "Dr";
Exceptions["Emi"] = "EMI";
Exceptions["En"] = "en";
Exceptions["For"] = "for";
Exceptions["Ft"] = "Ft";
Exceptions["Ii"] = "II";
Exceptions["Iii"] = "III";
Exceptions["Imax"] = "IMAX";
Exceptions["In"] = "in";
Exceptions["La"] = "la";
Exceptions["Los"] = "los";
Exceptions["Mac-Pc"] = "Mac-PC";
Exceptions["Macabre"] = "Macabre";
Exceptions["Macarena"] = "Macarena";
Exceptions["Macedonia"] = "Macedonia"; <-- Traps here
Exceptions["Macedonian"] = "Macedonian";
Exceptions["Mackie"] = "Mackie";
:
:
Since I know in advance the number of default entries added, should I
specify a load factor of some sort? This seems like a framework bug to
me.

Thanks

Gary Davis
Nov 15 '05 #1
4 5251
Hi Gary,

I did a search on Google for "Load factor too high" as well and there
was just a single reference.

http://dotnet.di.unipi.it/Content/ss...bcl/hashtable_
8cs-source.html

This took me to the source code for an implementation of Hastable.

The top of the file started with: [some lines omitted]

00004 // Copyright (c) 2002 Microsoft Corporation. All rights
reserved.
00005 //
00006 // The use and distribution terms for this software are
contained in the file
00007 // named license.txt, which can be found in the root of this
distribution.
00008 // By using this software in any fashion, you are agreeing to
be bound by the
00009 // terms of this license.
00010 //
00017 ** Class: Hashtable
00021 ** Purpose: Hash table implementation
00023 ** Date: September 25, 1999
00024 **

Somewhere down inside is the function that you are calling when you
assign to the hashtable.

00711 // Inserts an entry into this hashtable. This method is
called from the Set
00712 // and Add methods. If the add parameter is true and the
given key already
00713 // exists in the hashtable, an exception is thrown.
00714 private void Insert (Object key, Object nvalue, bool
add) {
00715 if (key == null) {
00716 throw new ArgumentNullException("key",
Environment.GetResourceString("ArgumentNull_Key")) ;
00717 }

At at the end of the function it says: [Caps added]

00777
00778 // If you see this assert, make sure load
factor & count are reasonable.
00779 // Then verify that our double hash function (h2,
described at top of file)
00780 // meets the requirements described above. YOU
SHOULD NEVER SEE THIS ASSERT.
00781 BCLDebug.Assert(false, "hash table insert failed!
Load factor too high, or our double hashing function is incorrect.");
00782 throw new
InvalidOperationException(Environment.GetResourceS tring
("InvalidOperation_HashInsertFailed"));
00783 }

Now, the error message isn't <exactly> like yours but it looks mighty
suspicious, doesn't it?

In which case the answer is, yes, set your loadfactor and initial
capacity. And who can dispute that it's a framework bug - they seem to
say so themselves!!

Note. There are no properties to determine the load factor or capacity
(number of buckets), but these are clearly visible if you stick your
Hashtable into a Watch - loadFactor, buckets.Length.

The documentation says 1.0 for initial load factor. I found that it
was 0.72 in .NET 1.0.3705

Regards,
Fergus
Nov 15 '05 #2
"Fergus Cooney" <wo****@tesco.net> wrote in message news:<#R**************@tk2msftngp13.phx.gbl>...
Hi Gary,

I did a search on Google for "Load factor too high" as well and there
was just a single reference.

http://dotnet.di.unipi.it/Content/ss...bcl/hashtable_
8cs-source.html

This took me to the source code for an implementation of Hastable.
...
Regards,
Fergus


Yes, I did see that hit and was very interested in the source file and
other stuff on that site. It showed how complex the hashtable actually
is.

However, even though it indicated the error, I was not sure why it
traps on occasion and generally works fine, for the exact same
constructor. Also, I couldn't figure how what to set the load factor
to (1.1? 0.9?).

Gary
Nov 15 '05 #3
Hi Gary,

I think someone famous quoted:
Ours is not to reason why,
Ours to work around, or die.

Yes, it does look a meaty bit of code.

From looking at Insert(), it only tries increasing the Hashtable
if it's reached the limit (specified by the capacity (number of
buckets) and load factor) which is kept in the private variable
loadsize.

Did you try my suggestion of seeing how the Hastable grows by
using Add Watch?
The code I used is:
Dim oHashtable As New Hashtable
For I = 1 to 50
oHashtable(i) = 1000 + i
Next

You'll see that loadsize is small (7 for me). When the 8th item is
to be added, Insert() finds loadsize exceeded and proceeds to expand
the hashtable. In the docs it says that "the number of buckets is
automatically increased to the smallest prime number that is larger
than twice the current number of buckets.".

The initial (for me) number of buckets is 11 and the loadfactor is
..72 which gives a limit (loadsize = buckets.Length * loadfactor) of 7
(7.92 rounded down).

So, double the number of buckets (22) and go to the next prime
giving a new number of buckets, 27. The loadfactor stays the same, so
the new capacity (loadsize) is 27 * .72 = 24. The 25th insert will
cause further expansion. And so on.

So, your job is to find an initial size for the Hashtable which
will not cause Insert to expand the table. If it doesn't expand, it
won't reach the Exception.

I don't know how many items you have. But whatever it is, you need
to calculate for a loadsize which greater than your number of items.

Let's say you have 51 items. The loadfactor is .72 so the number
of buckets will be at least 51 / 0.72 which gives 70.833 which this
time is riunded <up> to 71. The number of buckets must be a prime
number (for an effective hashing algorithm). 71 <is> a prime number so
that's no problem.

Let's say you have 52 items. The loadfactor is .72 so the number
of buckets will be at least 52 / 0.72 which gives 72.222 which this
time is rounded up to 73. The number of buckets must be a prime number
and the next prime is 89. So the table will have 89 entries.

Have you followed so far? Because now I'm going to say forget all
that, lol, that's just background.

For plain Hashtables, the constructors are (), which you've been
using, (iCapacity) and (iCapacity, fLoadFactor).

You can use either of the other two because all the calculations
above are done by the Hashtable. It will ensure that whatever
loadfactor is used (default or supplied) it will have a sufficiently
large table to handle the given number of items.

The loadfactor is effectively the slider between max memory
efficiency (loadfactor = 1) and max access efficiency (loadfactor
smallish).

Small hashtables (loadfactor high) will have lots of what are
called 'collisions' when the initial index of two or more items is the
same and steps must be taken to get round this. With large hashtables
(loadfactor low), an item's initial index is always available to just
that item. But for this to happen there will be a large number of
buckets that will never be used. With medium Hastables there will be
some or occasional collisions, depending on how 'medium' it is.

Still with me ? Because now I'm going to say forget all that
<again>, lol.

I wouldn't worry about the loadfactor (should have said that in my
last post) - just use the capacity.

Hope this gives you more than you need. :-).

Regards,
Fergus
Nov 15 '05 #4
Hi Gary,

It's strange that you can't see loadsize, etc. I've got my
Hashtable added to a Watch window. When I click on the [+] it shows me
everything.

Regards,
Fergus
Nov 15 '05 #5

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

Similar topics

1
by: Chandu | last post by:
Is there a way to optimize the use of Hashtable in java in terms of Memory and lookups what exactly are collisions in a hastable and how does it affect performance. Any help would be greatly...
2
by: Bill | last post by:
In which case Hashtable A equals to Hashtable B? 1). A and B have same key/value pairs, regardless Hashtable's initial size and load factor. 2). A and B have same key/value pairs, and have same...
7
by: davidw | last post by:
I always use NameValueCollection. But I read an article says the only differece between Hashtable and NameValueCollection is that NameValueCollection could accept more than one value with same key?...
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...
7
by: Naveen Mukkelli | last post by:
Hi, I'm working on a Client/Server application. Server application needs to keep track of all the clients. I'm planning to use a Hashtable for this purpose. There would be thousands of...
3
by: Lee Chapman | last post by:
Hi, I have a problem where my ASP.NET application occasionally generates a MissingFieldException exception. This unexpectedly happened on my development box, and so I was able to extract some...
5
by: Dick | last post by:
Hello, I'm trying to serialize a class with a Hashtable within: ' Class code: Imports System.Collections Class clsOptions Public countID As Integer Public persons As New Hashtable End Class
9
by: Nick Vaughan | last post by:
While running some long term tests on our Broadcast SDK one of our thread dropped out because an exception had been thrown: Exception: System.ArgumentOutOfRangeException Message: Load factor...
17
by: John A Grandy | last post by:
For a Hashtable that is expected to contain no more than 100 DictionaryEntry elements , what is the best load factor ? ( This Hashtable is a encapsulted in a class , an instance of which is...
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: 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
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
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...
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
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...
0
agi2029
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,...
0
isladogs
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...

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.