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

  • Home
  • SEARCH
  • 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 8373765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:45:43+00:00 2026-06-09T14:45:43+00:00

I read that initializing a dictionary with initial capacity may lead to better performance

  • 0

I read that initializing a dictionary with initial capacity may lead to better performance if the size could be estimated.

Dim MyDataTable As New DataTable

'Fill MyDataTable from Database

Dim CapacityEstimate As integer = MyDataTable.Rows.Count
Dim MyDictionary As New Dictionary(Of String, MyObjectType)(CapacityEstimate)

'Fill the Dictionary independent of data table

The CapacityEstimate variable is just an estimate (generally in the range of 2500 to 7000) of the number of Key/Value pairs that the dictionary should hold. So if I estimate it to be 4000 and end up with 4010 objects (I may go over or under, not certain) would the dictionary use a lot of memory to resize with those many key/value pairs already in it. Is this a good solution or am I better off not initializing it with initial capacity. Thanks.

EDIT: Related but not the same – Should a .NET generic dictionary be initialised with a capacity equal to the number of items it will contain?

  • 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-06-09T14:45:44+00:00Added an answer on June 9, 2026 at 2:45 pm

    Don’t worry about the small stuff. A dictionary like that does not use a lot of memory so having it resize itself can’t take a lot of memory either. The real storage is the objects for the key and data, the dictionary only contains references to them. 8 bytes per entry in 32-bit mode so that’s only 4000 x 8 + some overhead = 32 kilobytes.

    Furthermore, the capacity you pass is used to calculate the number of hash buckets in the dictionary. Which is always a prime number equal or larger than the capacity you specified. The prime numbers are picked from this array (copied from the Reference Source):

        internal static readonly int[] primes = {
            3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
            1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
            17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437,
            187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
            1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369};
    

    So if you pass 4000 then you’ll actually get 4049 buckets, the next largest prime. So overshooting to 4010 isn’t going to make a difference. If it does need to resize then it doubles the capacity. So a single resize would already produce 8419 buckets, well past you max estimate. Resizing isn’t very expensive either, couple of microseconds. Which is why Andre couldn’t see a difference.

    Which is, other than reasoning about it, the proper approach. Measure. Anybody can measure.

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

Sidebar

Related Questions

I read that using recursion with Divide and Conquer method is efficient. Could anyone
I’ve read that instead of initializing inherited members ( _c1 in our example )
I read that the Hadoop Map tasks write their output to local disk. Suppose
I read that MySQL fulltext search can cause table locking . It means people
I read that Windows wp8 allows native code. So how then have ports such
I read that when executing bundle install in command-line inside a Rails project, gems
I read that WCF service throttling enqueues requests internally without any additional code. Is
I read that the servlets map the current location based on the url. Clicking
I read that some webmail services prefetch url links in emails. The GET request
I read that .NET uses connection pooling. For example, if I instantiate a bunch

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.