[
https://issues.apache.org/jira/browse/FOP-2651?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18144583#comment-18144583
]
Martin Leitner commented on FOP-2651:
-------------------------------------
Attached FOP-2651.patch (against main, fef45a4e4) with a fix and tests.
h3. What happens today
{{CustomFontMetricsMapper.initialize()}} calls {{Font.createFont(int,
InputStream)}} for every configured font in every render (PCL, PNG/TIFF, AWT:
everything that sets up fonts through {{ConfiguredFontCollection}}). Each call
copies the font to a {{+~JF*.tmp}} file in {{java.io.tmpdir}}. Once glyphs are
drawn, it also holds a FreeType scaler outside the heap. Both are released only
when the font is garbage-collected.
That alone would only cost time, but the JDK also keeps the last 20 opened font
files reachable: {{SunFontManager.fontFileCache}}, its channel pool
({{CHANNELPOOLSIZE = 20}}, filled from {{TrueTypeFont.open()}}). A created font
is opened at once. Measured on JDK 21, with a new {{FopFactory}} per render and
one configured TTF:
||Renders||+~JF*.tmp after the renders||after System.gc()||
|10|10|10|
|40|27|20|
The 20 survivors are held by {{FontManagerFactory.instance -> fontFileCache}}.
A plain JDK program that calls {{Font.createFont}} in a loop shows the same, so
there is no leak in FOP itself. In an application that renders all day with a
new {{FopFactory}} per render, these fonts survive long enough to reach the old
generation. Their temp files and native memory then pile up until a full GC.
With Serial GC and a large heap that can take days, and we saw a container
memory limit kill the JVM before that, without any {{OutOfMemoryError}}.
Passing a {{File}} to {{createFont}}, as this issue proposes, would avoid the
temp-file copy. It would still create a new {{Font2D}}, scaler and channel-pool
entry per font per render.
h3. The patch
* New {{org.apache.fop.render.java2d.AwtFontCache}}: a process-wide cache of
the size-1 base {{java.awt.Font}}, keyed by font format plus the SHA-256 of the
font bytes. The key is not the URI, because resolver URIs are often not
{{file:}} URIs and one URI can deliver different bytes over time. It is a
strong-reference LRU, 256 fonts by default. The system property
{{org.apache.fop.render.java2d.awtFontCacheSize}} changes the size; 0 or less
disables the cache. {{Font}} is immutable, so the instances are shared between
threads. A missing font is created outside the lock; if two threads race for
one font, the first one stored wins.
* {{CustomFontMetricsMapper.initialize()}} takes the base font from the cache.
It now also closes the stream when loading fails.
* {{CustomFontMetricsMapper.getFont(int)}} compared the cached size in points
with the requested size in millipoints, so it never matched and derived a new
font on every call. It compares points now.
* Tests:
** {{AwtFontCacheTestCase}} (10 tests): sharing, LRU eviction, disabled cache,
invalid font, two font setups sharing one font, the same bytes under another
URI, concurrent requests, and reuse of the derived font.
** {{PCLRepeatedRenderFontSoak}} (2 tests): renders to PCL with a new
{{FopFactory}} each time. Renders 2..10 must create no new {{+~JF*.tmp}} file
(9 without the fix), and 200 renders at most one (31 without the fix). It runs
in its own surefire execution, {{pcl-font-soak}}, with {{-Xmx128m}} and
{{java.io.tmpdir}} set to the build directory, so temp files of other Java
processes cannot interfere. The class name does not end in "TestCase", so the
default execution skips it.
The full fop-core suite passes with the patch on main (JDK 21).
Related: FOP-2294 (the same AWT temp files are not deleted before the JVM
exits).
> Creation of CustomFontMetricsMapper using File instead InputStream
> ------------------------------------------------------------------
>
> Key: FOP-2651
> URL: https://issues.apache.org/jira/browse/FOP-2651
> Project: FOP
> Issue Type: Improvement
> Reporter: Simone Rondelli
> Priority: Minor
> Attachments: FOP-2651.patch
>
>
> The constructor of {{CustomFontMetricMapper}} creates an instance of
> {{java.awt.Font}} using an InputStream. This led to the creation of a temp
> file with the content of the stream. This is repeated for every renderer for
> every rendering.
> Passing a file would avoid the creation of such a temp file reducing the
> overall rendering time.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)