# How to calculate memory consumption when using the Lucene Engine for KNN vectors?

**URL:** <https://forum.opensearch.org/t/how-to-calculate-memory-consumption-when-using-the-lucene-engine-for-knn-vectors/18854>\
**Category:** k-NN\
**Created:** [April 12, 2024, 1:47pm UTC](https://forum.opensearch.org/t/how-to-calculate-memory-consumption-when-using-the-lucene-engine-for-knn-vectors/18854 "2024-04-12T13:47:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulCampbell](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@PaulCampbell](https://forum.opensearch.org/u/PaulCampbell)\
**Post date:** [April 12, 2024, 1:47pm UTC](https://forum.opensearch.org/t/how-to-calculate-memory-consumption-when-using-the-lucene-engine-for-knn-vectors/18854/1 "2024-04-12T13:47:12Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):  
AWS OpenSearch version 2.11

**Describe the issue** :  
In our initial implementation of semantic search, we stuck with the default property mappings for storing KNN vectors. We quickly hit memory limits with the KNN circuit breaker triggering, and had to significantly increase the size of our data node instances. We have been tracking memory usage using the API `/_plugins/_knn/stats?pretty.`

We recently switched to use the Lucene engine (from nmslib) to enable “Efficient Filtering,” and have found that the memory usage stats are no longer populated. I found that an existing defect has been logged here: [[BUG] KNN stats empty on cluster · Issue #1279 · opensearch-project/k-NN · GitHub](https://github.com/opensearch-project/k-NN/issues/1279).

I have also come across an article that suggests that when using the Lucene engine, the graph structure and the actual vectors are stored in separate segments, and only the graph structure is loaded into memory, not the vectors. If this is the case, it could alleviate our concerns over memory consumption and potentially save money on data node instances. Can someone confirm if this is indeed the case?

> **[Expanding k-NN with Lucene approximate nearest neighbor search](https://opensearch.org/blog/Expanding-k-NN-with-Lucene-aNN/)**
>
> OpenSearch is a community-driven, Apache 2.0-licensed open source search and analytics suite that makes it easy to ingest, search, visualize, and analyze data.

**Configuration** :

**Relevant Logs or Screenshots** :

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/e/e60e318662da98415ee28aa10d47ec45c58fb335.png)

---

<div class="post-metadata">

**Author:** ![Navneet](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/navneet/32/3645_2.png) [@Navneet](https://forum.opensearch.org/u/Navneet)\
**Post date:** [April 12, 2024, 7:28pm UTC](https://forum.opensearch.org/t/how-to-calculate-memory-consumption-when-using-the-lucene-engine-for-knn-vectors/18854/2 "2024-04-12T19:28:00Z")

</div>

So for Lucene the KNN stats api doesn’t work. The loading of both graphs and vectors in RAM(outside of heap) is controlled by Operating system via mmap system call. Hence atleast for now there is no way to understand how much memory Lucene HNSW is using at a point in time.

But having said that the memory which is required by Lucene to do HNSW still remains the same, which is pointed out in this formula: [k-NN index - OpenSearch Documentation](https://opensearch.org/docs/latest/search-plugins/knn/knn-index/#hnsw-memory-estimation)

So a rule of them you can use is to ensure that you nodes have enough ram(removing JVM) to hold these graphs and vectors. But as Operating system needs some other files to be loaded in RAM then graph files may be swapped out(this can lead to increase in latency).

This is where engines like nmslib and faiss are little better which ensure that graph files are always loaded in memory.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [June 11, 2024, 7:28pm UTC](https://forum.opensearch.org/t/how-to-calculate-memory-consumption-when-using-the-lucene-engine-for-knn-vectors/18854/3 "2024-06-11T19:28:11Z")

</div>

Closed on June 11, 2024, 7:28pm UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
