Introduction In enterprise Java applications, string concatenation with the + operator inside loops continues to cause performance issues. This article describes five common performance mistakes detected in real applications, provides profiling metrics, and offers practical solutions. It also includes a step-by-step guide to setting up Java 17 and IntelliJ IDEA on Mac and a list of profiling tools used in professional environments.
About Q2BSTUDIO Q2BSTUDIO is a software development company specializing in custom applications and custom software. We offer solutions that incorporate artificial intelligence, cybersecurity, aws and azure cloud services, business intelligence services, AI for enterprises, AI agents, and power bi. Our team combines expertise in software architecture, performance optimization, and analysis through real profiling to solve critical issues in production.
Pitfall 1 String concatenation in loops Frequent problem: using the + operator inside loops generates multiple temporary String objects and high memory pressure. Typical metrics observed in real cases include high allocation rates (for example 300 MB per minute), increased GC time, and increased p99 latencies. Solution: replace with StringBuilder when concatenation is cumulative or use String.join and formats that avoid unnecessary copies. Expected result in real profiling: reduction of allocation rate by 70 to 95 and decrease of GC pauses.
Pitfall 2 Poorly sized collections and excessive synchronization Problem: creating ArrayList without known initial size or using synchronized collections in critical paths causes rehashing, copies, and lock contention. Metrics: increased CPU time in insertion operations and higher latency under concurrent load. Solution: pre-size collections with an estimated capacity, use appropriate concurrent collections (ConcurrentHashMap, ConcurrentLinkedQueue), and avoid global locks. In real load tests this significantly reduces average and p95 latencies and improves throughput.
Pitfall 3 Excessive object creation and pressure on GC Problem: design patterns that generate many ephemeral objects (DTOs recreated on each request, unnecessary wrappers) increase GC frequency and affect latency. Observed metrics: increased GC pauses and CPU time dedicated to the collector. Solution: reuse objects when safe, apply pooling in specific cases, use primitive types and memory-efficient structures. With allocation profiling in real environments, it is possible to reduce the allocation rate and improve p99.
Pitfall 4 Database access without batching or optimized connection Problem: executing many individual queries in loops without batching or without PreparedStatement generates high latencies and CPU consumption on the database server. Metrics: increased round trips, per-query latencies, and lower overall throughput. Solution: use batch inserts and updates, PreparedStatements, connection pooling, and measure percentile latencies (p50 p95 p99). In real experiences, batching and proper pooling have reduced total processing times by 40 to 80 depending on the case.
Pitfall 5 Blocking I/O and inefficient thread management Problem: using blocking I/O on servers with a high number of connections causes thread blocking and increased memory usage due to thread stacks. Common metrics: high number of threads in RUNNABLE or BLOCKED state and increasing latencies when resources are saturated. Solution: migrate to NIO or reactive models for I/O, use concurrency limits and backpressure queues. In real systems this improves CPU utilization and reduces latencies under peaks.
How real profiling helps find and fix problems Profiling in representative environments allows locating CPU hot spots, identifying allocation hotspots, measuring latencies, and observing lock contention. Key metrics to capture: allocation rates (MB/s), GC times, latency percentiles (p50 p95 p99), CPU samples per method, and lock counts. With this information, changes with the greatest impact are prioritized and improvement is validated after deploying optimizations.
Recommended profiling tools Java Flight Recorder and JDK Mission Control for JVM tracing in production; async profiler for CPU samples and allocation profiling with low overhead; VisualVM for quick inspection; YourKit and JProfiler for deep analysis in development environments. Complement with APM metrics and structured logging to correlate distributed traces.
Quick guide to setting up Java 17 and IntelliJ IDEA on Mac Step 1 Install Homebrew if not available. Step 2 Install JDK 17 with the command brew install openjdk@17 and then export JAVA_HOME to the path indicated by brew. Step 3 Download and install IntelliJ IDEA from the official site or use the toolbox version. Step 4 In IntelliJ create a new SDK pointing to the Java 17 installation and configure the project to use that JDK. Step 5 Enable Java Flight Recorder in test environments by adding the appropriate JVM options or starting from IntelliJ with the chosen profiler. Step 6 Install and configure profiling tools such as async profiler or YourKit and run profiling sessions on representative load scenarios.
Example cases and results Case 1 Optimizing string concatenation in a batch service reduced temporary object allocation by 90 and improved total processing time from 18 minutes to 5 minutes. Case 2 Pre-sizing collections and removing global synchronization improved throughput by 3x in a highly concurrent microservice. Case 3 Batching JDBC statements and using a connection pool reduced average latency per request by 60 and decreased database load.
Summary of best practices Avoid concatenation with + in loops; pre-size collections; reduce creation of ephemeral objects; apply batching in data access; migrate to non-blocking I/O when necessary; measure with real profiling tools before and after each change; automate representative load tests.
How Q2BSTUDIO can help At Q2BSTUDIO we offer performance auditing services, optimization of custom applications, and consulting in custom software. We implement solutions that integrate artificial intelligence for predictive performance analysis, AI agents for intelligent monitoring, aws and azure cloud services, and visualization with power bi in business intelligence pipelines. We also provide safe refactorings and regression tests to validate performance improvements in real environments.
Conclusion Performance issues in Java in enterprise environments are often due to patterns that are easy to fix but difficult to detect without real profiling. Adopting good coding practices, accompanied by systematic profiling and appropriate tools, allows achieving significant reductions in latency, memory usage, and operational costs. Q2BSTUDIO accompanies companies throughout the entire cycle from diagnosis to implementation of scalable and intelligent solutions.




