You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/08/19 11:17:05 UTC
pdfbox-jbig2 git commit: PDFBOX-4290: avoid memory leak in
SoftReferenceCache by using weak keys + gitignore .iml files, by Grigoriy
Alekseev; make field final + add override [Forced Update!]
Repository: pdfbox-jbig2
Updated Branches:
refs/heads/master d38089887 -> 10f4c76d8 (forced update)
PDFBOX-4290: avoid memory leak in SoftReferenceCache by using weak keys + gitignore .iml files, by Grigoriy Alekseev; make field final + add override
Project: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/repo
Commit: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/commit/10f4c76d
Tree: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/tree/10f4c76d
Diff: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/diff/10f4c76d
Branch: refs/heads/master
Commit: 10f4c76d8c497ea52346c637836d3989f3a6ec65
Parents: e36da15
Author: Tilman Hausherr <ti...@snafu.de>
Authored: Sun Aug 19 10:50:15 2018 +0200
Committer: Tilman Hausherr <ti...@snafu.de>
Committed: Sun Aug 19 13:14:14 2018 +0200
----------------------------------------------------------------------
.gitignore | 15 +--
.../jbig2/util/cache/SoftReferenceCache.java | 117 ++++++++++---------
.../util/cache/SoftReferenceCacheTest.java | 57 +++++++++
3 files changed, 126 insertions(+), 63 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/10f4c76d/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index 79c51d9..9318c34 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,8 @@
-.idea/
-.settings/
-.project
-.classpath
-atlassian-ide-plugin.xml
-levigo-jbig2-imageio.iml
-target/
+.idea/
+.settings/
+.project
+.classpath
+atlassian-ide-plugin.xml
+levigo-jbig2-imageio.iml
+target/
+*.iml
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/10f4c76d/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
index 8374848..9ad7027 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
@@ -1,56 +1,61 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.pdfbox.jbig2.util.cache;
-
-import java.lang.ref.SoftReference;
-import java.util.HashMap;
-
-public class SoftReferenceCache implements Cache
-{
-
- private HashMap<Object, SoftReference<?>> cache = new HashMap<Object, SoftReference<?>>();
-
- public Object put(Object key, Object value, int sizeEstimate)
- {
- SoftReference<Object> softReference = new SoftReference<Object>(value);
- SoftReference<?> oldValue = cache.put(key, softReference);
- return getValueNullSafe(oldValue);
- }
-
- public Object get(Object key)
- {
- SoftReference<?> softReference = cache.get(key);
- return getValueNullSafe(softReference);
- }
-
- public void clear()
- {
- cache.clear();
- }
-
- public Object remove(Object key)
- {
- SoftReference<?> removedObj = cache.remove(key);
- return getValueNullSafe(removedObj);
- }
-
- private Object getValueNullSafe(SoftReference<?> softReference)
- {
- return softReference == null ? null : softReference.get();
- }
-}
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.jbig2.util.cache;
+
+import java.lang.ref.SoftReference;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+public class SoftReferenceCache implements Cache
+{
+
+ private final Map<Object, SoftReference<?>> cache = new WeakHashMap<Object, SoftReference<?>>();
+
+ @Override
+ public Object put(Object key, Object value, int sizeEstimate)
+ {
+ SoftReference<Object> softReference = new SoftReference<Object>(value);
+ SoftReference<?> oldValue = cache.put(key, softReference);
+ return getValueNullSafe(oldValue);
+ }
+
+ @Override
+ public Object get(Object key)
+ {
+ SoftReference<?> softReference = cache.get(key);
+ return getValueNullSafe(softReference);
+ }
+
+ @Override
+ public void clear()
+ {
+ cache.clear();
+ }
+
+ @Override
+ public Object remove(Object key)
+ {
+ SoftReference<?> removedObj = cache.remove(key);
+ return getValueNullSafe(removedObj);
+ }
+
+ private Object getValueNullSafe(SoftReference<?> softReference)
+ {
+ return softReference == null ? null : softReference.get();
+ }
+}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/10f4c76d/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java b/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java
new file mode 100644
index 0000000..878de5b
--- /dev/null
+++ b/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.jbig2.util.cache;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SoftReferenceCacheTest
+{
+ private static final int KB = 1024;
+ private static final int MB = KB * KB;
+
+ /**
+ * Should not throw an OutOfMemoryError
+ */
+ @Test
+ public void putDoesNotLeakMemory()
+ {
+ Cache cache = CacheFactory.getCache();
+ cache.clear();
+ long maxHeapBytes = Runtime.getRuntime().maxMemory();
+ int halfEntrySizeEstimate = 8 * MB;
+
+ for (int i = 0; i < (maxHeapBytes / halfEntrySizeEstimate) * 2; i++)
+ {
+ cache.put(new Long[KB][KB], new Long[KB][KB], halfEntrySizeEstimate);
+ }
+ }
+
+ @Test
+ public void putAndGet()
+ {
+ Cache cache = CacheFactory.getCache();
+ cache.clear();
+ Object key = new Object();
+ Object value = new Object();
+
+ cache.put(key, value, 0);
+
+ Assert.assertEquals(value, cache.get(key));
+ }
+}