r/JavaProgramming • • 4d 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

11 comments sorted by

11

u/Least_Ad_1795 3d 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 3d 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 4d ago

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

1

u/nerydlg 3d 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 3d 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 3d ago

Run it through a profiler.

1

u/gm310509 2d 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/koral_blue_ 1d ago

Java tiende a consumir recursos con gula...

1

u/jonathaz 1d ago

I suspect database queries that run slowly, perhaps hanging, timing out, etc. Over time more connections from the pool and more database resources get busy, and new requests spend more and more time waiting for an available connection from the pool. It could be certain queries that always run slowly, or combinations of variables that make a query take a slow path. Or if the app is also doing inserts or updates, deadlock is a fun one.