I don't think it is the "replaces" that actually causes your app to be reporting an out of memory error.
The replaces command needs to allocate some memory for the replacement and you ran out of memory before that.
Now it is triggering that error.

You can probably trigger the same error by any other string operation.