@lukasa
For what it's worth I tried not using a closure based Thread but instead a subclass to see if it helps but it doesn't seem to make a difference. Also if we allow the thread to exit immediately without the sleep it sometimes results in less memory usage and sometimes it does not, seems a bit like a race condition determines that.
But that really doesn't make heaps of sense either, these Threads are actually exiting before the program is finished and thus I can't see why if they sleep first the memory usage is then "stuck"?
import Foundation
// at least 32 bytes
struct MyAwesomeStruct {
let num0: Int64 // 8 bytes
let num1: Int64 // 8 bytes
let num2: Int64 // 8 bytes
let num3: Int64 // 8 bytes
}
class Executor : Thread {
override func main() {
super.main()
self.dataTest()
}
private func structTest() {
var data: [MyAwesomeStruct]! = [MyAwesomeStruct].init(repeating: MyAwesomeStruct(num0: 0, num1: 1, num2: 2, num3: 3), count: 100_000)
print("data count: \(data.count)")
data = nil
}
private func dataTest() {
var data: Data! = Data(repeating: 0, count: 10*1024*1024)
print("data count: \(data.count)")
data = nil
}
private func rawPointerTest() {
let data = UnsafeMutableRawBufferPointer.allocate(byteCount: 10*1024*1024, alignment: MemoryLayout<UInt8>.alignment)
print("data count: \(data.count)")
data.deallocate()
}
deinit {
print("Executor deinit")
}
}
for _ in 0..<100 {
let thread = Executor()
thread.start()
}
Thread.sleep(forTimeInterval: 5)
print("All threads should be be gone...")
Thread.sleep(forTimeInterval: 15)