Skip to content

FlatBuffers Swift: Memory Usage Plateaus at 387 MB After Large Serialization and Never Decreases (v25.2.10) #8642

@km-steve-ham

Description

@km-steve-ham

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:

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:

  1. Download and open the HelloWorld2 sample project.
  2. Run the app and tap the button to trigger serialization.
  3. 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:


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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions