[OpenJDK 2D-Dev] RFR: 8257500: Drawing MultiResolutionImage with ImageObserver "leaks" memory

Sergey Bylokhov serb at openjdk.java.net
Thu Feb 25 00:43:56 UTC 2021


This bug was reported as a leak of the ImageObserver when the user draws a multiresolution image.

The multiresolution image is an image that contains a few images inside, when the app uses the observer to get notifications about image loading we create a special internal observer and use it to track loading resolution-variants. This internal observer forwards notification from the resolution variant to the application observer.

The mapping from the application observer to the internal observer is done via "SoftCache" which is a kind of HashMap that uses soft references to the key and value.

Here the bug comes, the soft references are cleared only under memory pressure, and unused objects may sit hours in memory before being cleaned. Moreover, the internal observer was implemented using a strong reference to the application observer(it is not obvious since the lambda is used). So the key object refers to the application's observer cannot be clear fast. This causes an even longer delay of the memory cleanup, which was considered by the use as a "leak".

The fix changes the usage of SoftCache to the WeakHashMap, so the key(the application observer) will be cleared when the application lost the reference to it.

-------------

Commit messages:
 - Update ImageObserverLeak.java
 - Update MultiResolutionToolkitImage.java
 - cleanup
 - Update MultiResolutionToolkitImageTest.java
 - Initial version
 - temp version

Changes: https://git.openjdk.java.net/jdk/pull/2711/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2711&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8257500
  Stats: 125 lines in 3 files changed: 88 ins; 9 del; 28 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2711.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2711/head:pull/2711

PR: https://git.openjdk.java.net/jdk/pull/2711


More information about the 2d-dev mailing list