[ 
https://issues.apache.org/jira/browse/PDFBOX-5731?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17796052#comment-17796052
 ] 

Axel Howind commented on PDFBOX-5731:
-------------------------------------

[~lycheng] I have created a patch for current trunk. Could you please try it 
out?

What it does: I replaced nameMap and commonNameMap by a Map that holds 
WeakReference instances to values (we cannot use a WeakHashMap since that one 
holds weak keys instead). The clearResources() method is automatically invoked 
every time a new COSName instance is created (that is, if it is not yet present 
in the cache). This step is necessary, because the problem with unused COSName 
instances is that they hold a reference to their key. That means, the bulk of 
memory can only be released, if the string used as key can also be GCed.

Using a WeakHashMap would not help here because weak references to keys would 
not become stale unless the value is removed, which will not happen before the 
key is removed etc.

I have confirmed that unused entries are removed automatically. The instances 
that were stored in the commonNameMap before are also retained because their 
references never become stale (because they are defined as constants in 
COSName).
 * Calling clearResources manually is still possible but should not be 
necessary.
 * I also have tried to call clearResources only every 100th time the nameMap 
is changed but I could see no measurable performance impact, so I removed that 
code again.
 * I think there should no extra synchronisation be necessary when calling 
clearResources as IMHO the worst that could happen is that we end up with a 
couple of extra COSName instances. But this probably needs some more testing.

Please let me know if this solves your problem.

Added patch 
"attempted_fix_for_PDFBOX-5731__clear_out_unused_COSName_instances_automatically.patch".

> org.apache.pdfbox.cos.COSName#nameMap There is a memory leak problem.
> ---------------------------------------------------------------------
>
>                 Key: PDFBOX-5731
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5731
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.30, 3.0.1 PDFBox
>            Reporter: liu
>            Priority: Major
>         Attachments: 
> attempted_fix_for_PDFBOX-5731__clear_out_unused_COSName_instances_automatically.patch,
>  image-2023-12-08-16-02-12-293.png, screenshot-1.png, screenshot-2.png, 
> screenshot-3.png, screenshot-4.png, screenshot-5.png
>
>
>  !image-2023-12-08-16-02-12-293.png! 
>  !screenshot-1.png! 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org

Reply via email to