# Memory leak in C API?

**URL:** <https://forums.foundationdb.org/t/memory-leak-in-c-api/554>\
**Category:** Development\
**Created:** [July 9, 2018, 10:54am UTC](https://forums.foundationdb.org/t/memory-leak-in-c-api/554 "2018-07-09T10:54:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirilltitov](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/kirilltitov/32/328_2.png) [@kirilltitov](https://forums.foundationdb.org/u/kirilltitov)\
**Post date:** [July 9, 2018, 10:54am UTC](https://forums.foundationdb.org/t/memory-leak-in-c-api/554/1 "2018-07-09T10:54:23Z")

</div>

While developing Swift library for FDB I noticed slight memory leakage. At first I though it was my fault (you know, that cheeky `Data` objects, you always have memory issues with those things).

I continued debugging and ended up with just a [C program](https://gist.github.com/kirilltitov/97a6bfc8cdd8a5a08b60bea8536c507c) with looped `fdb_transaction_clear` function execution (within the same transaction, it doesn’t make any difference). The leak is still here — see line 67.

Probably it’s just me, I rarely use C/C++ and I’m not very competent in it. But I’m quite sure I did everything by the book.

Can someone help?

---

<div class="post-metadata">

**Author:** ![kirilltitov](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/kirilltitov/32/328_2.png) [@kirilltitov](https://forums.foundationdb.org/u/kirilltitov)\
**Post date:** [July 9, 2018, 11:14am UTC](https://forums.foundationdb.org/t/memory-leak-in-c-api/554/2 "2018-07-09T11:14:06Z")

</div>

HMMM. When I made a few more transactional cycles, it didn’t allocate more memory. Instead, it seems like it used previously allocated memory. So I suppose, it might eat up to 4GB RAM (or whatever the `memory` config directive is).

But I thought this is supposed to be server behaviour, not client. Isn’t it?

---

<div class="post-metadata">

**Author:** ![kirilltitov](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/kirilltitov/32/328_2.png) [@kirilltitov](https://forums.foundationdb.org/u/kirilltitov)\
**Post date:** [July 9, 2018, 11:34am UTC](https://forums.foundationdb.org/t/memory-leak-in-c-api/554/3 "2018-07-09T11:34:51Z")

</div>

Furthermore, after complete connection shutdown (but before exit), it released ~60 megabytes of total ~700 allocated megabytes. This is definitely not OK.

---

<div class="post-metadata">

**Author:** ![SteavedHams](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/steavedhams/32/18_2.png) [@SteavedHams](https://forums.foundationdb.org/u/SteavedHams)\
**Post date:** [July 10, 2018, 12:26am UTC](https://forums.foundationdb.org/t/memory-leak-in-c-api/554/4 "2018-07-10T00:26:41Z")

</div>

Most of the memory used by fdb (client or server) is allocated via a “fast allocator” (flow/FastAlloc.h) which allocates memory from the OS in large blocks and then keeps it for reuse within the process. This means that from the OS perspective an fdb process’s memory footprint will generally not go down.

After committing your transaction, if you were to repeat the same transaction again you would not see an increase in memory usage as the memory which was freed (internally) after the first attempt would be reused.

Transactions have a per-instance memory pool called an “arena” (flow/Arena.h) in which all memory needed for the transaction’s operations are allocated. This includes temporary copies of user keys, which is what is happening in fdb\_transaction\_clear(). This arena is freed all at once when the transaction is committed (whether successful or not), reset, or destroyed. The arena’s memory comes from the fast allocator, so once freed it remains in the fast allocator to be reused within the process instead of being given back to the OS.

While it would be possible to optimize fdb\_transaction\_clear() to require less temporary memory, write transactions in FDB are limited to 10MB of work and 5 seconds of lifetime so it is not expected that a sane write transaction workload would cause a significant amount of memory to be allocated.

---

<div class="post-metadata">

**Author:** ![kirilltitov](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/kirilltitov/32/328_2.png) [@kirilltitov](https://forums.foundationdb.org/u/kirilltitov)\
**Post date:** [July 10, 2018, 12:55pm UTC](https://forums.foundationdb.org/t/memory-leak-in-c-api/554/5 "2018-07-10T12:55:14Z")

</div>

Thank you so much for such a detailed answer!

I’m aiming to use my Swift FoundationDB wrapper in long-living server application (on top of Swift-NIO), so stable memory usage and consumption is a matter of great importance for me.

I hope, everything is gonna be OK 🙂
