[ https://issues.apache.org/jira/browse/MNG-7899?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17774626#comment-17774626 ]
ASF GitHub Bot commented on MNG-7899: ------------------------------------- sebastien-doyon commented on PR #1269: URL: https://github.com/apache/maven/pull/1269#issuecomment-1760038298 Memory allocation profiling using [ConsoleMavenTransferListener_memalloc_Test.java](https://github.com/sebastien-doyon/maven/blob/MNG-7899_-_4_perf-tests/maven-embedder/src/test/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener_memalloc_Test.java) calling the ConsoleMavenTransferListener class 100 million times. This test shows a drastic decrease of : - char[] allocation (before : **190 GiB**, after : **85,4 GiB**) - java.lang.String allocation (before : **45,3 GiB**, after : **3,8 GiB**) - java.lang.StringBuffer allocation (before : **65,4 GiB**, after : **1,14 GiB**) - java.text.DecimalFormat allocation (before : **27,4 GiB**, after : **none recorded**) - more... Here the JMC capture for the initial code (file available [here](https://github.com/sebastien-doyon/maven/raw/MNG-7899_-_4_perf-tests/maven-embedder/recording-initial.jfr)) <img width="1237" alt="Capture d’écran 2023-10-12 à 18 52 20" src="https://github.com/apache/maven/assets/2573779/9ba678bd-d5bc-41c2-82d8-79f2984d880e"> Here the JMC capture for the optimised code (file available [here](https://github.com/sebastien-doyon/maven/raw/MNG-7899_-_4_perf-tests/maven-embedder/recording-optimized.jfr)) <img width="1237" alt="Capture d’écran 2023-10-12 à 18 52 30" src="https://github.com/apache/maven/assets/2573779/525eff55-386f-4175-805e-12d8d8b02ef7"> To reproduce: - checkout the [MNG-7899-profiling-initial](https://github.com/sebastien-doyon/maven/tree/MNG-7899-profiling-initial) tag - execute the following commands : ``` mvn clean verify -Drat.skip=true -DskipTests mvn test -Drat.skip=true -pl :maven-embedder -Dtest=**/ConsoleMavenTransferListener_memalloc_Test -Dspotless.check.skip ``` - open the `maven-embedder/recording-initial.jfr` file with JMC - checkout the [MNG-7899-profiling-optimised](https://github.com/sebastien-doyon/maven/tree/MNG-7899-profiling-optimised) tag - execute the following commands : ``` mvn clean verify -Drat.skip=true -DskipTests mvn test -Drat.skip=true -pl :maven-embedder -Dtest=**/ConsoleMavenTransferListener_memalloc_Test -Dspotless.check.skip ``` - open the `maven-embedder/recording-optimized.jfr` file with JMC > Various memory usage improvements > --------------------------------- > > Key: MNG-7899 > URL: https://issues.apache.org/jira/browse/MNG-7899 > Project: Maven > Issue Type: Improvement > Components: Design, Patterns & Best Practices, Embedding, > General, Logging > Affects Versions: 4.0.0-alpha-2 > Reporter: sebastien > Priority: Major > > Some optimisations can be applied to the code to reduce the use of temporary > objects. > Typical improvements identified are: > * reduce scope of temporary objects creation to avoid creating when not > needed. Example : > {code:java} > public String toString() { > StringBuilder sb = new StringBuilder(512); > if (isEmpty()) { > return "empty"; > } > for (MetadataGraphVertex v : vertices) { > .....{code} > can be replaced by > {code:java} > public String toString() { > if (isEmpty()) { > return "empty"; > } > StringBuilder sb = new StringBuilder(512); > for (MetadataGraphVertex v : vertices) { > .....{code} > * Reuse StringBuilder objects in loops by setting its length to zero > * Use the StringBuilder.append() with index to avoid String.substring(). > Example: > > {code:java} > int idx = resourceName.lastIndexOf('/'); > buffer.append(idx < 0 ? resourceName : resourceName.substring(idx + 1)); > {code} > can be replaced by > > {code:java} > int idx = resourceName.lastIndexOf('/'); > if (idx < 0) { > buffer.append(resourceName); > } else { > buffer.append(resourceName, idx + 1, resourceName.length()); > } {code} > > > * Replace dynamic string creation static constants when possible > * Avoid creating temporary strings with + operator when the final > destination can be used instead, like PrintStream.print() method > * Avoir using StringBuilder.append() method and util method MessageUtils.a() > when the final destination can be used instead, like PrintStream.print() > method > -- This message was sent by Atlassian Jira (v8.20.10#820010)