Java Tomcat "OutOfMemoryError: Java heap space" caused by endless Sessions?
java, memory, memory-leaks, spring, tomcat
Solution
Wow I'm dumb. I had been working on a lightweight javascript resource loader for one day, but there were two glaring bugs:
- There was no retry limit.
- I forgot a "/" between the path and filename being loaded so it was endlessly requesting a non-existent file.
Anyways the loader was flooding the server with requests, which was only discovered by examining the tomcat access logs, as suggested by user @dimoniy, which revealed the horror:
127.0.0.1 - - [17/Dec/2013:17:39:34 -0500] "GET /myapp/ajax/loadjquery.js HTTP/1.1" 302 -
127.0.0.1 - - [17/Dec/2013:17:39:35 -0500] "GET /myapp/ajax/loadjquery.js HTTP/1.1" 302 -
127.0.0.1 - - [17/Dec/2013:17:39:37 -0500] "GET /myapp/ajax/loadjquery.js HTTP/1.1" 302 -
127.0.0.1 - - [17/Dec/2013:17:39:39 -0500] "GET /myapp/ajax/loadjquery.js HTTP/1.1" 302 -
So, basically I brought massive shame upon my family. Thanks to everyone who commented with suggestions!
Problem
I apologize for the title but I'm not quite sure how to ask this. I've recently started encountering "OutOfMemoryError: Java heap space" after leaving my app running in an embedded tomcat server for a very short while. I don't even have to query the server, I simply run `mvn clean install tomcat7:run` and leave the server running and the OutOfMemoryError is thrown after a few minutes. I ran the app through a profiler (VisualVM). The server is up and running roughly around the first increase in "Heap size", the "Used heap" just keeps growing after that and until bust. Okay, so it smells like a memory leak, but during my search for answers I came across this similar question which suggests adding a `SessionListener` as per this example. I added this class and listener tag, ran the server and things got interesting. Immediately after the server started, the log was flooded with: ``` ... 21:57:41.346 [http-bio-8080-exec-2] DEBUG c.f.p.system.LoggingSessionListener[34] - Session created: B9AC20CB3912D39D1AF8CEBC7D7F7ADD 21:57:41.346 [http-bio-8080-exec-2] DEBUG c.f.p.system.LoggingSessionListener[35] - Total sessions: 148 21:57:41.352 [http-bio-8080-exec-2] DEBUG c.f.p.system.LoggingSessionListener[34] - Session created: 1A4A4EB69585E99FAE3852D3AD9D4D22 21:57:41.353 [http-bio-8080-exec-2] DEBUG c.f.p.system.LoggingSessionListener[35] - Total sessions: 149 21:57:41.359 [http-bio-8080-exec-2] DEBUG c.f.p.system.LoggingSessionListener[34] - Session created: C48EE6DF9B0E3111AD38EAE864791C0C 21:57:41.359 [http-bio-8080-exec-2] DEBUG c.f.p.system.LoggingSessionListener[35] - Total sessions: 150 21:57:41.365 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[34] - Session created: D7384A391FBA06BE049AB4970EDB3E1E 21:57:41.365 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[35] - Total sessions: 151 21:57:41.370 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[34] - Session created: F153AE5CC427EA1E1D67FA934E54D7AB 21:57:41.370 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[35] - Total sessions: 152 21:57:41.375 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[34] - Session created: 70F1714A1010DE2AC1DAE37B191288B0 21:57:41.376 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[35] - Total sessions: 153 21:57:41.381 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[34] - Session created: F4C69A8E9A41CFDD59DB211BC431A409 21:57:41.381 [http-bio-8080-exec-6] DEBUG c.f.p.system.LoggingSessionListener[35] - Total sessions: 154 ... ``` So the app is endlessly creating sessions and the profiler confirms it: But now I'm stuck. I've tried disabling parts of the app which seem likely culprits, although there is nothing in the app that explicitly creates sessions. Any ideas of what could be causing this behavior, or a next step for extracting more information (such as where the Sessions are being created, if possible), will be massively appreciated, thanks!