Search in sources :

Example 1 with WeakRefAssetCache

use of com.jme3.asset.cache.WeakRefAssetCache 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;
        }
    }
}
Also used : SimpleAssetCache(com.jme3.asset.cache.SimpleAssetCache) WeakRefCloneAssetCache(com.jme3.asset.cache.WeakRefCloneAssetCache) AssetCache(com.jme3.asset.cache.AssetCache) WeakRefAssetCache(com.jme3.asset.cache.WeakRefAssetCache) SimpleAssetCache(com.jme3.asset.cache.SimpleAssetCache) ArrayList(java.util.ArrayList) WeakRefCloneAssetCache(com.jme3.asset.cache.WeakRefCloneAssetCache) CloneableAssetProcessor(com.jme3.asset.CloneableAssetProcessor) AssetProcessor(com.jme3.asset.AssetProcessor) CloneableAssetProcessor(com.jme3.asset.CloneableAssetProcessor) WeakRefAssetCache(com.jme3.asset.cache.WeakRefAssetCache)

Aggregations

AssetProcessor (com.jme3.asset.AssetProcessor)1 CloneableAssetProcessor (com.jme3.asset.CloneableAssetProcessor)1 AssetCache (com.jme3.asset.cache.AssetCache)1 SimpleAssetCache (com.jme3.asset.cache.SimpleAssetCache)1 WeakRefAssetCache (com.jme3.asset.cache.WeakRefAssetCache)1 WeakRefCloneAssetCache (com.jme3.asset.cache.WeakRefCloneAssetCache)1 ArrayList (java.util.ArrayList)1