1
votes

I get the following exception in C#

Index was outside the bounds of the array.

in System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)

in System.Collections.Generic.Dictionary`2.System.Collections.IDictionary.set_Item(Object key, Object value)

The application is running with SQL Server on the same machine. After the first occurrence of the exception, all subsequent operations on this dictionary will get this exception 100%. At that time the server memory usage is 98% and SQL server is occupying 85%+. With my understanding SQL Server will release the memory if other application is acquiring, but it seems not the case.

so the question is:

  1. will the dictionary has index out of bound when it fails to expand? or my exception is due to other reason?
  2. if 1 is true, any suggestion on how to balance the usage of memory before application and SQL server?

Update

adding the code as requested:

if (data == null)
    return;
var obj = CacheData<T>();
lock (obj)
{
    obj[data.ID] = data; <== exception occurs here
}
1
When do you get the exception? Is it when you are looping through the keys? Like @MitchWheat says, no code, no answer.shree.pat18

1 Answers

1
votes

It is probably a concurrency problem. Maybe you forgot to lock your dictionary (obj) elsewhere? Eitherway, you could use ConcurrentDictionary, that is thread-safe.