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)
ByteCount[Range[10 ^ 6]]MemoryConstrained[Range[10 ^ 6], 10000]Run a command until a memory limit is exceeded:
MemoryConstrained[Table[k = i ^ 2;Range[k], {i, 100}], 10000]The loop ran until memory allocation for the result of Range exceeded the limit:
kScope (2)
Specify the value to return if the time constraint is reached:
MemoryConstrained[Range[10], 10, "too much memory"]Use CheckAbort inside MemoryConstrained to select which parts are skipped if the limit is reached:
MemoryConstrained[CheckAbort[Range[10 ^ 6];Echo["first"], Echo["second"], PropagateAborts -> False];Echo["finished CheckAbort"], 10 ^ 4]Properties & Relations (4)
The third argument is only evaluated if the memory limit is reached:
MemoryConstrained[Sin[π], 100, Print["failure"]]MemoryConstrained[Range[100], 100, Echo["failure"]]A nested AbortProtect can block the action of MemoryConstrained:
MemoryConstrained[AbortProtect[x = Range[10^4];Echo["eval"]], 10^2, Echo["constraint"]]An enclosing AbortProtect is overridden by MemoryConstrained:
AbortProtect[MemoryConstrained[RandomReal[1, 10^5];Echo["eval"], 10^4, Echo["constraint"]]]While code inside the MemoryConstrained is terminated, AbortProtect runs to completion:
AbortProtect[MemoryConstrained[RandomReal[1, 10 ^ 5];Echo["eval"], 10^4, Echo["constraint"]];Echo["past MemoryConstrained"]]MemoryConstrained can constrain the first argument of CheckAbort, but not the second:
MemoryConstrained[CheckAbort[RandomReal[1, 10^10];Echo["eval"], RandomReal[1, 10^5];Echo["constraint"]], 10^3]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_2026_memoryconstrained, author="Wolfram Research", title="{MemoryConstrained}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/MemoryConstrained.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_memoryconstrained, organization={Wolfram Research}, title={MemoryConstrained}, year={2007}, url={https://reference.wolfram.com/language/ref/MemoryConstrained.html}, note=[Accessed: 12-June-2026]}