473,837 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

System.Web.Http UnhandledExcept ion: Exception of type
System.Web.Http UnhandledExcept ion was thrown. --->
System.InvalidO perationExcepti on: Hashtable insert failed. Load factor
too high.
at System.Collecti ons.Hashtable.I nsert(Object key, Object nvalue,
Boolean add)
at System.Collecti ons.Hashtable.s et_Item(Object key, Object value)
at AEC.StrMixed..c tor() in C:\Documents and
Settings\...\AE C\StrMixed.cs:l ine 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


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

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


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
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
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 ArgumentNullExc eption("key",
Environment.Get ResourceString( "ArgumentNull_K ey"));
00717 }

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

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
00781 BCLDebug.Assert (false, "hash table insert failed!
Load factor too high, or our double hashing function is incorrect.");
00782 throw new
InvalidOperatio nException(Envi ronment.GetReso urceString
("InvalidOperat ion_HashInsertF ailed"));
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

Nov 15 '05 #2
"Fergus Cooney" <wo****@tesco.n et> wrote in message news:<#R******* *******@tk2msft ngp13.phx.gbl>. ..
Hi Gary,

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


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

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

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?).

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

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

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

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. :-).

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

Nov 15 '05 #5

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

Similar topics

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 appreciated Regards
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 initial size. 3). A and B have same key/value pairs, and have same load factor. 4). A and B have same key/value pairs, and have same initial size and load factor. 5). Any other possible conditions?
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? I am not quite understand this. And I think NameValueCollection's Set is a nice method. If I use HashTable, how can I update a item in it? Do I have to check if it exists, and remove and add it again? Thanks.
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.
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 clients connect to the server application. In this case, according to my plan,
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 information from the debugger: The field that is "missing" is called 'logger'. Here's it's description taken from ildasm.exe: ..field public static class log4net.ILog logger
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
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 needs to be between 0.1 and 1. Parameter name: loadFactor Source: mscorlib at System.Collections.Hashtable..ctor(Int32 capacity, Single loadFactor, IHashCodeProvider hcp, IComparer comparer) at System.Collections.Hashtable..ctor()
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 cached and methods on which are called by n threads ( n is approx = 25 ) )
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...
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...
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...
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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();...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.