From 389e66bef56b4f81d3c9683469acb5affc32bd7f Mon Sep 17 00:00:00 2001 From: Changyu Bi Date: Mon, 30 Sep 2024 10:27:45 -0700 Subject: [PATCH] Add comment for memory usage in BeginTransaction() and WriteBatch::Clear() (#13042) Summary: ... to note that memory may not be freed when reusing a transaction. This means reusing a large transaction can cause excessive memory usage and it may be better to destruct the transaction object in some cases. Pull Request resolved: https://github.com/facebook/rocksdb/pull/13042 Test Plan: no code change. Reviewed By: jowlyzhang Differential Revision: D63570612 Pulled By: cbi42 fbshipit-source-id: f19ff556f76d54831fb94715e8808035d07e25fa --- include/rocksdb/utilities/transaction_db.h | 5 ++++- include/rocksdb/write_batch.h | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/rocksdb/utilities/transaction_db.h b/include/rocksdb/utilities/transaction_db.h index 70226664005..78ff0816ff6 100644 --- a/include/rocksdb/utilities/transaction_db.h +++ b/include/rocksdb/utilities/transaction_db.h @@ -460,7 +460,10 @@ class TransactionDB : public StackableDB { // // If old_txn is not null, BeginTransaction will reuse this Transaction // handle instead of allocating a new one. This is an optimization to avoid - // extra allocations when repeatedly creating transactions. + // extra allocations when repeatedly creating transactions. **Note that this + // may not free all the allocated memory by the previous transaction (see + // WriteBatch::Clear()). To ensure that all allocated memory is freed, users + // must destruct the transaction object. virtual Transaction* BeginTransaction( const WriteOptions& write_options, const TransactionOptions& txn_options = TransactionOptions(), diff --git a/include/rocksdb/write_batch.h b/include/rocksdb/write_batch.h index df7048af36d..7cdca233aae 100644 --- a/include/rocksdb/write_batch.h +++ b/include/rocksdb/write_batch.h @@ -209,6 +209,8 @@ class WriteBatch : public WriteBatchBase { using WriteBatchBase::Clear; // Clear all updates buffered in this batch. + // Internally, it calls resize() on the string buffer. So allocated memory + // capacity may not be freed. void Clear() override; // Records the state of the batch for future calls to RollbackToSavePoint().