Skip to content

Instantly share code, notes, and snippets.

@cykl
Created August 20, 2014 06:33
Show Gist options
  • Save cykl/48c501f95ad618fd3217 to your computer and use it in GitHub Desktop.
Save cykl/48c501f95ad618fd3217 to your computer and use it in GitHub Desktop.
HashMap VS TreeMap memory footprint
import org.openjdk.jol.info.GraphLayout;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Main {
static final int SIZE = 1<<18;
static final double LOAD_FACTOR = 0.75;
public static void main(String[] args) {
int elemCount = (int)(SIZE * LOAD_FACTOR) - 10;
measureMapFootprint(new HashMap<String, String>(SIZE), elemCount);
measureMapFootprint(new TreeMap<String, String>(), elemCount);
}
static void measureMapFootprint(Map<String, String> map, int elemCount) {
for (int i = 0; i < elemCount; i++) {
map.put(Integer.toString(i), Integer.toString(i));
}
System.out.println(GraphLayout.parseInstance(map).totalSize());
System.out.println(GraphLayout.parseInstance(map).toFootprint());
}
}
29198752
java.util.HashMap instance footprint:
COUNT AVG SUM DESCRIPTION
393196 31 12422272 [C
393196 24 9436704 java.lang.String
1 48 48 java.util.HashMap
1 1048592 1048592 [Ljava.util.HashMap$Node;
196598 32 6291136 java.util.HashMap$Node
982992 29198752 (total)
29722944
java.util.TreeMap instance footprint:
COUNT AVG SUM DESCRIPTION
393196 31 12422272 [C
393196 24 9436704 java.lang.String
1 48 48 java.util.TreeMap
196598 40 7863920 java.util.TreeMap$Entry
982991 29722944 (total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment