CArrowArrayStreamExporter never disposes exported record batches, leaving native memory to the finalizer
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 38/100
Direzione di ricerca
Start by reading CArrowArrayStreamExporter.GetNext, CArrowArrayExporter.ExportRecordBatch, and the ownership behavior in ArrowBuffer.TryExport and ExportedAllocationOwner; check how NativeMemoryManager behaves when pinned. The issue describes the leak and candidate approaches, but choosing a safe ownership strategy is the main design work. Done means releasing an exported array also releases the batch's buffers without relying on finalization, including the non-shared-buffer case.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
CArrowArrayStreamExporter.GetNext reads a RecordBatch from the underlying IArrowArrayStream, exports it with CArrowArrayExporter.ExportRecordBatch, and then never disposes it:
RecordBatch recordBatch = arrayStream.ArrowArrayStream.ReadNextRecordBatchAsync().Result;
if (recordBatch != null)
{
CArrowArrayExporter.ExportRecordBatch(recordBatch, cArray);
}
For allocator-backed buffers, ArrowBuffer.TryExport takes its own reference (_handle.Retain()), so the exported ArrowArray keeps the memory alive independently of the batch. When the consumer calls release, only the exported reference is dropped. The batch's own reference is never released, so with the default NativeMemoryAllocator the native memory is only freed when the NativeMemoryManager finalizer eventually runs.
In practice this means every batch exported through the C stream interface depends on finalization to free its buffers. Reclamation is driven entirely by GC activity in the exporting runtime, which is mostly the AddMemoryPressure calls in NativeMemoryAllocator and ExportedAllocationOwner.
This is especially visible in NativeAOT-compiled libraries (for example, ADBC drivers). They have their own private GC heap, and nothing collects that heap once the library stops allocating.
Measurements
I used a NativeAOT-compiled ADBC driver loaded by a C host. The host drains a stream of 6 batches × 4M rows twice and releases every array, the stream and the statement/connection/database. All numbers are process private bytes; the baseline is about 3 MB.
| as-is | stream disposes each batch after export | |
|---|---|---|
| after release, no GC | 395 MB | 335 MB |
after GC.Collect(2, Aggressive, true, true) |
138 MB | 5.8 MB |
after Collect + WaitForPendingFinalizers + Aggressive |
5.5 MB | — |
The 138 MB left behind in the "as-is" column is native memory that only the finalizer queue frees.
Suggested fix
Have GetNext dispose the batch once it has been exported. Disposing is safe for buffers that TryExport retains (_handle != null).
There is one caveat. Buffers without a shared handle are exported by pinning _memory, and some MemoryManager implementations (e.g. NativeMemoryManager) throw when disposed while pinned. A complete fix therefore needs one of these:
- make the export path always take an owning reference, or
- only dispose the batch when every buffer was exported by reference, or
- move ownership of the batch into the
ExportedAllocationOwner, so it is disposed when the exported array is released.
The third option is probably the cleanest, since it also covers the non-shared case.
Component(s)
C#
- Lingua principale
- C#
- Stelle
- 42
- Fork
- 31
- Merge medio
- 1g 1h
- PR unite (30g)
- 24
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di apache/arrow-dotnet
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
apache/arrow-dotnet#410 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Support the proposed DictionaryEncoding.custom_metadata (format 1.6) for dictionaries of extension types in IPCForse già presa @CurtHagenlocher l’ha presa 2 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
apache/arrow-dotnet#475 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 38/100
apache/arrow-dotnet#409 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
apache/arrow-dotnet#397 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
apache/arrow-dotnet#378 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di apache/arrow-dotnet
Issue simili
-
/api/oauth/introspect emits clientId and tokenType instead of RFC 7662's client_id and token_typeApertabug P2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
nightscout/nocturne#2317 ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG]: Position intent set on a simple order is dropped when it becomes a bracket, OCO or OTO orderForse già presa @RaphaelFakhri l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
alpacahq/alpaca-trade-api-csharp#903 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
joinrpg/joinrpg-net#5752 ·
I maintainer di solito rispondono entro 1 giorno
-
bug ux
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
CodeForFire/lagebuch#714 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
dimitar-radenkov/Pointframe#294 ·
I maintainer di solito rispondono entro 1 giorno