ha9u63a7
ha9u63a7

Reputation: 6854

Guava - difference in intialCapacity and concurrencyLevel

Guava docs says

https://guava.dev/releases/16.0/api/docs/com/google/common/cache/CacheBuilder.html#initialCapacity(int)

Sets the minimum total size for the internal hash tables. For example, if the initial capacity is 60, and the concurrency level is 8, then eight segments are created, each having a hash table of size eight. Providing a large enough estimate at construction time avoids the need for expensive resizing operations later, but setting this value unnecessarily high wastes memory.

So what's the point of using initialCapacity if it's going to create 8 tables with 8 rows ? Is this some documentation typo?

Upvotes: 0

Views: 2971

Answers (1)

MikeFHay
MikeFHay

Reputation: 9023

Under the hood, Guava's cache is implemented as multiple hash tables, each with a dedicated lock, so that concurrent writes don't all contest on a single lock.

initialCapacity refers to the capacity of the entire cache, not each individual underlying table. 8 tables each of size 8 can together handle up to 64 elements, so the documentation is correct.

Upvotes: 1

Related Questions