Exiting gracefully when memory exhausted
-
@JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.
Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.
-
@wrosecrans Agree. Add some factor to the available memory while it may be hard to get the exact number of free memory.
-
@wrosecrans said in Exiting gracefully when memory exhausted:
@JoeCFD At best, that's just a race condition. Another app could allocate everything between when you query and when you try to allocate. There's no good definition of "available memory" on modern operating systems to even query.
Best bet is just to allow the user to configure some limits and bail before you hit them. Trying to respond gracefully once your application has already had a failed allocation is almost certainly going to fail in some way or another. Better to fail safe than to try to plow ahead in an unknown state.
I have considered allowing the user to set a maximum memory usage and exit gracefully if they hit that limit. But the current appoach seems to work fine on macOS, I'm surprised it doesn't work on Windows.
-
@AndyBrice Don't be surprised if it starts to fail visibly on MacOS after an OS update, a new Qt version, or some changes to your code. It "appears" to work, but you can't rely on the behavior you are seeing.
-
@wrosecrans You might be right. I'm just surprised there doesn't seem to be a reliable way to handle this.
I don't suppose Qt has a crossplatform way to find out how much memory a process is using? That would be useful.
-
I'm not aware of Qt providing anything that fits the bill.
Providing a single useful memory usage number from an application's perspective sounds challenging. Shared pages, mapped but unallocated pages, buffers held on behalf of the process, etc make the analysis complicated.
-
@AndyBrice said in Exiting gracefully when memory exhausted:
I'm surprised it doesn't work on Windows
I‘m surpised as well, maybe its msvc‘s exception handling? I remember that to be bad or haphazardly implemented. Try with nothrow and check against nullptr instead.
I guess you know how to override new/delete to use a custom memory pool ? I fyou decide to go the fixed max memory route
-
That would involve a lot of rewriting!
you do not have to necessarily rewrite your whole code, a small test program could, as first step be enough, no ?
Sounds nasty...!
I've never done it myself, and only seen it done once, in the source code of Command & Conquer