The chart shows activeSessions from Catalina:type=Manager,host=localhost,context=/<app> climbing in a near-straight line. No plateau. You restarted Tomcat this morning, and by midafternoon the count is already back to where it was before the restart. Sessions are being created faster than they expire, and with the default 30-minute session-timeout, expiry is slow enough that the slope looks gentle until you compare it to the request rate.

This is a session leak, not a traffic spike. A spike produces a step up that plateaus at roughly peak_creation_rate * session_timeout. A leak has no plateau: the line keeps rising after traffic drops because sessions that should have expired are still pinned, or because new sessions are being minted faster than the background expiry loop can clear them.

The end state is mechanical. Tomcat’s default StandardManager keeps sessions as in-memory objects on the heap. Monotonic session growth is monotonic heap growth. Eventually the post-GC heap baseline rises to the point that Full GCs dominate execution, throughput collapses, and the JVM either throws OutOfMemoryError or livelocks. The session leak is rarely the incident you get paged for; the GC death spiral is. By the time you see the spiral, the session chart has been telling the story for hours.

The diagnostic path: confirm the leak is real (not a slow-expiry lag), then bucket the cause into one of three: the application never invalidates, expiry is blocked, or session creation is pathological.

What this means

A Tomcat session under StandardManager is a StandardSession held in a ConcurrentHashMap keyed by session ID. It exists from createSession() until expire() runs. Expiry is triggered in three ways:

  1. The container background process expires sessions whose lastAccessedTime + maxInactiveInterval is in the past. With default settings this runs roughly every minute .
  2. Application code calls HttpSession.invalidate().
  3. The webapp is undeployed, expiring every session for that context.

If activeSessions is rising without plateau, one of these is failing to keep up with creation. Read the Manager MBean directly. activeSessions is not in the Manager status XML; you need JMX. Adjust the host and context in the object name to match your deployment.

# Read active and expired session counts for the ROOT context
java -jar jmxterm.jar -l localhost:9090 -n -v silent -e \
  "get -b Catalina:type=Manager,host=localhost,context=/ activeSessions expiredSessions"

Pair activeSessions with expiredSessions. The cumulative expiredSessions counter tells you whether expiry is happening at all:

  • expiredSessions is rising, but activeSessions still climbs: expiry is working, but creation is outpacing it. Look at the creation side (bots, JSP defaults, login churn).
  • expiredSessions is flat or barely moving while activeSessions climbs: expiry is broken or blocked. Look at the background thread, listener deadlocks, or a misconfigured timeout.

The rejectedSessions counter tells you whether maxActiveSessions is set. With the default of -1 (unlimited), there is no backstop; the count climbs until the heap runs out. rejectedSessions > 0 means a limit is configured and Tomcat is shedding new session creations. Users see errors, but the heap is protected.

flowchart td
    A[activeSessions rising, no plateau] --> B{expiredSessions rising too?}
    B -- Yes --> C[Creation outpaces expiry: bots, JSP session=true, login churn]
    B -- No --> D[Expiry broken: jstack background thread]
    D --> E{Thread blocked in listener?}
    E -- Yes --> F[HttpSessionListener deadlock]
    E -- No --> G[Verify session-timeout in web.xml]
    C --> H[Confirm slope drops after fix]
    F --> H
    G --> H

Common causes

CauseWhat it looks likeFirst thing to check
Application never calls invalidate() on logoutactiveSessions tracks active users, sessionMaxAliveTime is close to the configured timeout, expiredSessions rises slowlyGrep application code for session.invalidate() and audit logout paths
Crawler or bot session mintingHigh correlation between bot request volume and activeSessions slope; access log shows many requests with no JSESSIONID cookieFilter access log by user-agent and session-cookie presence
HttpSessionListener deadlock blocking expiryexpiredSessions flat; thread dump shows the container background processor thread BLOCKED inside a session listenerjstack the JVM and inspect the container background processor thread
JSP pages creating implicit sessionsEvery JSP hit mints a session, even for anonymous content; slope tracks total hits, not authenticated hitsGrep .jsp files for <%@ page session= directives
maxActiveSessions not configuredrejectedSessions always 0; no backstop before heap exhaustsConfirm context.xml or <Manager> has no maxActiveSessions attribute
Session timeout far longer than user activitysessionMaxAliveTime very high relative to typical sessions; long-running idle sessions accumulateInspect <session-timeout> in web.xml and any per-context overrides

