From 544283a650afc58ec6dd84e13e6d5b4700197446 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Sun, 29 May 2011 09:33:51 -0500 Subject: [PATCH] More memory tuning on BufferedImage handling, workaround bukkit leak --- src/main/java/org/dynmap/MapChunkCache.java | 15 +++++++++++++++ src/main/java/org/dynmap/kzedmap/KzedMap.java | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/dynmap/MapChunkCache.java b/src/main/java/org/dynmap/MapChunkCache.java index 636d2f48..e8d11145 100644 --- a/src/main/java/org/dynmap/MapChunkCache.java +++ b/src/main/java/org/dynmap/MapChunkCache.java @@ -14,6 +14,7 @@ public class MapChunkCache { private World w; private static Method getchunkdata = null; private static Method gethandle = null; + private static Method poppreservedchunk = null; private static Field heightmap = null; private static boolean initialized = false; @@ -177,6 +178,13 @@ public class MapChunkCache { } catch (NoSuchMethodException nsmx) { } catch (NoSuchFieldException nsfx) { } + /* Get CraftWorld.popPreservedChunk(x,z) - reduces memory bloat from map traversals (optional) */ + try { + Class c = Class.forName("org.bukkit.craftbukkit.CraftWorld"); + poppreservedchunk = c.getDeclaredMethod("popPreservedChunk", new Class[] { int.class, int.class }); + } catch (ClassNotFoundException cnfx) { + } catch (NoSuchMethodException nsmx) { + } initialized = true; if(gethandle != null) Log.info("Chunk snapshot support enabled"); @@ -220,6 +228,13 @@ public class MapChunkCache { * while the actual in-use chunk area for a player where the chunks are managed * by the MC base server is 21x21 (or about a 160 block radius) */ w.unloadChunk(chunk.x, chunk.z, false, false); + /* And pop preserved chunk - this is a bad leak in Bukkit for map traversals like us */ + try { + if(poppreservedchunk != null) + poppreservedchunk.invoke(w, chunk.x, chunk.z); + } catch (Exception x) { + Log.severe("Cannot pop preserved chunk - " + x.toString()); + } } } } diff --git a/src/main/java/org/dynmap/kzedmap/KzedMap.java b/src/main/java/org/dynmap/kzedmap/KzedMap.java index 6c4faa94..b456ae08 100644 --- a/src/main/java/org/dynmap/kzedmap/KzedMap.java +++ b/src/main/java/org/dynmap/kzedmap/KzedMap.java @@ -293,8 +293,8 @@ public class KzedMap extends MapType { img.width = x; img.height = y; img.argb_buf = new int[x*y]; - img.buf_img = createBufferedImage(img.argb_buf, img.width, img.height); } + img.buf_img = createBufferedImage(img.argb_buf, img.width, img.height); return img; } @@ -303,6 +303,7 @@ public class KzedMap extends MapType { */ public static void freeBufferedImage(KzedBufferedImage img) { img.buf_img.flush(); + img.buf_img = null; /* Toss bufferedimage - seems to hold on to other memory */ synchronized(lock) { long k = (img.width<<16) + img.height; LinkedList ll = imgcache.get(k);