[Interest] bad_alloc raised : Amout of RAM available ?

Constantin Makshin cmakshin at gmail.com
Mon Jul 8 19:20:33 CEST 2013


A bit off-topic, but still: it's not a very good idea to make your
application's behavior/logic dependent on amount of free memory reported by
the system.
Reasons:
1) most, if not all, modern OSes use "delayed allocation" strategy, so a
successful malloc() or its equivalent doesn't guarantee you actually will
be able to use that memory;
2) multitasking means there's always a chance that during the period
between you request information about available RAM and try to use it
another process will allocate some amount of memory for its own needs,
causing your allocation to fail.

My suggestion is either to:
1) use system's native functions to get total amount of memory and use it
to determine the upper limit for your allication (e.g. never allocate more
than 50% of RAM even if it's not used by anything else);
2) enlarge your buffers when necessary until you get an "out of memory"
signal in form of std::bad_alloc, NULL returned by an allocation function,
etc..
On Jul 8, 2013 4:20 PM, <qt.dantec at free.fr> wrote:

> Using 4.8 under windows / mingw, I catch a std::bad_alloc exception
> raised somewhere, that I do not seem to have raised. At that point,
> there is still 66% of system memory available according to windows.
>
> 1) Is there a Qt way to programmatically monitor the amount of RAM yet
> available to the current process ?
>
> 2) Is there a standard way to debug this, in order to find where the
> exception was raised ?
>
> Quentin
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130708/6dafe462/attachment.html>


More information about the Interest mailing list