Quick checks

All read-only except the heap dump. Run these before changing anything. If multiple Tomcat instances run on the host, target the correct PID instead of relying on pgrep.

# Per-context session attributes via JMX
java -jar jmxterm.jar -l localhost:9090 -n -v silent -e \
  "get -b Catalina:type=Manager,host=localhost,context=/ activeSessions expiredSessions rejectedSessions sessionMaxAliveTime sessionAverageAliveTime"

# Confirm expiry is actually happening: sample twice, 5 minutes apart
for i in 1 2; do
  java -jar jmxterm.jar -l localhost:9090 -n -v silent -e \
    "get -b Catalina:type=Manager,host=localhost,context=/ expiredSessions"
  sleep 300
done

# Heap pressure correlate: post-GC old gen baseline
jstat -gcutil $(pgrep -f 'catalina.startup.Bootstrap') 1000 5

# Thread state of the container background processor
jstack $(pgrep -f 'catalina.startup.Bootstrap') | \
  grep -A 5 -E "Catalina-utility|ContainerBackgroundProcessor"
# Top client IPs by request volume (proxy for bot/crawler activity)
awk '{print $1}' /var/log/tomcat/localhost_access_log.$(date +%Y-%m-%d).txt | \
  sort | uniq -c | sort -rn | head

# To check JSESSIONID cookie presence per request, the access log Valve must
# include %{Cookie}i in its pattern. The default pattern does not log cookies.

# Find JSP pages that do not opt out of session creation
grep -rni '<%@.*page.*session' $CATALINA_BASE/webapps/ 2>/dev/null | grep -v 'session="false"'

