r/JavaProgramming • • 2d ago

Why does my Java application become slow after running for several hours?

I'm working on a Java application that performs database operations and handles multiple user requests.

Initially, everything works fine, but after running for a few hours, response times increase significantly.

There are no major errors in the logs.

For experienced Java developers, what would you investigate first?

Would you check memory usage, garbage collection, database connections, or thread management?

9 Upvotes

9 comments sorted by

10

u/Least_Ad_1795 2d ago

I'd investigate memory usage, garbage collection, and database connection pooling first. Also, check for thread leaks and slow SQL queries. Java Flight Recorder (JFR), VisualVM, and thread dumps can help identify the bottleneck. Monitoring GC logs and connection pool metrics over time is especially useful for finding issues that appear after several hours.

1

u/AutoWiredMind_98 1d ago

Use jvm visualizer tool, then using a rest api client like postman bombard your service
Now in jvm visualizer tool u can see the live objects and other details

Analyze those and update the code to clean up those objects

1

u/Minecraftian14 2d ago

Memory, GC, DB waits, and stray threada is exactly what I'll look for 👀. What is your question?

1

u/nerydlg 1d ago

Do profiling on your app basically you need to monitore memory cpu and heap my guess is that you have a memory leak somewhere

1

u/fykup 1d ago

It's really hard to tell without instrumentation. Based on your description, I'd suspect a memory leak or increasing GC pressure. The JVM may be spending more and more CPU time on garbage collection, leaving fewer resources for actual application work.

But as others suggested, you really need to profile the application. JFR is a good starting point.

Another useful approach is to monitor the application continuously and see what changes over those few hours.

If you're using Spring Boot, Micronaut or Quarkus, you can try StatLite (I'm the author). It collects existing application metrics and gives you historical charts of request latency, error rates, CPU and JVM heap usage. You can correlate increasing latency with changes in memory and CPU usage.

It's lightweight (~15 MiB RSS at idle), can run on the same server, and doesn't require a Prometheus/Grafana stack.

It won't replace a profiler, but it can help you narrow down what to investigate and when.

1

u/Pale_Height_1251 1d ago

Run it through a profiler.

1

u/gm310509 9h ago

I would check for a memory leak.

Start out by monitoring the process size in your operating system's process list application.

If it is growing up to the maximum allowed by the JVM, then that is likely the problem and you will need to dig into that a bit deeper. Note that it will grow, the question is does it grow to the maximum and how much work is the GC needing to do to try and keep everything within that limit as memory gets tighter and tighter.

I assume that whatever queries you are running against the Database are not suffering any performance degradation (e.g. if you run them from a query submission utility).

Are you closing your resultsets when you are finished with them? All the databases I worked with released the memory associated with a resultSet when it went out of scope, but I remember one didn't (not sure why) unless you explicitly closed it. So, out of habit I will always now close my resultset, statement and if it is from a connection pool, the connection so as to return it to the pool.

1

u/sky_light62 1h ago

watch the post-GC heap floor over those hours first—if it keeps climbing instead of cycling flat, you've got retained references forcing constant GC work and JFR adds less than 1% overhead if you turn it on while the slowness builds.