1. release the allocated memory.
  2. free the allocated memory.
  3. delete the allocated memory.

What are the differences between them?


Well, the differences have more to do with the computer language behind them. Delete is inappropriate. Assuming C++ or similar, an object is being deleted, and its associated memory is automatically freed thereafter. Free will be understood by any C-family programmer. Release probably will be too but when you are writing for a C audience, stick with C terminology. (Release is more often used with respect to a connection pool, an OS resource, but not memory—in C. In another language, your mileage may vary.)

  • This also raises the question why the words free and release were chosen for deallocating memory, while the opposite, allocating memory, is never called catch or capture. Oh well. – Mr Lister Mar 2 '13 at 17:09
  • I always thought malloc sounded like a make of candy. – Andrew Lazarus Mar 2 '13 at 17:31
  • Release is a keyword in Objective-C (which uses reference counting), and decreases the reference count of an object: it will be deallocated (ie deleted) once all references have been released. – Oliver Mason Mar 2 '13 at 18:20
  • 2
    @Mr Lister: So many of my students torture the memory that the words free or release are appropriate. – The Frog Mar 3 '13 at 2:52

Generally memory allocation in programming languages operates in pairs of functions, and as Andrew Lazarus states, this depends on the language in question. Manual memory management is most common in C-style languages, while many others (including Java) have automatic management through garbage collection etc, so the programmer has not to worry about allocating or deallocating memory.

In C the functions are malloc()/free() (there are different variants for allocation, eg calloc()). So here you would definitely use the second statement, to free allocated memory.

Objective-C uses mostly reference counting (which in the current version is done automatically), and here you create objects using [[MyClass alloc] init], or short [MyClass new] (though this is not conventionally used). Note that this is about objects, not 'raw' memory. For memory you'd use the C functions. When you want to keep a handle to an object alive you call [myObject retain] which adds one to the object's reference count, and to get rid of it you call [myObject release]. Once there are no references to an object it is deallocated, and it's memory freed. So here you would almost use the first statement, but for an object which is released.

In C++ you create objects with new and delete them with delete (last time I did this—I have not used C++ for a long time). Again you delete an object, not memory.

In a language-agnostic context I would say the first two (release/free) are mostly synonymous, while I would not use the third one at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.