Skip to content

Instantly share code, notes, and snippets.

@bric3
Last active November 6, 2015 18:03
Show Gist options
  • Save bric3/0948d92c2250479d808e to your computer and use it in GitHub Desktop.
Save bric3/0948d92c2250479d808e to your computer and use it in GitHub Desktop.
Calculate the size of a cache using jol (Java Object Layout) (eviction concerns not taken care of)
public class CacheSizing {
public static final int _10_MEGABYTE = 10_000_000;
// The type Value is a Value Object whose max size is known, that is :
// - no string with "unbounded" variable length
// - no Collection with any size
@Test
public void possible_cache_size_value() {
int possible_cache_size_per_server = 10_000;
LoadingCache<String, Value> c = CacheBuilder.newBuilder()
.maximumSize(possible_cache_size_per_server)
.recordStats()
.build(new CacheLoader<String, Value>() {
@Override
public Value load(String accessToken) {
return ...;
}
});
c.putAll(Stream.generate(ValueFactory::create)
.limit(10_000)
.collect(toMap(Value::key, Function.identity())));
System.out.println(GraphLayout.parseInstance(c).toFootprint());
assertThat(GraphLayout.parseInstance(c).toFootprint()).isLessThan(_10_MEGABYTE);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<dependencies>
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>${jol-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</pom>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment