({{, (RefLink[MemoryConstrained, paclet:ref/MemoryConstrained])[expr,b]
evaluates expr, stopping if more than b bytes of memory are requested. }, {, (RefLink[MemoryConstrained, paclet:ref/MemoryConstrained])[expr,b,failexpr]
returns failexpr if the memory constraint is not met. }})

OpenersNotesSection(Spacer1)Details

(RefLink[MemoryConstrained, paclet:ref/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.

(RefLink[MemoryConstrained, paclet:ref/MemoryConstrained]) evaluates failexpr only if the evaluation is aborted.

(RefLink[MemoryConstrained, paclet:ref/MemoryConstrained]) returns (RefLink[$Aborted, paclet:ref/$Aborted]) if the evaluation is aborted and no failexpr is specified.

Aborts generated by (RefLink[MemoryConstrained, paclet:ref/MemoryConstrained]) are treated just like those generated by (RefLink[Abort, paclet:ref/Abort]), and can thus be overruled by (RefLink[AbortProtect, paclet:ref/AbortProtect]).

(RefLink[MemoryConstrained, paclet:ref/MemoryConstrained])[expr,(RefLink[Infinity, paclet:ref/Infinity])] imposes no memory constraint.

OpenersPrimaryExamplesSection(Spacer1)Examples  (2)

OpenersExampleSection0(Spacer1)Basic 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 (RefLink[Range, paclet:ref/Range]) exceeded the limit:

k

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[TimeConstrained, paclet:ref/TimeConstrained])  ▪  (RefLink[MemoryAvailable, paclet:ref/MemoryAvailable])  ▪  (RefLink[MemoryInUse, paclet:ref/MemoryInUse])  ▪  (RefLink[MaxMemoryUsed, paclet:ref/MaxMemoryUsed])  ▪  (RefLink[$RecursionLimit, paclet:ref/$RecursionLimit])  ▪  (RefLink[Abort, paclet:ref/Abort])  ▪  (RefLink[Interrupt, paclet:ref/Interrupt])  ▪  (RefLink[AllowedCloudExtraParameters, paclet:ref/AllowedCloudExtraParameters])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Memory Management, paclet:tutorial/MemoryManagement])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Tuning & Debugging, paclet:guide/TuningAndDebugging])}, {▪(RefLinkPlain[Memory Measurement & Optimization, paclet:guide/MemoryMeasurementAndOptimization])}, {▪(RefLinkPlain[Flow Control, paclet:guide/FlowControl])}, {▪(RefLinkPlain[Conditionals, paclet:guide/Conditionals])}, {▪(RefLinkPlain[Evaluation Control, paclet:guide/EvaluationControl])}, {▪(RefLinkPlain[Global Computation Settings & Parameters, paclet:guide/GlobalComputationSettingsAndParameters])}, {▪(RefLinkPlain[Package Bulletproofing, paclet:guide/PackageBulletproofing])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])(WebLink[: Debugging Your Code, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])}})}})