# DocumentSubsetReader is holding on to large number of objects

**URL:** <https://discuss.elastic.co/t/documentsubsetreader-is-holding-on-to-large-number-of-objects/379455>\
**Category:** Elasticsearch\
**Created:** [June 25, 2025, 4:47am UTC](https://discuss.elastic.co/t/documentsubsetreader-is-holding-on-to-large-number-of-objects/379455 "2025-06-25T04:47:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vjgorla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vjgorla/32/95775_2.png) [@vjgorla](https://discuss.elastic.co/u/vjgorla)\
**Post date:** [June 25, 2025, 4:47am UTC](https://discuss.elastic.co/t/documentsubsetreader-is-holding-on-to-large-number-of-objects/379455/1 "2025-06-25T04:47:58Z")

</div>

On one of our nodes, NUM\_DOCS\_CACHE in  
[elasticsearch/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReader.java at v8.11.1 · elastic/elasticsearch](https://github.com/elastic/elasticsearch/blob/v8.11.1/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReader.java) is holding on to 67,000 entries consuming 5GB of heap. Each entry in the map appears to be another cache which is holding on to small number objects, but because the cache is created with 256 CacheSegments by default, all these are adding up to consume a lot of memory.

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [June 25, 2025, 5:06am UTC](https://discuss.elastic.co/t/documentsubsetreader-is-holding-on-to-large-number-of-objects/379455/3 "2025-06-25T05:06:55Z")

</div>

_I deleted my older reply, I misread your message._

It sounds like you have a lot of segments for shards with Document Level Security enabled.

If the `DocumentSubsetDirectoryReader.NUM_DOCS_CACHE` has 67,000 entries, then that implies that you have 67,000 segments on that node for an index where users have DLS enabled.

Is that right? Do you really have that many segments?

---

<div class="post-metadata">

**Author:** ![vjgorla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vjgorla/32/95775_2.png) [@vjgorla](https://discuss.elastic.co/u/vjgorla)\
**Post date:** [June 25, 2025, 7:02am UTC](https://discuss.elastic.co/t/documentsubsetreader-is-holding-on-to-large-number-of-objects/379455/4 "2025-06-25T07:02:28Z")

</div>

I didn't check when I took the heap dump but right now each node has around 600 segments. We are having cluster stability issues with nodes going OOM on a regular basis, not sure if this is contributing to the problem.

We do use DLS with templated queries. But 67,000 segments doesn't sound right.

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [June 27, 2025, 3:11am UTC](https://discuss.elastic.co/t/documentsubsetreader-is-holding-on-to-large-number-of-objects/379455/5 "2025-06-27T03:11:16Z")

</div>

I've never seen this before, and the `NUM_DOCS_CACHE` should remove entries when segment readers are closed.  
If you truly have `67,000` entries in that map, but don't have 67,000 segments, then something is wrong.

Can you open a support case, and include any heap dumps you have?

You can include a link to this thread in your support case so whoever picks it up can reach out to me if they need to.
