Repository navigation
Description
Affects PMD Version:
Only tested on 7.16, 7.17. Bug does not exist on 6.26
Description:
CPD is throwing java.lang.OutOfMemoryError: Java heap space on several tested projects that work without issue on version 6.26 running with identical parameters. Occurs even when memory is set to 16GB.
Notably, on alsa-firmware 1.2.1, which only has 54 C files (~134k LoC), and AllegroOGG 1.0.3-39.fc43, which only has 96 C files (~60k LoC)
Also reproducible on some other codebases:
alpineemail 2.26
mysql-server 9.4.0
mongodb r8.2.1
Exception Stacktrace:
[TRACE] Tokenizing ./alsa/systems/alsa-1.2.1-7/code/alsa-firmware-1.2.1/ymfpci/fw_writer.c
[TRACE] Tokenizing ./alsa/systems/alsa-1.2.1-7/code/alsa-firmware-1.2.1/ymfpci/hwmcode.c
[DEBUG] Running match algorithm on 54 files...
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.HashMap.resize(HashMap.java:700)
at java.base/java.util.HashMap.putVal(HashMap.java:659)
at java.base/java.util.HashMap.put(HashMap.java:608)
at java.base/java.util.HashSet.add(HashSet.java:220)
at net.sourceforge.pmd.cpd.MatchCollector.registerTokenMatch(MatchCollector.java:119)
at net.sourceforge.pmd.cpd.MatchCollector.lambda$reportMatch$2(MatchCollector.java:103)
at net.sourceforge.pmd.cpd.MatchCollector$$Lambda$146/0x00000008400fc440.accept(Unknown Source)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at net.sourceforge.pmd.cpd.MatchCollector.reportMatch(MatchCollector.java:103)
at net.sourceforge.pmd.cpd.MatchCollector.collect(MatchCollector.java:57)
at net.sourceforge.pmd.cpd.MatchAlgorithm.lambda$findMatches$1(MatchAlgorithm.java:57)
at net.sourceforge.pmd.cpd.MatchAlgorithm$$Lambda$138/0x00000008400fec40.accept(Unknown Source)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1693)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at net.sourceforge.pmd.cpd.MatchAlgorithm.findMatches(MatchAlgorithm.java:53)
at net.sourceforge.pmd.cpd.CpdAnalysis.findMatches(CpdAnalysis.java:205)
at net.sourceforge.pmd.cpd.CpdAnalysis.performAnalysis(CpdAnalysis.java:164)
at net.sourceforge.pmd.cli.commands.internal.CpdCommand.doExecute(CpdCommand.java:136)
at net.sourceforge.pmd.cli.commands.internal.CpdCommand.doExecute(CpdCommand.java:27)
at net.sourceforge.pmd.cli.commands.internal.AbstractAnalysisPmdSubcommand$$Lambda$87/0x00000008400e4440.apply(Unknown Source)
at net.sourceforge.pmd.cli.internal.PmdRootLogger.executeInLoggingContext(PmdRootLogger.java:55)
at net.sourceforge.pmd.cli.commands.internal.AbstractAnalysisPmdSubcommand.execute(AbstractAnalysisPmdSubcommand.java:185)
at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:30)
at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:16)
at picocli.CommandLine.executeUserObject(CommandLine.java:2031)
Steps to reproduce:
curl -OL https://github.com/pmd/pmd/releases/download/pmd_releases%2F7.17.0/pmd-dist-7.17.0-bin.zip
unzip pmd-dist-7.17.0-bin.zip
alias pmd="./pmd-bin-7.17.0/bin/pmd"
curl -OL https://github.com/alsa-project/alsa-firmware/releases/download/v1.2.1/alsa-firmware-1.2.1.zip
unzip alsa-firmware-1.2.1.zip
export PMD_JAVA_OPTS="-Xmx16g"
pmd cpd --verbose --minimum-tokens 100 --no-fail-on-error --format xmlold --language cpp ./alsa-firmware-1.2.1 > alsa-1.2.1.xml 2> alsa-1.2.1-err.log
PMD 6.26, by contrast, can handle a minimum token threshold of 20 with no issue:
CLASS_PATH=$(find ./pmd-bin-6.26.0/lib -name '*.jar' | tr '\n' ':' | sed 's/:$//')
java -Xmx16g -cp "$CLASS_PATH" net.sourceforge.pmd.cpd.CPD --minimum-tokens 20 --skip-lexical-errors --format xml --language cpp --files ./alsa-firmware-1.2.1 > alsa-1.2.1.xml 2> alsa-1.2.1-err.log
Running PMD through: [CLI]
Additional Info
Running primarily on Ubuntu 22.04, although this behavior has been observed on Windows 11 as well
java --version
openjdk 24.0.2-internal 2025-07-15
Related older issues:
Activity
zbynek commentedon Oct 18, 2025
With the default memory setting it throws OOM by creating report for this single file: https://github.com/alsa-project/alsa-firmware/blob/master/emu/emu0404_netlist.h
jsotuyod commentedon Oct 19, 2025
Actually, PMD 7.0.0 should "work" too.
The issue however, is that any of those versions would also produce insane number of reports for those files, with very little actual value (see #2438).
For instance, the mentioned file (https://github.com/alsa-project/alsa-firmware/blob/master/emu/emu0404_netlist.h) has a particularly nefarious scenario. It's a single array with 54908 literals, with massive amounts of repetitions (ie: the exact line
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,appears 3233 times – over a total of 6889 lines… and that's not even considering different offsets).In order to produce meaningful reports, that is, only maximal and non-overlapping areas being reported, CPD needs to store some additional data as it goes through matches. The amount of memory is "small" but escalates linearly with the number of potential matches found that needs disambiguation. Unfortunately, on these worst-case scenarios, that can escalate very quickly.
There are 3 ways to improve on this, from worst to best:
--minimum-tokenswhich in turns reduces the number of matches… this however, has an impact on the results for all the code. Not really recommended.--ignore-literal-sequencesto have the lexer completely skip over all literal sequences (such as this one), getting rid of the issue, but maybe failing to detect other smaller literal sequences that shouldn't be duplicated. Probably the best alternative if you don't want to change the source code however./* CPD-OFF */comment (and an optional/* CPD-ON */at the end if there is more code in the file you want to analyze). See https://docs.pmd-code.org/latest/pmd_userdocs_cpd.html#suppressionzbynek commentedon Oct 19, 2025
@jsotuyod the 7.0.0 improvement introduces
tokenMatchSets-- do I understand correctly that these are essentially equivalence classes and could be deduplicated?POC patch
lukasgraef commentedon Oct 19, 2025
@zbynek
How much memory does the deduplication roughly save?
Could we combine that with the BitSet change from #6150 to save even more memory?
jsotuyod commentedon Oct 19, 2025
@zbynek yes, sort of. They are not exact matches, but "categories" from which we need to pick one match.
@lukasgraef yes, I think that usage of Bitset is actually very interesting. Very good insight. If you move that into a pr rather than a draft I'm happy to take a look at it.
lukasgraef commentedon Oct 19, 2025
@jsotuyod I've moved the PR to ready.
zbynek commentedon Oct 19, 2025
We have to deal with two different scenarios:
While Bitsets compress the information for the huge category, they are probably not the best for the standard case since storing just 2 numbers 1 and 100,000 may allocate 800,000 bytes of memory with default JDK implementation.
Deduplication should have negligible (positive) effect in the standard case and a massive one in the extreme case. I could verify that the OOM is gone, but I don't have more precise numbers yet.
lukasgraef commentedon Oct 19, 2025
There's also the SparseBitSet library (https://github.com/brettwooldridge/SparseBitSet) which claims to be more memory efficient than java.util.BitSet. Maybe that would fit better for both scenarios?
zbynek commentedon Oct 19, 2025
So the total heap dump is ~290MB and the hash map is ~280MB out of that for the single file.
SparseBitSet might improve that further.
adangel commentedon Oct 29, 2025
Alright, I did some testing with the various suggestions. Here are the results:
Version Explanation:
Summary:
Next steps (for 7.19.0):
Next steps (longer term):
and output the peak memory, timings (and maybe more stats) into the report or in an additional file
behind a CLI flag/ENV flag.
Detailed Results: Peak Memory Usage
Note:
-Xms500m -Xmx4g. That means, I don't have results for case 1/3 because of OOMCase 1: all without problematic files
-> 39 files, 900933 tokens
1.8.0_472:
25.0.1:
Case 2: only one file
-> 109828 tokens
1.8.0_472:
25.0.1:
Case 3: all files
-> 54 files, 2081189 tokens
1.8.0_472:
25.0.1:
Script:
Script
oowekyala commentedon Oct 29, 2025
I have a branch cpd-opts which improves by a lot the efficiency of CPD both in terms of memory and run time. For instance running on the full JDK sources is possible with < 900MB memory in < 6 seconds, whereas on master (even with #6150) it takes about 10GB memory and a minute. I'm preparing a PR, will use your scripts to compare to main @adangel.
adangel commentedon Feb 22, 2026
I'm moving this to 7.23.0. I'm still working on the pmd-regression-tester (see pmd/pmd-regression-tester#169 ), but that won't be finished for this Friday's release.