Type Method

size(ofValue:)

Returns the contiguous memory footprint of the given instance.

Declaration

static func size(ofValue value: T) -> Int

Parameters

value

A value representative of the type to describe.

Return Value

The size, in bytes, of the given value’s type.

Discussion

The result does not include any dynamically allocated or out of line storage. In particular, pointers and class instances all have the same contiguous memory footprint, regardless of the size of the referenced data.

When you have a type instead of an instance, use the MemoryLayout<T>.size static property instead.

let x: Int = 100

// Finding the size of a value's type
let s = MemoryLayout.size(ofValue: x)
// s == 8

// Finding the size of a type directly
let t = MemoryLayout<Int>.size
// t == 8