What is the correct way to handle “out of memory”?

后端 未结 7 912
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 13:57

Recently, I work on a video player program on Windows for a CCTV program. As the program has to decode and play many videos streams at the same time, I think it might meet

相关标签:
7条回答
  • 2020-12-30 14:53

    On Linux, malloc() will never fail -- instead, the OOM killer will be triggered and begin killing random processes until the system falls over. Since Linux is the most popular UNIX derivative in use today, many developers have learned to just never check the result of malloc(). That's probably why your colleagues ignore malloc() failures.

    On OSes which support failures, I've seen two general patterns:

    • Write a custom procedure which checks the result of malloc(), and calls abort() if allocation failed. For example, the GLib and GTK+ libraries use this approach.

    • Store a global list of "purge-able" allocations, such as caches, which can be cleared in the event of allocation failure. Then, try the allocation again, and if it still fails, report it via the standard error reporting mechanisms (which do not perform dynamic allocation).

    0 讨论(0)
提交回复
热议问题