In a first time I wondered the error could be surpassing the max_post_size ini variable. But.. Usually it's abt 8M so, you could easily expect up to 8Mbs of html stuff into the text area.
I bet any dom parser is unable to parse so much code. So I ended up with an arbitrary decision (256KB) because >90% of web pages should be smaller.
I'm aware of your try to measure available memory during all the process but as I feel it's run at an higher level and will not work if stuffs broke before so, although measurement is correct, i feel it's run too late in the flow.

As you know, generally, limiting and not trusting user input is always a good practice for integrity of the system.
That's why I wouldn't parse unconditionally any size of input they throw but set a reasonable limit that could fit majority of web pages.