How to find memory size using Xcode

I know that this has been asked and answered in some form many times, but I still cannot get it to work.

All I want to do is find total memory capacity and available memory just like you see in settings > general.

I've tried using [NSProcessInfo processInfo].physicalMemory but this returns a number that doesn't come anywhere close to the numbers I see in settings.

Any and all suggestions are welcome

thanks in advance.

Accepted Reply

You can get the free space and capacity of a particular volume using code like this.

func printVolumeCapacities(url: URL) {
    let values: URLResourceValues = try! url.resourceValues(forKeys: [.volumeAvailableCapacityKey, .volumeTotalCapacityKey])
    print(values.volumeAvailableCapacity!)
    print(values.volumeTotalCapacity!)
}

Note This is Swift 3 code, compiling with Xcode 8.0b4. These APIs are much less fun to call from Swift 2 )-:

So, if you get your Documents directory URL and pass it into this, it’ll tell you how much space you have to work with.

I wrote:

What do you want to do with this value? Display it to the user? If not that then what?

You wrote:

yes, i would just like to get the total and available disk space in xcode …

That doesn’t really answer my question. While the above code works, whether it does what you want to do depends on your requirements, and you’ve not really explained those. Until you do, I can’t give you a decent answer.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I presume you’re talking about Settings > General > About > Capacity here. If so, the reason you’ve had no luck with your searches is that this display disk space, not memory, that is, the amount of RAM. The RAM size of iOS devices is generally not advertised to users.

AFAIK there’s no high-level API for getting the capacity of the device in user terms. You could look through the mounted volumes and sum up their size but it’s not something I’d recommend, especially with APFS (and its space sharing feature) looming.

What do you want to do with this value? Display it to the user? If not that then what?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

thanks for the response. yes, i would just like to get the total and available disk space in xcode, but I can't figure out how to do that.

Thanks.

You can get the free space and capacity of a particular volume using code like this.

func printVolumeCapacities(url: URL) {
    let values: URLResourceValues = try! url.resourceValues(forKeys: [.volumeAvailableCapacityKey, .volumeTotalCapacityKey])
    print(values.volumeAvailableCapacity!)
    print(values.volumeTotalCapacity!)
}

Note This is Swift 3 code, compiling with Xcode 8.0b4. These APIs are much less fun to call from Swift 2 )-:

So, if you get your Documents directory URL and pass it into this, it’ll tell you how much space you have to work with.

I wrote:

What do you want to do with this value? Display it to the user? If not that then what?

You wrote:

yes, i would just like to get the total and available disk space in xcode …

That doesn’t really answer my question. While the above code works, whether it does what you want to do depends on your requirements, and you’ve not really explained those. Until you do, I can’t give you a decent answer.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"