-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Closed
Description
Title
FlatBuffers Swift: Memory Usage Plateaus at 387 MB After Large Serialization and Never Decreases (v25.2.10)
Description
Summary:
When serializing a large array (1,000,000 items) using FlatBuffers Swift (v25.2.10), memory usage jumps to ~387 MB and never decreases, even after all references are released and FlatBufferBuilder.clear() is called.
Minimal Reproducible Example:
-
Sample code (Xcode project): HelloWorld2 on iCloud Drive
-
Key code snippet:
var accs: [ACC] = []
accs.reserveCapacity(1_000_000)
for i in 0..<1_000_000 {
accs.append(ACC(reqId: "id\(i)", ms: Double(i), x: Double(i), y: Double(i), z: Double(i)))
}
var b = FlatBufferBuilder()
var offsets = [Offset]()
offsets.reserveCapacity(accs.count)
for acc in accs {
let reqId = b.create(string: acc.reqId)
let start = FBACC.startFBACC(&b)
FBACC.add(reqId: reqId, &b)
FBACC.add(msTime: Int64(acc.msTime), &b)
FBACC.add(nsTimestamp: Int64(acc.nsTimestamp), &b)
FBACC.add(x: acc.x, &b)
FBACC.add(y: acc.y, &b)
FBACC.add(z: acc.z, &b)
let end = FBACC.endFBACC(&b, start: start)
offsets.append(end)
}
let vector = b.createVector(ofOffsets: offsets)
let accsOffset = FBACCs.createFBACCs(&b, accsVectorOffset: vector)
b.finish(offset: accsOffset)
let result = b.data
b.clear()
print("FlatBuffer result size: \(result.count) bytes")Steps to Reproduce:
- Download and open the HelloWorld2 sample project.
- Run the app and tap the button to trigger serialization.
- Observe memory usage in Xcode or Activity Monitor.
Observed Behavior:
- Memory usage spikes to ~387 MB after serialization.
- Memory does not decrease, even after all objects and buffers are released and
FlatBufferBuilder.clear()is called.
Expected Behavior:
- Memory usage should decrease after serialization and cleanup, or there should be a way to force the FlatBuffers builder to release memory back to the OS.
Environment:
- FlatBuffers Swift version: 25.2.10
- iOS/macOS (Xcode)
- Sample project link
Notes:
- This makes it difficult to use FlatBuffers in long-running apps that occasionally serialize large payloads.
- I have tried explicit cleanup, autorelease pools, and reinitializing the builder, but memory is never released.
- Is this a limitation of the Swift implementation, the underlying allocator, or is there a recommended workaround?
Metadata
Metadata
Assignees
Labels
No labels