Home | History | Annotate | Download | only in asset
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package jme3test.asset;
     34 
     35 import com.jme3.asset.Asset;
     36 import com.jme3.asset.AssetCache;
     37 import com.jme3.asset.AssetKey;
     38 import java.util.ArrayList;
     39 import java.util.List;
     40 
     41 public class TestAssetCache {
     42 
     43     /**
     44      * Keep references to loaded assets
     45      */
     46     private final static boolean KEEP_REFERENCES = false;
     47 
     48     /**
     49      * Enable smart cache use
     50      */
     51     private final static boolean USE_SMART_CACHE = true;
     52 
     53     /**
     54      * Enable cloneable asset use
     55      */
     56     private final static boolean CLONEABLE_ASSET = true;
     57 
     58     private static int counter = 0;
     59 
     60     private static class DummyData implements Asset {
     61 
     62         private AssetKey key;
     63         private byte[] data = new byte[10000];
     64 
     65         public byte[] getData(){
     66             return data;
     67         }
     68 
     69         public AssetKey getKey() {
     70             return key;
     71         }
     72 
     73         public void setKey(AssetKey key) {
     74             this.key = key;
     75         }
     76     }
     77 
     78     private static class SmartKey extends AssetKey {
     79 
     80         public SmartKey(){
     81             super(".");
     82             counter++;
     83         }
     84 
     85         @Override
     86         public int hashCode(){
     87             return 0;
     88         }
     89 
     90         @Override
     91         public boolean equals(Object other){
     92             return false;
     93         }
     94 
     95         @Override
     96         public boolean useSmartCache(){
     97             return true;
     98         }
     99 
    100         @Override
    101         public Object createClonedInstance(Object asset){
    102             DummyData data = new DummyData();
    103             return data;
    104         }
    105     }
    106 
    107     private static class DumbKey extends AssetKey {
    108 
    109         public DumbKey(){
    110             super(".");
    111             counter++;
    112         }
    113 
    114         @Override
    115         public int hashCode(){
    116             return 0;
    117         }
    118 
    119         @Override
    120         public boolean equals(Object other){
    121             return false;
    122         }
    123 
    124         @Override
    125         public Object createClonedInstance(Object asset){
    126             if (CLONEABLE_ASSET){
    127                 DummyData data = new DummyData();
    128                 return data;
    129             }else{
    130                 return asset;
    131             }
    132         }
    133     }
    134 
    135     public static void main(String[] args){
    136         List<Object> refs = new ArrayList<Object>(5000);
    137 
    138         AssetCache cache = new AssetCache();
    139 
    140         System.gc();
    141         System.gc();
    142         System.gc();
    143         System.gc();
    144 
    145         long memory = Runtime.getRuntime().freeMemory();
    146 
    147         while (true){
    148             AssetKey key;
    149 
    150             if (USE_SMART_CACHE){
    151                 key = new SmartKey();
    152             }else{
    153                 key = new DumbKey();
    154             }
    155 
    156             DummyData data = new DummyData();
    157             cache.addToCache(key, data);
    158 
    159             if (KEEP_REFERENCES){
    160                 refs.add(data);
    161             }
    162 
    163             if ((counter % 100) == 0){
    164                 long newMem = Runtime.getRuntime().freeMemory();
    165                 System.out.println("Allocated objects: " + counter);
    166                 System.out.println("Allocated memory: " + ((memory - newMem)/1024) + "K" );
    167                 memory = newMem;
    168             }
    169         }
    170     }
    171 }
    172