Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understand the behavior of the new-handler

I'm reading Effective C++ 55 by Scott Meyers and have a question from item 49:

When operator new is unable to fulfill a memory request, it calls the new-handler function repeatedly until it can find enough memory.

A well-designed newhandler function must do one of the following:

  • Make more memory available.
  • Install a different new-handler.
  • Deinstall the new-handler
  • Throw an exception
  • Not return

When new can't allocate memory, it means that there is not enough memory, and the question is how and from where can the newhandler allocate more memory?

Can you explain all of these steps?

like image 279
Seno Alvrtsyan Avatar asked Feb 16 '15 12:02

Seno Alvrtsyan


2 Answers

It depends on the implementation. I can tell you the way I usually do it:

1) The new handler allocates a large amount of memory at startup as a reserve.

2) When ordinary allocations fail, the new handler dips into its reserve.

3) Code that controls load management can hook the memory management system and determine when it has dipped into its reserve. It generally reacts by trimming caches and shedding load.

4) The memory manager attempts to refill its reserves as memory is freed.

5) When the reserve is restored, hooks are notified that they may grow caches and/or resume accepting additional load.

6) When the reserves get low, allocations that are able to fail (typically large allocations) fail. All code must sanely handle the failure of large allocations.

7) If the reserves are exhausted, allocations that are unable to fail (typically small allocations) block.

8) If the blocking condition persists or large allocations continue to fail and the reserve can't be restored, abnormal termination is triggered.

like image 133
David Schwartz Avatar answered Nov 01 '22 20:11

David Schwartz


It can discard data that is not really needed. Say, photoshop caches the display image at several scales, and keeps as much as it can. This is how it knows just how much it can get away with.

like image 21
JDługosz Avatar answered Nov 01 '22 20:11

JDługosz