0

In C, you can check if the system has enough free memory by checking the return value of a call to malloc(). How do you check if the system has enough free memory in Objective-C? Does alloc throw an exception if there is not enough memory?

I have created a class that derives from NSObject for an in-memory photo cache. I need to check before saving something into the cache if there is enough memory, and can't quite figure out the proper way to do so.

Thanks

Josh Sklar
  • 461
  • 1
  • 3
  • 14
  • you may check available memory as it does print_free_memory function in http://stackoverflow.com/questions/5012886/knowing-available-ram-on-an-ios-device – stosha Jul 25 '13 at 05:33

1 Answers1

1

-alloc will return nil if it is unable to allocate the memory requested. Just check that the return value is non-nil.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • What would you recommend to do for this if I am doing a `[NSData dataWithContentsOfURL:]` call? Will this return `nil` if there is not enough free memory? – Josh Sklar Jul 25 '13 at 02:48
  • Yes. Any method that allocates memory like that should return `nil` if it fails to allocate the memory. – user1118321 Jul 25 '13 at 02:53