/** * 以指定范围内的指定概率执行gc * * @param size * @param rand */ public static final void gc(final int size, final long rand) { if (rand > size) { throw new RuntimeException(("GC random probability " + rand + " > " + size).intern()); } if (LSystem.random.nextInt(size) <= rand) { LSystem.gc(); } }
/** * 加载资源文件 * * @param resName * @return */ public static final ArrayByte getResource(String resName) { if (resName == null) { return null; } String innerName = resName; String keyName = innerName.replaceAll(" ", "").toLowerCase(); synchronized (lock) { if (lazyResources.size() > LSystem.DEFAULT_MAX_CACHE_SIZE) { lazyResources.clear(); LSystem.gc(); } byte[] data = (byte[]) lazyResources.get(keyName); if (data != null) { return new ArrayByte(data); } } InputStream in = null; // 外部文件标志 boolean filePath = innerName.startsWith("$"); if (filePath || isExists(resName)) { try { innerName = innerName.substring(1, innerName.length()); in = new BufferedInputStream(new FileInputStream(new File(innerName))); } catch (FileNotFoundException ex) { } } else { in = new BufferedInputStream(classLoader.getResourceAsStream(innerName)); } ArrayByte byteArray = new ArrayByte(); try { byteArray.write(in); in.close(); byteArray.reset(); lazyResources.put(keyName, byteArray.getData()); } catch (IOException ex) { byteArray = null; } if (byteArray == null) { throw new RuntimeException(resName + " file not found !"); } return byteArray; }
/** * 以指定概率使用gc回收系统资源 * * @param rand */ public static final void gc(final long rand) { gc(100, rand); }
/** 清空框架临时资源 */ public static void destroy() { GraphicsUtils.destroy(); // ZipResource.destroy(); Resources.destroy(); LSystem.gc(); }