c# - Memory using by a Hashtable object in .Net -


we have web application using httpruntime cache storing using db data. code snippet is:

public static void add(string cachename, hashtable cachevalue) {   system.web.httpruntime.cache.add(cachename, cachevalue, null, datetime.now.addseconds(60), timespan.zero, system.web.caching.cacheitempriority.high, null); } 

here hashtable object receives data database , adding httpruntime cache. how find out memory taken hashtable object using code.....thanks.

there no way size of managed object, differs implementation implementation.

see link more details: size of managed object

for inaccurate estimate can try using gc.gettotalmemory(false) before hashtable initialized , right after filled data:

        console.write("how many entries store in hashtable?:");          int hashtableentries;         int.tryparse(console.readline(), out hashtableentries);          var membeforehashinit = gc.gettotalmemory(true);          var hashtable = new hashtable();          (int = 0; < hashtableentries; i++)             hashtable.add(i, i);          var memafterhashinit = gc.gettotalmemory(false);          var diff = memafterhashinit - membeforehashinit;          console.writeline("memory used since startup: {0} bytes" +             "\r\n" +             "hashtable entries: {1}" +             "\r\n" +             "press key exit", diff, hashtableentries);         console.readline(); 

as appears, once hashtable initialized, 8192 bytes allocated in memory, appears allocates memory 64 of elements, giving 128 bytes per element. add 65th element hashtable reserves 8192 bytes 64 elements.

it may differ on system, it's clr decide :)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -