Last active
December 22, 2015 03:59
-
-
Save jordanlewis/6413827 to your computer and use it in GitHub Desktop.
Extra boolean allocations via reflection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Foo { | |
public boolean member = true; | |
} | |
@Test | |
public void testReflectionAllocation() throws Exception { | |
Foo foo = new Foo(); | |
IdentityHashMap<Boolean, Void> set = new IdentityHashMap<Boolean, Void>(); | |
for (int i = 0; i < 100; i++) { | |
// using Field.get() makes new instances | |
set.put((Boolean) Foo.class.getField("member").get(foo), null); | |
} | |
assertEquals(100, set.size()); | |
set.clear(); | |
for (int i = 0; i < 100; i++) { | |
// using Field.getBoolean() reuses the cached instances | |
set.put(Foo.class.getField("member").getBoolean(foo), null); | |
} | |
assertEquals(1, set.size()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment