[fpc-pascal] TFileStream.WriteBuffer() and RAM used ?
Jonas Maebe
jonas.maebe at elis.ugent.be
Sat Dec 10 15:16:07 CET 2016
On 10/12/16 14:54, fredvs wrote:
> OK, I was thinking that too but... is it normal that the app crash if there
> are no more ram available ?
The problem is most likely that you are running out of contiguous
virtual address space, rather than out of (virtual) memory altogether.
And yes, it is normal that your program terminates with a run time error
(or exception) if it cannot allocate further memory. While there is an
option to instead get nil back from getmem
(http://www.freepascal.org/docs-html/current/rtl/system/returnnilifgrowheapfails.html
), almost no code has been written with this possibility in mind and
hence will probably crash if you set this variable to true.
> If so, how to know how many ram is disponible (and then create a temporally
> file if too few).
Apart from the fact that this is probably not the problem that you are
facing: such an approach simply cannot work on operating systems that
run more than one application at a time. Right after you checked whether
there is still memory available, another application could allocate all
that memory.
The proper way to handle this is to make the buffer size configurable,
and to choose a sensible default (a few hundreds of megabytes on a 32
bit system, possibly more on a 64 bit system). It is not just about your
program crashing or not: if you use more memory than is physically
available, the system will start swapping and that will kill all
usability. Since you cannot predict what the user will do while running
your program, you have to make this configurable.
Jonas
More information about the fpc-pascal
mailing list