Search in sources :

Example 1 with AssetCache

use of com.jme3.asset.cache.AssetCache 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)

Example 2 with AssetCache

use of com.jme3.asset.cache.AssetCache in project jmonkeyengine by jMonkeyEngine.

the class DesktopAssetManager method addToCache.

@Override
public <T> void addToCache(AssetKey<T> key, T asset) {
    AssetCache cache = handler.getCache(key.getCacheType());
    if (cache != null) {
        cache.addToCache(key, asset);
        cache.notifyNoAssetClone();
    } else {
        throw new IllegalArgumentException("Key " + key + " specifies no cache.");
    }
}
Also used : AssetCache(com.jme3.asset.cache.AssetCache)

Example 3 with AssetCache

use of com.jme3.asset.cache.AssetCache in project jmonkeyengine by jMonkeyEngine.

the class DesktopAssetManager method loadAsset.

@Override
public <T> T loadAsset(AssetKey<T> key) {
    if (key == null)
        throw new IllegalArgumentException("key cannot be null");
    for (AssetEventListener listener : eventListeners) {
        listener.assetRequested(key);
    }
    AssetCache cache = handler.getCache(key.getCacheType());
    AssetProcessor proc = handler.getProcessor(key.getProcessorType());
    Object obj = cache != null ? cache.getFromCache(key) : null;
    if (obj == null) {
        // Asset not in cache, load it from file system.
        AssetInfo info = handler.tryLocate(key);
        if (info == null) {
            if (handler.getParentKey() != null) {
                // that something went wrong.
                for (AssetEventListener listener : eventListeners) {
                    listener.assetDependencyNotFound(handler.getParentKey(), key);
                }
            }
            throw new AssetNotFoundException(key.toString());
        }
        obj = loadLocatedAsset(key, info, proc, cache);
    }
    T clone = (T) obj;
    if (obj instanceof CloneableSmartAsset) {
        clone = registerAndCloneSmartAsset(key, clone, proc, cache);
    }
    return clone;
}
Also used : AssetCache(com.jme3.asset.cache.AssetCache)

Aggregations

AssetCache (com.jme3.asset.cache.AssetCache)3 AssetProcessor (com.jme3.asset.AssetProcessor)1 CloneableAssetProcessor (com.jme3.asset.CloneableAssetProcessor)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