Last active
May 15, 2023 01:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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