18

I was wondering if the call to operator delete is synchronous or not. In other words, if I do:

delete p;

Does the C++ Standard guarantee that only after this call finishes execution the memory is freed? Or is the call asynchronous and simply schedules a task for OS to free this memory as soon as it decides that it is the best time to do so?

If the first case is the valid one, then does it mean that we have to implement our own asynchronous deleter facility? I'm asking because I'd say that most of the time we (programmers) don't care when the memory is freed exactly, and therefore we don't want our code to freeze and wait for this (most likely expensive?) system call to finish, but rather schedule a task for deletion and immediately continue the execution. Does C++ provide any standard facility (maybe through standard library?) to do this without reinventing the wheel?

4
  • define "the memory is freed" please Apr 15, 2013 at 20:40
  • If you want to define when release should do, use own new and delete operator. It is very common when you have many small objects.
    – Naszta
    Apr 15, 2013 at 20:41
  • Releasing memory is much much cheaper than allocating it Apr 15, 2013 at 20:49
  • 1
    If you want asynchronous deletion you could play around with std::unique_ptr and use a custom deleter.
    – James
    Apr 15, 2013 at 20:56

4 Answers 4

18

delete is synchronous. Now, that doesn’t mean that the underlying memory is actually freed at that time by the operating system but from the view of the C++ system it behaves as if.

I'm asking because I'd say that most of the time we (programmers) don't care when the memory is freed exactly

But delete isn’t mainly about memory, it’s just as much about calling a destructor in a deterministic fashion – it’s a general-purpose resource-freeing mechanism, not restricted to memory. And here it is important to have synchronicity, otherwise one of the core aspects of C++ – RAII – wouldn’t work.

6
  • I like your explanation, it definitely makes sense. +1. Apr 15, 2013 at 20:43
  • I don't even think that there's anything in the C++ standard about "asynchronousness" in the sense the OP has in mind (like asynchronous I/O). (There's only stuff about threading and the memory model.) So I didn't want to go and say that "X is synchronous" when the entire concept is never even defined...
    – Kerrek SB
    Apr 15, 2013 at 20:48
  • @KerrekSB That’s a good point. It’s hard to talk about asynchronous semantics in a field where they aren’t defined. Apr 15, 2013 at 20:51
  • @Konrad Rudolph: Of course I understand that destructors are called deterministically, otherwise the principle of resource-freeing on the language level would not be possible at all, as you said. But, just to make it clear, for example, when the whole destructor chain (which is synchronous) has finished execution it still doesn't imply at all that the actual memory occupied by those objects (for which the destructors were called) is actually freed. Is this what you were emphasizing? Apr 15, 2013 at 21:01
  • 1
    It might be worth explicitly stating that there isn't a system call involved in the freeing process. Apr 16, 2013 at 3:11
7

Nothing guarantees that delete p; will release memory back to the OS -- ever. In fact, in quite a few (most?) implementations, it will do nothing of the sort.

Exactly how much does happen when you call delete varies though -- in some cases it's nearly instant (just links the block to a list of free memory blocks). In others it does rather more work: destroying objects, and perhaps searching the current free list for adjacent blocks that can be coalesced with the one being freed.

4

As far as you are concerned, everything behaves exactly as you expect, and there are no hidden traps or gotchas. You can always say T * p = new T; delete p; and that's correct, no matter in what context this occurs.

Given that the operating system routinely has to allocate memory for all sorts of processes and threads concurrently, you can assume that that problem has already been solved correctly.

More formally, 18.6.1.4/1 ("Data Races") says:

For purposes of determining the existence of data races, the library versions of operator new, user replacement versions of global operator new, and the C standard library functions calloc and malloc shall behave as though they accessed and modified only the storage referenced by the return value. The library versions of operator delete, user replacement versions of operator delete, and the C standard library function free shall behave as though they accessed and modified only the storage referenced by their first argument.

Regarding "blockingness": It all boils down to what the C library's allocation function does. Nothing is specified beyond "this function returns a pointer to some memory". It's up to the platform how it provides memory allocation.

4
  • I think he's asking whether the call to delete is blocking, as opposed to whether or not it's threadsafe. Apr 15, 2013 at 20:40
  • Yes, I'm interested whether it is blocking. Apr 15, 2013 at 20:42
  • @AndrewDurward: I don't think that can be answered from within the question, at least not until you're more explicit about what "blocking" means. On a single-threaded MS-DOS, the answer may well be "no", but on a multi-tasking system, you can always get descheduled.
    – Kerrek SB
    Apr 15, 2013 at 20:42
  • @Haroogan: As I said, the OS will have to manage the memory for every process, all at once, so it will have to synchronize at some point. But a good OS should ensure that no single process or thread is starved by memory allocation. You'd have to consult your OS's documentation, though, as well as your C library implementation.
    – Kerrek SB
    Apr 15, 2013 at 20:43
-1

The free call in C/C++ is already extremely fast no matter what. There is no reason why you would delay a delete it for a more opportune time in that regard. Any book keeping you would need to do for that purpose would more than offset the actual free call duration.

If your destructor does more work like closing sockets of DB connections then you could do that type of work at a later time but that should be more of an exceptional case.

2
  • 1
    That’s quite naive. Consider std::vector<ComplexObject>* pv = new std::vector(1000000); ignoring for now that this code is nonsense. Calling delete pv; can be quite inefficient because the destructor of every object in the container needs to be called and there may be very good reasons to delay its execution. Apr 15, 2013 at 20:52
  • Hmm I guess you could do the delete in a second thread just for that purpose on a multi-core machine.... May 9, 2013 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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