Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:44:17+00:00 2026-05-27T08:44:17+00:00

TDictionary<TKey,TValue> uses an internal array that is doubled if it is full: newCap :=

  • 0

TDictionary<TKey,TValue> uses an internal array that is doubled if it is full:

newCap := Length(FItems) * 2;
if newCap = 0 then
  newCap := 4;
Rehash(newCap);

This performs well with medium number of items, but if one gets to the upper limit it is very unfortunate, because it might throw an EOutOfMemory exception even if there is almost half of the memory still available.

Is there any way to influence this behaviour? How do other collection classes deal with this scenario?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:44:18+00:00Added an answer on May 27, 2026 at 8:44 am

    You need to understand how a Dictionary works. A dictionary contains a list of “hash buckets” where the items you insert are placed. That’s a finite number, so once you fill it up you need to allocate more buckets, there’s no way around it. Since the assignment of objects-to-buckets is based on the result of a hash function, you can’t simply add buckets to the end of the array and put stuff in there, you need to re-allocate the whole list of blocks, re-hash everything and put it in the (new) corresponding buckets.

    Given this behavior, the only way to make the dictionary not re-allocate once full is to make sure it never gets full. If you know the number of items you’ll insert in the dictionary pass it as a parameter to the constructor and you’ll be done, no more dictionary reallocations.

    If you can’t do that (you don’t know the number of items you’ll have in the dictionary) you’ll need to reconsider what made you select the TDictionary in the first place and select a data structure that offers better compromise for your particular algorithm. For example you could use binary search trees, as they do the balancing by rotating information in existing nodes, no need for re-allocations ever.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In .NET, there is a constructor for Dictionary<TKey, TValue> that takes one parameter, int
The MSDN explains Lookup like this: A Lookup<TKey, TElement> resembles a Dictionary<TKey, TValue> .
SortedList<TKey, TValue>.IndexOfKey(key) returns -1 if key is not in the list. Does this mean
Instead of using Dictionary<TKey,TValue> I want some type of collection class that can use
I guess everything is in the title... I know that Dictionary<TKey,TValue> does keep the
I find that SortedList<TKey, TValue> SortedDictionary<TKey, TValue> and Dictionary<TKey, TValue> implement the same interfaces.
As MSDN says ConcurrentDictionary<TKey, TValue> Class Represents a thread-safe collection of key-value pairs that
IDictionary<TKey, TValue> ArgumentNullException - key is null. Then, in the remarks... Implementations can vary
I have used Dictionary(TKey, TValue) for many purposes. But I haven't encountered any scenario
I want to get type of TKey and TValue given a Dictionary<TKey,TValue> type. eg.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.