Skip to content

Instantly share code, notes, and snippets.

@jordanlewis
Last active December 22, 2015 03:59
Show Gist options
  • Save jordanlewis/6413827 to your computer and use it in GitHub Desktop.
Save jordanlewis/6413827 to your computer and use it in GitHub Desktop.
Extra boolean allocations via reflection
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