Memory leak after manual compactions

219 views
Skip to first unread message

Rahul Arora

unread,
Oct 18, 2023, 9:53:20 PM10/18/23
to rocksdb
RocksDB 8.5.4 (RocksDBJni)

Hi, I'm using RocksDB for a read-heavy workload. After several tests, I found out that the reads work best if the DB is in the best state (compacted). So, I'm running manual compaction by running db.compactRange(CF) every 60 mins.

I also keep monitoring my system's memory usage by "sar -r 1" command. And, it clearly shows an increase in memory after every compaction which is never reclaimed.

Is this a known issue? or am I doing something wrong here?

Please let me know if more details are required.

MARK CALLAGHAN

unread,
Oct 18, 2023, 10:20:50 PM10/18/23
to Rahul Arora, rocksdb
I am not aware of a serious memory leak. What allocator do you use -- jemalloc and tcmalloc are far better at reducing RSS for RocksDB processes versus glibc malloc.
https://smalldatum.blogspot.com/2023/08/rocksdb-and-glibc-malloc-dont-play-nice.html

--
You received this message because you are subscribed to the Google Groups "rocksdb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rocksdb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rocksdb/d5b8978c-1092-4d4b-bb5a-bc901bc4819fn%40googlegroups.com.


--
Mark Callaghan
mdca...@gmail.com

Rahul Arora

unread,
Oct 19, 2023, 8:40:53 AM10/19/23
to rocksdb
I've been using the default one. The blogs gave some good insight on the memory allocators for Rocks. Will try using jemalloc / tcmalloc.

Thanks.

Rahul Arora

unread,
Oct 19, 2023, 12:27:23 PM10/19/23
to rocksdb
So, although running compactions manually does increase the memory by a small % I also tried turning the manual ones off completely and found out that the memory was still increasing at an almost constant rate. 

In my app, I'm only doing read-modify-write type of operations. Earlier, I was not closing the WriteBatch object which was leading to a memory leak every time I was writing something. Not sure what is it this time.

During the reads, I only use the ColumnFamilyHandle object (which inherited RocksObject that can caused the problem during writes earlier) which is only defined once and is reused multiple times. One important thing to note here is that I'm using multi-get operations. So, with all the keys, I also have to pass on their respective ColumnFamilyHandles to the method call. In my case, it's the same handle so before the call, I just make 'n' copies of the ColumnFamilyHandle object.

I'm guessing that it should not be the problem. 

I;m not using iterators. My filter and index blocks are cached in the block cache. My memtable size is 100 MB * 6. 

Any suggestions please?

Rahul Arora

unread,
Oct 19, 2023, 9:30:43 PM10/19/23
to rocksdb
After further analysis, I found nothing wrong while checking the properties for the various things like memtables, block cache, etc

I don't see unnecessary (deleted) files also using the 'lsof' command.

Adam Retter

unread,
Oct 20, 2023, 12:50:19 AM10/20/23
to Rahul Arora, rocksdb
Have you made sure that you are calling close on any object that inherits from RocksObject (once you are finished with it)?

Rahul Arora

unread,
Oct 20, 2023, 10:26:00 PM10/20/23
to rocksdb
Hi, yes, I'm closing all the objects that inherit RocksObject. From what I've figured out, the issue is in the read path somewhere. Writing doesn't take extra memory apart from the memtables.

Rahul Arora

unread,
Oct 20, 2023, 10:34:28 PM10/20/23
to rocksdb
I'll probably try using some other memory allocator once like Mark mentioned. Can someone suggest how can I do that though for JNI?

Ricardo Fonseca

unread,
Oct 21, 2023, 7:00:30 AM10/21/23
to Rahul Arora, rocksdb
Hello,

This might be a long shot, but I think there could be a memory leak if RocksDB::multiGet is called without passing a ReadOptions object as argument, since on the C++ side of rocksdbJNI there is an allocation of a ReadOptions object without a subsequent memory free (see here).

A quick test to see if it resolves your memory leak, is to allocate a single ReadOptions object and pass it as argument for RocksDB::multiGet each time.

Cheers,
Ricardo


Adam Retter

unread,
Oct 21, 2023, 7:22:43 AM10/21/23
to Ricardo Fonseca, Rahul Arora, rocksdb
It looks to me like ReadOptions is allocated on the Stack in
RocksDB::multiGet, and so it should be cleaned up when the scope
exits.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rocksdb/CAFpd3Woxh1-FR-RiitatvDzOG3PmCFjZt-LVBzY66E5_dZ6NtA%40mail.gmail.com.



--
Adam Retter

skype: adam.retter
tweet: adamretter
http://www.adamretter.org.uk

Ricardo Fonseca

unread,
Oct 21, 2023, 7:31:43 AM10/21/23
to Adam Retter, Rahul Arora, rocksdb
Ah yes, you are right. I misread the & as a *.

In that case there is no leak caused by ReadOptions.

Rahul Arora

unread,
Oct 22, 2023, 2:15:42 AM10/22/23
to rocksdb
I've ensured all RocksObjects are closed in the code after use. I'm adding a trimmed-down code of my application that mimicks how I interact with RocksDB. 

Hopefully, this will give a better insight.
RocksBackend.java

Rahul Arora

unread,
Oct 22, 2023, 4:51:12 AM10/22/23
to rocksdb
Found out that this PR (https://github.com/facebook/rocksdb/pull/11221) addresses the memory leaks after version 7.

Can someone please tell me if it Is gonna be pushed to Maven? else I can fork it and create my own build

Adam Retter

unread,
Oct 22, 2023, 9:08:22 AM10/22/23
to Rahul Arora, rocksdb
That PR is from my colleague Alan, as far as I can see it doesn't fix any memory leaks 

Alan Paxton

unread,
Oct 22, 2023, 9:26:09 AM10/22/23
to rocksdb
What Adam says, with a very minor exception. The PR https://github.com/facebook/rocksdb/pull/11221 is designed to make it more difficult to add leaks in future code.

--Alan

Rahul Arora

unread,
Oct 23, 2023, 2:29:28 AM10/23/23
to rocksdb
Oh got it. There does exist a memory leak in the versions >= 7 though. I've not been able to figure out why though. I tried using the latest 6.x version of Rocksdbjni but the performance was at least 20x worse than the latest rocksdbjni (8.5.4) version.

Any suggestions please?

Rahul Arora

unread,
Oct 23, 2023, 2:31:16 AM10/23/23
to rocksdb
Mentioning again that it's happening in the read flow only. And I don't see any RocksObject being used in the read flow except ReadOptions which, in my code, is defined only once.

Attaching my trimmed-down code here. Can someone please take a look at it and suggest something?
RocksBackend.java

Adam Retter

unread,
Oct 23, 2023, 4:11:28 AM10/23/23
to Rahul Arora, rocksdb
You would need to use appropriate profiling tools to identify where the memory leak is

Reply all
Reply to author
Forward
0 new messages