# Check configured session timeout and any per-context session limits
grep -A 2 'session-config' $CATALINA_BASE/conf/web.xml
grep -rni 'session-timeout\|maxActiveSessions' \
  $CATALINA_BASE/conf/ $CATALINA_BASE/webapps/*/WEB-INF/web.xml 2>/dev/null

How to diagnose it

  1. Confirm the slope is real, not a traffic artifact. Plot activeSessions alongside the request rate. A leak has the session line rising while the request rate is flat or dropping. A pure traffic spike plateaus within roughly one session-timeout of the traffic peak.

  2. Confirm expiry is happening. Sample expiredSessions twice, a few minutes apart. If the counter does not move at all, expiry is blocked; go to step 4. If it moves but activeSessions keeps rising, expiry is overwhelmed; go to step 3.

  3. If expiry is overwhelmed, identify the creation source. The fastest signal is the access log. Group requests by client IP and user-agent and count how many arrive without a JSESSIONID cookie. Crawlers that ignore cookies are the classic offender; each request mints a new session. If the application uses JSPs, verify that pages not needing a session have <%@ page session="false" %>.

  4. If expiry is blocked, take a thread dump immediately. The container runs session expiry on a background thread. A thread dump showing that thread BLOCKED inside an HttpSessionListener.sessionDestroyed call means listener code is holding a lock or deadlocking with a request thread. Frameworks that wrap the session lifecycle (some Vaadin Flow versions are a documented case ) have hit this pattern.

  5. If neither creation nor expiry is obviously wrong, capture a heap dump before restarting.

    # WARNING: the :live option triggers a Full GC and pauses the JVM.
    # On JDK 11+ prefer: jcmd <pid> GC.heap_dump /path/to/heap.hprof
    jmap -dump:live,format=b,file=heap.hprof $(pgrep -f 'catalina.startup.Bootstrap')
    

    Inspect with Eclipse MAT. Look for retained StandardSession instances and what holds them. The leak is occasionally a custom HttpSessionActivationListener or a static collection caching session references.

  6. Do not restart as the first move. A restart clears the symptom and destroys the evidence. The thread dump and heap dump are what tell you which fix to deploy.

Metrics and signals to monitor

SignalWhy it mattersWarning sign
activeSessions (per context)Direct measure of in-memory session countMonotonic rise without plateau over multiple session-timeout windows
expiredSessions (per context)Confirms the expiry loop is runningFlat or near-flat while activeSessions rises
rejectedSessions (per context)Tells you whether maxActiveSessions is set and being hitNon-zero means a limit exists and is shedding load
sessionMaxAliveTimeLongest-lived session observedClose to session-timeout suggests sessions live until forced expiry; far higher suggests a listener defect
JVM old gen post-GC baselineSessions are heap-resident; this is where leaks surfaceValley of the sawtooth trending up over hours
Full GC frequencyLate-stage signal of heap pressureAny sustained Full GC on G1 warrants investigation
Request rate without JSESSIONIDProxy for new-session creation rateDisproportionate to expected first-visit traffic

Fixes

Application never calls invalidate()

Add explicit session.invalidate() to logout and session-abandonment paths. Verify by watching expiredSessions rise during a controlled logout test. If session-timeout is long (hours) and most sessions are abandoned rather than logged out, lower the timeout to match actual user dwell time.

Tradeoff: lowering session-timeout shortens the idle window before users are forced to log in again. Coordinate with product.

Crawler or bot session minting

Install CrawlerSessionManagerValve in server.xml or context.xml. It identifies crawler user-agents and binds all their requests to a single session per client, collapsing the per-request session explosion .

Alternatively, mark crawler-facing pages with <%@ page session="false" %> so they do not mint a session at all. For endpoints that genuinely need no session (health checks, static content), ensure they do not call getSession(true).

HttpSessionListener deadlock

This requires code or framework changes. If the deadlock is in a framework, upgrade past the affected version. If the deadlock is in application listener code, remove the blocking work from sessionDestroyed: do it asynchronously, or before invalidation.

Tradeoff: moving work out of sessionDestroyed means it no longer runs under the session’s transactional context. Refactor accordingly.

Implicit JSP session creation

Audit JSPs and add <%@ page session="false" %> to any page that does not read or write session state. This is cheap and prevents the most common content-page leak.

Configure maxActiveSessions as a backstop

Set <Manager maxActiveSessions="..." /> in context.xml to a value sized to your heap. This does not fix the leak; it converts an OOM into user-visible errors and increments rejectedSessions, which your monitoring can alert on. Use it as a safety net, not a fix.

Prevention

  • Alert on monotonic growth. The cleanest rule: slope of activeSessions is positive across three consecutive session-timeout windows. This eliminates false positives from a single traffic spike.
  • Alert on expiry flatness. If expiredSessions is not moving over a window of a few minutes during which traffic is arriving, expiry is broken.
  • Alert on rejectedSessions > 0. Even one rejected session means a configured limit is being hit and real users are seeing failures.
  • Always set maxActiveSessions. It is the difference between slow degradation and a hard OOM. Size it from (post-GC heap headroom) / (average session size).
  • Audit JSPs for session="false" as part of deployment review.
  • Track framework session lifecycle bugs. For frameworks that hook session lifecycle, monitor upstream issue trackers for known deadlock or retention bugs.
  • Capture heap dumps on OOM. Use -XX:+HeapDumpOnOutOfMemoryError. The session leak shows up clearly in retained StandardSession objects.

How Netdata helps

  • Per-second JMX collection of activeSessions, expiredSessions, and rejectedSessions per context makes the slope visible before GC pressure starts.
  • Correlate session counts with the old-gen post-GC baseline in the same time window to confirm whether sessions are the heap consumer or a coincident signal.
  • Alert directly on rejectedSessions > 0 to catch when maxActiveSessions is being hit, rather than discovering it during an incident.
  • Per-second resolution distinguishes “expiry is slow” from “expiry is blocked”; minute-level granularity can hide the difference.