caasdads
caasdads

Reputation: 419

Who consumes more memory, ArrayList or HashSet / LinkedHashSet?

I have a very huge collection, so even relatively small memory difference can make difference.

Or the difference is minimal?

Does memory consumtion growth differently when we add more elements?

I believe that LinkedHashSet eats more memory per N elements than HashSet (to store linked-list links), but I cannot compare ArrayList to HashSet.

Upvotes: 2

Views: 403

Answers (1)

GBlodgett
GBlodgett

Reputation: 12819

Typically, a HashMap uses 32 bytes per entry (12 bytes header + 16 bytes data + 4 bytes padding). It will also use 4 * the capacity bytes, so when it's all said and done a HashMap object will occupy

32 * size + 4 * capacity bytes

An ArrayList on the other hand generally allocates 4-8 bytes per entry. This can be more however, if you allocate a bigger capacity of the ArrayList, and only hold a few elements.

Upvotes: 2

Related Questions