swiftxcodeswiftuistructinstruments

How to track stack memory or value type object in Swift?


I'm trying to see the memory allocation of my struct on the stack memory with Instrument tool, but I saw only heap allocations.

Is there an option in the Instrument where I can view the allocations on stack memory?

enter image description here

//code

struct ContentView: View {
 @State var arr: [Model] = [] 
 var body: some View {
    VStack {
        Text("Hello, world!")
    }
    .task {
        for _ in 0...5000 {
           arr.append(Model())
        }
    }
 }
}

//struct model

struct Model {
 let id: Int = 1
}

Solution

  • An array in Swift is actually a struct, with a reference counted object holding the data - that makes assigning an array with a million items fast. The actual data is always on the heap. (I wouldn’t be surprised if arrays with small amount of data don’t do any heap allocations, for example an array with one object reference).