use of com.jme3.asset.cache.SimpleAssetCache in project jmonkeyengine by jMonkeyEngine.
the class TestAssetCache method runTest.
private static void runTest(boolean cloneAssets, boolean smartCache, boolean keepRefs, int limit) {
counter = 0;
List<Object> refs = new ArrayList<Object>(limit);
AssetCache cache;
AssetProcessor proc = null;
if (cloneAssets) {
proc = new CloneableAssetProcessor();
}
if (smartCache) {
if (cloneAssets) {
cache = new WeakRefCloneAssetCache();
} else {
cache = new WeakRefAssetCache();
}
} else {
cache = new SimpleAssetCache();
}
System.gc();
System.gc();
System.gc();
System.gc();
long memory = Runtime.getRuntime().freeMemory();
while (counter < limit) {
// Create a key
DummyKey key = new DummyKey();
// Create some data
DummyData data = new DummyData();
// Post process the data before placing it in the cache
if (proc != null) {
data = (DummyData) proc.postProcess(key, data);
}
if (data.key != null) {
// means the asset will never be collected => bug
throw new AssertionError();
}
cache.addToCache(key, data);
// Get the asset from the cache
AssetKey<DummyData> keyToGet = key.clone();
// Clone the asset
if (proc != null) {
// Data is now the clone!
data = (DummyData) proc.createClone(data);
if (smartCache) {
// Registering a clone is only needed
// if smart cache is used.
cache.registerAssetClone(keyToGet, data);
// otherwise => bug
if (data.key != key) {
throw new AssertionError();
}
}
}
// an out of memory error.
if (keepRefs) {
// Prevent the saved references from taking too much memory ..
if (cloneAssets) {
data.data = null;
}
refs.add(data);
}
if ((counter % 1000) == 0) {
long newMem = Runtime.getRuntime().freeMemory();
System.out.println("Allocated objects: " + counter);
System.out.println("Allocated memory: " + ((memory - newMem) / (1024 * 1024)) + " MB");
memory = newMem;
}
}
}
Aggregations