You asked a few questions here ...
Instead of deleting the memory, what benefits will we get based on reusing objects?
It totally depends on your application. Even assuming I know what the application is, you left one more detail unclear - what is the reuse strategy? But even knowing that it is very difficult to predict or answer in general. Try some things and measure them.
As a rule, I like to minimize the most free distribution. However, this is premature optimization. This will only matter for thousands of calls.
What is the process of the new?
It depends entirely on the implementation. But the general strategy that distributors use is to have a free list, that is, a list of blocks that were freed in the process. When a free list is empty or contains insufficiently continuous free space, it should request a kernel for memory, which it can only issue in blocks with a constant page size. (4096 on x86.) The distributor must also decide when to cut, lay, or combine blocks. Multithreading can also put pressure on distributors, as they must synchronize their free lists.
As a rule, this is a rather expensive operation. Perhaps not so much in relation to what else you are doing. But it is not cheap.
Does a context switch occur?
Quite possible. It is also possible that this will not happen. Your OS can freely switch to context at any time when it receives an interrupt or syscall, so ... This can happen many times; I do not see any special relationship between this and your distributor.
New memory is allocated, who performs the allocation? OS?
This can come from a free list, and in this case the system call is not involved, therefore, no help from the OS. But this can come from the OS if the free list cannot satisfy the request. In addition, even if this comes from a free list, your kernel could upload this data so that you can get a page error on access, and the kernel allocator will work. Therefore, I think it will be a mixed bag. Of course, you can have an appropriate implementation that does all kinds of crazy things.
asveikau
source share