Bukama commented on issue #368:
URL: 
https://github.com/apache/maven-checkstyle-plugin/issues/368#issuecomment-7342992635

   The following comment was created by AI after I let it analyse the issue and 
the technical details supply your thoughts as well. So I'll close this as as 
not planed.
   
   ---
   
   
   I looked into this, and the retained memory is caused by Checkstyle, not by 
the plugin.
   
   ### Root cause
   
   The ANTLR-generated parsers in Checkstyle keep their prediction caches in 
**static** fields (checked with `javap` against `checkstyle-9.3.jar`, the 
plugin's default version; newer versions are generated the same way):
   
   ```java
   // JavaLanguageParser, JavaLanguageLexer, JavadocParser
   protected static final DFA[] _decisionToDFA;
   protected static final PredictionContextCache _sharedContextCache;
   ```
   
   These caches grow with every file that gets parsed. On a large code base 
like Jenkins they easily reach the 160–180 MB seen in the heap dump.
   
   Static fields are only freed when their class is unloaded. Maven caches the 
plugin's ClassRealm for the whole build session, so the Checkstyle classes stay 
loaded and the caches stay reachable until the build ends.
   
   The plugin already calls `Checker.destroy()` once processing is done 
([`DefaultCheckstyleExecutor`](https://github.com/apache/maven-checkstyle-plugin/blob/master/src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java)).
 That releases the `Checker`/`TreeWalker` instances, but it can't touch static 
state. Checkstyle itself never calls `clearDFA()` or clears these caches 
anywhere.
   
   ### Why the plugin shouldn't clear the caches itself
   
   In theory the plugin could add its own cleanup step after 
`checker.destroy()`. For example, it could create a dummy parser instance and 
call `getInterpreter().clearDFA()`, then reset `_sharedContextCache` via 
reflection, since `PredictionContextCache` has no public `clear()`. I don't 
think we should do that:
   
   - **It depends on Checkstyle internals.** The plugin would have to know the 
generated parser/lexer class names and rely on ANTLR runtime internals (the 
protected `cache` field). Users regularly override the Checkstyle version, so 
this would break silently whenever the grammar classes or the ANTLR version 
change.
   - **It isn't safe in parallel builds.** With `-T`, all modules running 
Checkstyle share the same static caches. If one thread clears them while 
another is still parsing, it's modifying the unsynchronized `HashMap` behind 
`PredictionContextCache` at the same time. That can corrupt it or make parsing 
fail.
   - **It makes multi-module builds slower.** The DFA cache is what makes ANTLR 
fast after warm-up. Clearing it after every execution means every module has to 
rebuild it from scratch, which makes large reactor builds noticeably slower.
   - **It's in the wrong layer.** Only Checkstyle knows when its parsers are no 
longer needed and how to release their state safely.
   
   ### Suggested way forward
   
   This should be fixed in Checkstyle. Possible approaches there:
   
   - have `TreeWalker.destroy()` / `Checker.destroy()` clear the ANTLR DFA and 
prediction context caches, or
   - hold the caches per parser/`TreeWalker` instance instead of in static 
fields
   
   Once Checkstyle ships a fix, the plugin only needs a version bump. Until 
then, affected builds can work around it by giving Maven more heap or by 
running Checkstyle in a separate Maven invocation.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to