MemoryConstrained[expr,b]
evaluates expr, stopping if more than b bytes of memory are requested.
MemoryConstrained[expr,b,failexpr]
returns failexpr if the memory constraint is not met.


MemoryConstrained
MemoryConstrained[expr,b]
evaluates expr, stopping if more than b bytes of memory are requested.
MemoryConstrained[expr,b,failexpr]
returns failexpr if the memory constraint is not met.
Details

- MemoryConstrained generates an interrupt to stop the evaluation of expr if the amount of additional memory requested during the evaluation of expr exceeds b bytes.
- MemoryConstrained evaluates failexpr only if the evaluation is aborted.
- MemoryConstrained returns $Aborted if the evaluation is aborted and no failexpr is specified.
- Aborts generated by MemoryConstrained can be overruled by AbortProtect. »
- MemoryConstrained[expr,Infinity] imposes no memory constraint.
Examples
open all close allBasic Examples (2)
Run a command until a memory limit is exceeded:
The loop ran until memory allocation for the result of Range exceeded the limit:
Scope (2)
Specify the value to return if the time constraint is reached:
Use CheckAbort inside MemoryConstrained to select which parts are skipped if the limit is reached:
Properties & Relations (4)
The third argument is only evaluated if the memory limit is reached:
A nested AbortProtect can block the action of MemoryConstrained:
An enclosing AbortProtect is overridden by MemoryConstrained:
While code inside the MemoryConstrained is terminated, AbortProtect runs to completion:
MemoryConstrained can constrain the first argument of CheckAbort, but not the second:
Tech Notes
History
Introduced in 1988 (1.0) | Updated in 2007 (6.0)
Text
Wolfram Research (1988), MemoryConstrained, Wolfram Language function, https://reference.wolfram.com/language/ref/MemoryConstrained.html (updated 2007).
CMS
Wolfram Language. 1988. "MemoryConstrained." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2007. https://reference.wolfram.com/language/ref/MemoryConstrained.html.
APA
Wolfram Language. (1988). MemoryConstrained. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MemoryConstrained.html
BibTeX
@misc{reference.wolfram_2025_memoryconstrained, author="Wolfram Research", title="{MemoryConstrained}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/MemoryConstrained.html}", note=[Accessed: 08-August-2025]}
BibLaTeX
@online{reference.wolfram_2025_memoryconstrained, organization={Wolfram Research}, title={MemoryConstrained}, year={2007}, url={https://reference.wolfram.com/language/ref/MemoryConstrained.html}, note=[Accessed: 08-August-2025]}