2

When you do a release, you do not immediately remove the memory. I used this code and I can see the memory before and after the use of release and it do not change. Ok, it will be release after some time.

But, what can I do for release all memory I can before start a library that will use a lot of memory? Or how can I immediately release memory?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rodrigo
  • 11,909
  • 23
  • 68
  • 101
  • 3
    I think you're misunderstanding how memory works. Your application has a pool of memory reserved for it and when you release memory it goes back to this pool. If you need the released memory for your program it should be immediately available. – N_A Nov 18 '11 at 17:04
  • 1
    The resident size is probably accurate only to the whole page, so shouldn't be taken as an absolute guide. In any case, can you show your code that isn't releasing memory immediately? The relevant object may just be ending up in the autorelease pool for some reason, such as because you used a getter to obtain it at some point even if you then relieved the parent object of ownership. – Tommy Nov 18 '11 at 17:07
  • A good analogy might be, if you're using a tool, you finish set it down and announce 'I'm done with this'. It might be a minute or two before the next user comes along, but that doesn't mean it wasn't available. – NJones Nov 18 '11 at 18:00

2 Answers2

4

Memory Management is a big thing in iOS but this tidbit of information helped me a lot during my development.

"Each object has a "retain count" which is increased by calling "retain" and decreased by calling "release". Once the retain count hits 0, the object is released and the memory can be used for something else.

You can "autorelease" objects. This means the retain count isn't immediately decreased, but is decreased the next time the current autorelease pool is drained.

iOS apps have an event loop in which your code runs. After each iteration of the event loop, the autorelease pool is drained. Any object with a retain count of 0 is released.

By default, autoreleased objects are returned by methods that don't begin with new, copy, mutableCopy, retain or init. This means you can use them immediately but if you don't retain them the object will be gone on the next iteration of the run loop.

If you fail to release retained objects but no longer reference them then you will have a memory leak, this can be detected by the leaks tool in Instruments.

One strategy is to autorelease everything returned by the above named methods and store objects in retain properties (or copy for strings). In your object's dealloc method, set all your properties to nil. Setting a retain/copy property to nil releases the object that it currently points to. As long as you don't have any circular references (avoided by not using retain properties for "parent" objects such as delegates), you will never encounter any leaks."

here is the link to the thread for this information

http://www.quora.com/What-is-the-best-way-to-understand-memory-management-in-iOS-development

It's a good thread with some useful code examples as well as other references.

BDubCook
  • 488
  • 1
  • 4
  • 15
1

Release frees the memory immediatly (assuming it's the last release). That means, it can be used by your application again when allocating. Note that every applications have some chunks (pages) of free memory assigned by system and when allocating/deallocating part of a page, the released memory is not returned automatically to the system. It's just marked as free and can be used again by the application.

To understand all this, you need to learn something about how operating systems handle memory allocation, virtual memory etc.

Sulthan
  • 128,090
  • 22
  • 218
  • 270