14. Memory in long loops
To apply the three rules in long loops: what is built in each round is freed at its end.
Explanation
Section titled “Explanation”Every string you build, and every list and dictionary, takes a place in memory from an allocator, which is where memory is taken from and given back to. In loops:
- What you build (a string with
+orنص(...), a list, a dictionary) stays until the end of the program if you do not free it, then it all returns on exit. In a short program that does no harm. - A string you build and assign to a variable inside a long loop: free it with
خاتمه حرر(الاسم)right after creating it, and it returns at the end of each round. (حرر(س)gives the space back immediately, andخاتمه جملةdefers the statement until the block exits.) - A string you build and do not assign to a variable, but pass straight to
اطبع,طولor a comparison, returns on its own after use (since 0.8.0):اطبع("سطر " + نص(ع))needs noحرر.
To see what was left, run the program with the --debug-mem option: at the end it prints every line that allocated and did not free, with its number of blocks and their size, largest first. This program builds a thousand strings in a loop without حرر:
ا = "س"لكل ع في مدى(0, 1000): ن = "سطر " + نص(ع)And what meem برنامج.م --debug-mem prints at its end (from the reference handbook):
[ذاكرة] معلومة: كتل لم تُحرَّر من الافتراضي: 1000 (26890 بايت)، تعود للنظام عند الخروج سطر 3: 1000 كتلة، 26890 بايت تلميح: الحجز المتكرر في سطر واحد (حلقة في الغالب) يُعاد بـ خاتمه حرر(...) بعد إنشائه، أو بساحة للمرحلةThe report says, in Arabic: 1000 blocks (26890 bytes) were not freed from the default allocator and return to the system on exit; all of them come from line 3; and a hint that allocation repeated on one line (usually a loop) is given back with خاتمه حرر(...) after creation, or with an arena for the phase.
Example
Section titled “Example”لكل ع في مدى(0, 3): ن = "سطر " + نص(ع) خاتمه حرر(ن) اطبع(ن)Common mistakes
Section titled “Common mistakes”Forgetting to free in a long loop raises the program’s peak memory use, and the --debug-mem option reveals it by line, and --warn-unproven mentions it before running («لا دليل على تحرير», no proof of freeing, E0471). Inside a function, a local string that is neither freed nor returned is compile error E0472. Using a string after freeing it is caught by the detector (on by default when you run meem برنامج.م) as a runtime error with its line. And freeing the same value twice is an error:
م = جديد(1)حرر(م)حرر(م)Exit code: 3
In the error catalog: E0349
م = جديد(1)حرر(م)اطبع(م == لاشيء)Exercise
Section titled “Exercise”Build a thousand strings in a loop («سطر» and its number) and add up their lengths, freeing each string at the end of its round with خاتمه حرر. Run it with --debug-mem: if it prints no memory report, nothing was left.
Show the solution
مجموع = 0لكل ع في مدى(0, 1000): ن = "سطر " + نص(ع) خاتمه حرر(ن) مجموع += طول(ن)اطبع("مجموع الأحرف: ", مجموع)See also
Section titled “See also”Lesson 10: three rules · Manual memory · Memory reference