Skip to content

Instantly share code, notes, and snippets.

@djryanash
Last active May 15, 2023 01:52
Show Gist options
  • Save djryanash/9dc0008eb5a417f5c8db5db46ab1ec9d to your computer and use it in GitHub Desktop.
Save djryanash/9dc0008eb5a417f5c8db5db46ab1ec9d to your computer and use it in GitHub Desktop.
Function to print the memory usage of whatever you pass in - struct/class/function/variable/constant, etc
class ViewController: UIViewController {
override func viewDidLoad() {
self.checkMemoryUsage(self.tableView) }
func checkMemoryUsage<R>(_ thingToCheck: R, name: String = #function) {
// You can pass in almost anything - a variable/constant/struct/class/function etc.
let size = MemoryLayout.size(ofValue: thingToCheck)
let stride = MemoryLayout.stride(ofValue: thingToCheck)
let alignment = MemoryLayout.alignment(ofValue: thingToCheck)
print("\(name).size: \(size)")
print("\(#function)).stride: \(stride)")
print("\(type(of: thingToCheck)).alignment: \(alignment)") } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment