Tomcat – Web application with Hibernate, Spring and Oracle goes up to 99% of the CPU

hibernatetomcat

We have an web application in Linux environment that the CPU sometimes goes up to 99%.

Sometimes it takes days, and other times it takes minutes. We are using Hibernate with Spring in a tomcat webapp and an Oracle Database.

Checking the logs it appears the following:

"ConnectionManager – transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!".

And then the sessioncount starts to grow until 256 sessions (the max allowed by our Apache confs). This is the line that appears when the session count gets 256:

"ContainerBackgroundProcessor[StandardEngine[Catalina]] ManagerBase – Start expire sessions StandardManager at 1259947978384 sessioncount 256"

After that the CPU gets 99%.

Any suggestions? All will be HIGHLY appreciated.

Thanks in advance.

Best Answer

There's lots of things it might be. My experience is this is often due to memory problems. When the used heap size gets large the garbage collector goes into overdrive and CPU performance really suffers. Some steps to isolate the issue:

  • monitor the system while the CPU use is spiking using top. Confirm that the CPU use is from Java. (Unfortunately, you can't be more precise than that).

  • monitor the heap size use with VisualVM or JConsole. Normal behavior is to see heap size gradually increasing, then suddenly spiking down when the GC kicks in (a sawtooth pattern). If memory use stays high, it can't be garbage collected

  • this may be obvious, but checks your logs for OutOfMemoryExceptions

  • Make sure your VM is using the max possible heap size. For a 32bit JVM, this is 1400 or 1500M. Use the JVM option -Xmx1500M.

  • Make sure the OS is not using much swap memory (check with top or free). Make sure it has adequate memory to run Linux and Tomcat (3 or 4 GB is a good start for a 32bit machine).

One more note -- review your application for memory and connection leaks. Spring will take care of much of this for you. Make sure you are not putting stuff in the HttpSession object (or a map in the Session) which doesn't need to be there. If you do any direct JDBC or file handling, make sure all streams, Connections and PreparedStatements (this is what I always forget) are closed.

Related Topic