Repository navigation
Description
Writes of a large text property keep about twice their size in memory until the process ends. 300 writes of 1 MiB grow the resident set by 696 MiB in a process that holds one record, and the cause I found is the _bytes_cache of arcadedb._native.lib.bc().
Database._exec calls lib.bc(text), where text is the JSON text of the bound parameters. For a write with a large property that text contains the whole value. bc() remembers the encoded bytes in the module dict _bytes_cache, keyed by the str, for the first 4096 distinct texts (src/arcadedb/_native/lib.py, if len(_bytes_cache) < 4096). The limit counts entries and not bytes, so the cache holds the str and the bytes of each write, up to 4096 times twice the size of the text. A read with a 1 MiB bound parameter goes through exec_query and lib.b(text), which does not cache, and keeps nothing.
Repro (27 lines; uv run --with arcadedb python repro_bytes_cache_342.py [off]; off replaces lib.bc with a plain value.encode("utf-8") and changes nothing else):
# Writes of a large text property keep about twice their size in memory until the process ends (arcadedb native wheel).
# usage: uv run --with arcadedb python repro_bytes_cache_342.py [off] "off" replaces arcadedb._native.lib.bc by a plain encode
import os, sys, tempfile, importlib.metadata as md
import arcadedb
import arcadedb._native.lib as lib
if "off" in sys.argv:
lib.bc = lambda value: value.encode("utf-8") # the only change: no cache of the parameter text
def rss_mib():
return next(int(l.split()[1]) for l in open("/proc/self/status") if l.startswith("VmRSS:")) / 1024
db = arcadedb.create_database(os.path.join(tempfile.mkdtemp(), "db"))
db.command("sql", "CREATE VERTEX TYPE WatchState")
db.command("sql", "CREATE PROPERTY WatchState.state_json STRING")
db.command("sql", "CREATE PROPERTY WatchState.key STRING")
db.command("sql", "CREATE INDEX ON WatchState (key) UNIQUE")
before = rss_mib()
for i in range(300):
text = os.urandom(512 * 1024).hex() # 1 MiB, new content every write
with db.transaction():
db.command("sql", "UPDATE WatchState SET key = ?, state_json = ? UPSERT WHERE key = ?", "k", text, "k")
cache = lib._bytes_cache
print("arcadedb", md.version("arcadedb"), "bytes cache off:", "off" in sys.argv)
print("RSS growth over 300 writes of 1 MiB: %.0f MiB" % (rss_mib() - before))
print("lib._bytes_cache: %d entries, %.0f MiB of keys and values" % (len(cache), sum(sys.getsizeof(k) + sys.getsizeof(v) for k, v in cache.items()) / 1048576))
db.close()Numbers (Linux x86-64, Python 3.15.0, arcadedb 26.11.1.post4, the latest on PyPI; main at 259e336 has the same lines):
| run | RSS growth over 300 writes of 1 MiB | lib._bytes_cache |
|---|---|---|
| as installed | 696 MiB | 307 entries, 600 MiB |
bc replaced by a plain encode |
39 MiB | 0 entries |
On the other host I use (Python 3.12) the same script gives the same picture, and over 1,000 writes per mode the resident set slope is about 2 GiB per 1,000 writes of 1 MiB for an UPSERT and for an UPDATE by RID, and about 550 MiB for ten UPSERTs of 100 KB per transaction (the cache reaches its 4096 entries), against almost nothing with the cache off. A 1 KB write keeps about 2 KB, which is harmless.
I ruled out the engine: the same writes through the Java engine API on a JVM (JDK 21.0.13 and 25.0.5, 26.11.1 jars, one transaction per write, ten writes per transaction, TX_WAL=false, MAX_PAGE_RAM=32) keep the heap flat after a full GC (9 to 15 MiB). I also ruled out the heap and page-cache caps (ARCADEDB_NATIVE_ISOLATE_ARGS changed nothing), a large parameter on a read, and the application that found it (a service of mine that wrote a 1.8 MB state text on every tick grew about 330 MiB per hour; the same script shows it). A text written inside the SQL itself (no bound parameter) keeps a smaller amount even with the cache off, probably the statement cache of ArcadeData/arcadedb#12350. I did not test the JVM wheel, which has no _native package.
Suggested fix: do not cache the parameter text, as the exec_query path already does with lib.b(text), and keep bc() for the statement text and the language name, which are the repeated ones. Bounding the cache by bytes, or skipping values above a few KB, would also work. I am happy to open a pull request with a test for it.
Activity
fix(#412): the native bytes cache keeps no write parameters and no la…
tae898 commentedon Nov 17, 2026
Fixed in #413 (merged as 097436a):
lib.bc()now remembers a text only when it has at most 1024 characters and the cache has fewer than 4096 entries, and the lazy command path encodes its parameter text without caching it. The repro of this issue (300 writes of 1 MiB) grew the resident set by 631 MiB before the change and by 30 MiB after it on the same host and wheel, with 6 cache entries instead of 307. The fix goes out with the next post release of the native wheel, and I will confirm it on the released wheel then.