Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the optimal amount to malloc at a given time when the total needed is not known?

I've implemented a multi-level cache simulator that needs to store the values currently in the simulator. With current configurations, the maximum size of all values being stored could reach 2G. Obviously I'm not going to assume this worst case scenario and allocate all of that memory up-front. Instead, I have the program set to allocate memory as needed in chunks. The expense of this allocation is exacerbated by the fact that I'm callocing in order to provide 0 values when no write has occurred previously at the specified location.

My question is, is there a good heuristic for how much memory should be allocated each time more is needed? Currently I'm using an arbitrary value and I considered some solution that would use some ratio of the total system memory (I presume it's possible to dynamically detect this at compile and/or runtime), but even with the latter I'm using an arbitrary ratio with still doesn't sit well with me.

Any insight into best practices for this kind of situation would be appreciated!

like image 232
Dan Avatar asked Nov 11 '22 17:11

Dan


1 Answers

A common rule of thumb is to grow geometrically, for example by doubling, on each reallocation.

like image 179
phs Avatar answered Nov 15 '22 12:11

phs