Is “Out Of Memory” A Recoverable Error?

后端 未结 24 2113
闹比i
闹比i 2020-11-30 21:44

I\'ve been programming a long time, and the programs I see, when they run out of memory, attempt to clean up and exit, i.e. fail gracefully. I can\'t remember the last time

24条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 22:25

    OOM should be recoverable because shutdown isn't the only strategy to recovering from OOM.

    There is actually a pretty standard solution to the OOM problem at the application level. As part of you application design determine a safe minimum amount of memory required to recover from an out of memory condition. (Eg. the memory required to auto save documents, bring up warning dialogs, log shutdown data).

    At the start of your application or at the start of a critical block, pre-allocate that amount of memory. If you detect an out of memory condition release your guard memory and perform recovery. The strategy can still fail but on the whole gives great bang for the buck.

    Note that the application need not shut down. It can display a modal dialog until the OOM condition has been resolved.

    I'm not 100% certain but I'm pretty sure 'Code Complete' (required reading for any respectable software engineer) covers this.

    P.S. You can extend your application framework to help with this strategy but please don't implement such a policy in a library (good libraries do not make global decisions without an applications consent)

提交回复
热议问题