Commit 9ebdec6
committed
fix: memory leak in InMemorySessionService.deleteSession() - clean up empty parent maps
When deleteSession() removes the last session for a userId, the now-empty
userId map remained in the parent appName map. Similarly, if all users under
an appName were deleted, the empty appName map remained in the top-level
sessions map. This caused linear memory growth with the number of unique
users/apps, eventually leading to OutOfMemoryError.
Replace the manual get+remove pattern with computeIfPresent() calls that
atomically remove parent map entries when they become empty. When the
remapping function returns null, ConcurrentHashMap automatically removes
the key - this is thread-safe without additional locking.
Before:
sessions = { appName: { userId: {} } } <-- empty maps leak
After:
sessions = {} <-- fully cleaned up
Add two new tests:
- deleteSession_cleansUpEmptyParentMaps: verifies via reflection that the
internal sessions map is completely empty after deleting the only session
- deleteSession_doesNotRemoveUserMapWhenOtherSessionsExist: verifies that
a userId entry is NOT pruned when the user still has remaining sessions
Fixes #6871 parent 0b5ac92 commit 9ebdec6
File tree
2 files changed
+58
-7
lines changed- core/src
- main/java/com/google/adk/sessions
- test/java/com/google/adk/sessions
2 files changed
+58
-7
lines changedLines changed: 10 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
202 | 205 | | |
203 | 206 | | |
204 | 207 | | |
| |||
Lines changed: 48 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
247 | 248 | | |
248 | 249 | | |
249 | 250 | | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
250 | 298 | | |
0 commit comments