Skip to content
العربية

14. Memory in long loops

To apply the three rules in long loops: what is built in each round is freed at its end.

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:

  1. 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.
  2. 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.)
  3. 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
لكل ع في مدى(0, 3):
ن = "سطر " + نص(ع)
خاتمه حرر(ن)
اطبع(ن)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
سطر 0سطر 1سطر 2

Try this example in the playground

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:

Causes the error
م = جديد(1)
حرر(م)
حرر(م)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
[خطأ تشغيل E0349] سطر 3:1 - تحرير مؤشر قيمته لاشيء (هل حُرّر من قبل؟)  حرر(م)

Exit code: 3

In the error catalog: E0349

Try this example in the playground

Fixes it
م = جديد(1)
حرر(م)
اطبع(م == لاشيء)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
صواب

Try this example in the playground

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
Solution
مجموع = 0
لكل ع في مدى(0, 1000):
ن = "سطر " + نص(ع)
خاتمه حرر(ن)
مجموع += طول(ن)
اطبع("مجموع الأحرف: ", مجموع)
Output recorded from the compilermeem --debug-mem 13_حل.م · meem 0.17.3+434e13f
مجموع الأحرف: 6890

Lesson 10: three rules · Manual memory · Memory reference