Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

21
LINES

< > BotCompany Repo | #1009433 // Measure size of HashMap [gives a good rough estimate]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (430L/4K/12K).

!7

static int n = 20000;

p {
  for (int tableSize = 0; tableSize < 100; tableSize += 10) {
    O[] data = arrayOfBlankObjects(tableSize);
    O[] array = new O[n];
    long mem1 = usedMemoryAfterGC();
    for i over array: {
      new HashMap hashMap;
      for (O x : data) hashMap.put(x, x);
      array[i] = hashMap;
    }
    long mem2 = usedMemoryAfterGC();
    double objSize = (mem2-mem1)/(double) l(array);
    int rounded = iround(objSize);
    long f = guessHashMapSize(tableSize);
    print("Hash table size=" + tableSize + ". Approximate object size: " + objSize + " => " + rounded + " bytes. Formula: " + f);
  }
}

Author comment

Began life as a copy of #1009423

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1009433
Snippet name: Measure size of HashMap [gives a good rough estimate]
Eternal ID of this version: #1009433/6
Text MD5: 390e276ef3a79a53ddcb4fe0ac3670ad
Transpilation MD5: a11a7f920c75be42028f9f399c422bd6
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-01 20:05:33
Source code size: 649 bytes / 21 lines
Pitched / IR pitched: No / No
Views / Downloads: 498 / 621
Version history: 5 change(s)
Referenced in: #1009436 - Measure size of ArrayList [OK]
#1009442 - Measure size of TreeMap [gives astonishingly good estimates]
#1012109 - Measure size of fairLock (ReentrantLock) - 48 bytes in CompressedOOPS 64 Bit VM
#1034068 - Measure size of synchronized LinkedHashMap