Or you monitor free RAM and when it falls below a certain threshold, you trigger the save (provided the rest isn't gone at once).
Another method could be to allocate a sufficient amount of memory, wait for the out of mem error, free the reserved mem and save as quickly as possible.
Everything depends on how RAM is consumed; little by little or large chunks at once.