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

Sergey Bylokhov serb at openjdk.java.net
Fri Feb 26 20:00:45 UTC 2021


On Wed, 24 Feb 2021 18:57:25 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

> 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.

This pull request has now been integrated.

Changeset: 6800ba46
Author:    Sergey Bylokhov <serb at openjdk.org>
URL:       https://git.openjdk.java.net/jdk/commit/6800ba46
Stats:     125 lines in 3 files changed: 88 ins; 9 del; 28 mod

8257500: Drawing MultiResolutionImage with ImageObserver "leaks" memory

Reviewed-by: azvegint, aivanov

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

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


More information about the 2d-dev mailing list