获取堆上对象的大小(以字节为单位)
Get the size (in bytes) of an object on the heap
我知道您可以使用 MemoryLayout<T>.size
来获取类型 T
的大小。
例如:MemoryLayout<Int32>.size // 4
但是,对于 class 个实例(对象),MemoryLayout<T>.size
returns 对象引用的大小(在 64 位机器上为 8 字节),而不是对象的大小堆上的实际对象。
class ClassA { // Objects should be at least 8 bytes
let x: Int64 = 0
}
class ClassB {// Objects should be at least 16 bytes
let x: Int64 = 0
let y: Int64 = 0
}
MemoryLayout<ClassA>.size // 8
MemoryLayout<ClassB>.size // 8, as well :(
如何获取对象本身的大小?
对于那些想知道的人,我没有真正需要这个,我只是在探索 Swift 及其与 C 的互操作性。
据我在Playground测试,这个函数returns貌似意义重大:
func heapSize(_ obj: AnyObject) -> Int {
return malloc_size(Unmanaged.passUnretained(obj).toOpaque())
}
class MyClass {
//no properites...
}
let myObj = MyClass()
print(heapSize(myObj)) //->16
class MyBiggerClass {
var str: String?
var i: Int = 0
}
let myBiggerObj = MyBiggerClass()
print(heapSize(myBiggerObj)) //->64
似乎当前 Swift 运行时使用 malloc
兼容的东西在堆中分配内存。 (malloc
在分配小块时提供一些填充以适应分配的大小的 2 次幂。因此,实例实际需要的大小可能小于 malloc_size
。)
我还没有测试过这能在多大程度上起作用,并且仅仅依赖于当前实现的未记录的行为将来会在没有任何通知的情况下随时改变。
但如果你真的知道,这可能是探索的一个很好的起点。
Apple 平台 上的一个选项,因为 Swift class 是 currently built on top of Objective-C classes there, would be to use the Obj-C runtime function class_getInstanceSize
,它为您提供实例的大小(以字节为单位) class,包括任何填充。
// on a 64-bit machine (1 word == 8 bytes)...
import Foundation
class C {}
print(class_getInstanceSize(C.self)) // 16 bytes metadata for empty class
// (isa ptr + ref count)
class C1 {
var i = 0
var i1 = 0
var b = false
}
print(class_getInstanceSize(C1.self)) // 40 bytes
// (16 metadata + 24 ivars, 8 for i + 8 for i1 + 1 for b + 7 padding)
我知道您可以使用 MemoryLayout<T>.size
来获取类型 T
的大小。
例如:MemoryLayout<Int32>.size // 4
但是,对于 class 个实例(对象),MemoryLayout<T>.size
returns 对象引用的大小(在 64 位机器上为 8 字节),而不是对象的大小堆上的实际对象。
class ClassA { // Objects should be at least 8 bytes
let x: Int64 = 0
}
class ClassB {// Objects should be at least 16 bytes
let x: Int64 = 0
let y: Int64 = 0
}
MemoryLayout<ClassA>.size // 8
MemoryLayout<ClassB>.size // 8, as well :(
如何获取对象本身的大小?
对于那些想知道的人,我没有真正需要这个,我只是在探索 Swift 及其与 C 的互操作性。
据我在Playground测试,这个函数returns貌似意义重大:
func heapSize(_ obj: AnyObject) -> Int {
return malloc_size(Unmanaged.passUnretained(obj).toOpaque())
}
class MyClass {
//no properites...
}
let myObj = MyClass()
print(heapSize(myObj)) //->16
class MyBiggerClass {
var str: String?
var i: Int = 0
}
let myBiggerObj = MyBiggerClass()
print(heapSize(myBiggerObj)) //->64
似乎当前 Swift 运行时使用 malloc
兼容的东西在堆中分配内存。 (malloc
在分配小块时提供一些填充以适应分配的大小的 2 次幂。因此,实例实际需要的大小可能小于 malloc_size
。)
我还没有测试过这能在多大程度上起作用,并且仅仅依赖于当前实现的未记录的行为将来会在没有任何通知的情况下随时改变。
但如果你真的知道,这可能是探索的一个很好的起点。
Apple 平台 上的一个选项,因为 Swift class 是 currently built on top of Objective-C classes there, would be to use the Obj-C runtime function class_getInstanceSize
,它为您提供实例的大小(以字节为单位) class,包括任何填充。
// on a 64-bit machine (1 word == 8 bytes)...
import Foundation
class C {}
print(class_getInstanceSize(C.self)) // 16 bytes metadata for empty class
// (isa ptr + ref count)
class C1 {
var i = 0
var i1 = 0
var b = false
}
print(class_getInstanceSize(C1.self)) // 40 bytes
// (16 metadata + 24 ivars, 8 for i + 8 for i1 + 1 for b + 7 padding)