c++ - How to measure Memory Usage of std::unordered_map -
we know hash table based container implementations std::unordered_map use lot memory don't know how how much?
apart space complexity notations, , not considering if container element pointer larger object:
is there way figure out how many bytes being used such container during run-time?
is there way tell, @ runtime, how memory any container uses?
if want rough size, think bucket_count() , max_load_factor() enough, gives current count of buckets , load factor.
rational:
if
load_factor<= 1, indicatesbucket_count()>= items in map, bucket_count() size of memory usage.if
load_factor> 1,bucket_count()*load_factorindicates max item in map. note max size, not real size.
so rough memory usage this:
unsigned n = mymap.bucket_count(); float m = mymap.max_load_factor(); if (m > 1.0) { return n * m; } else { return n; } if want accurate memory usage, may need count buckets see how many elements in it:
size_t count = 0; (unsigned = 0; < mymap.bucket_count(); ++i) { size_t bucket_size = mymap.bucket_size(i); if (bucket_size == 0) { count++; } else { count += bucket_size; } }
Comments
Post a Comment