Skip to content
العربية

10. Memory for beginners: three rules

To know three rules for memory in Meem, and to read the detector’s message when you break one: which line, and what happened.

Meem has no garbage collector: the memory your program takes, you give back, or it all returns when the program exits. Three rules are enough for now:

  1. Allocate: what you build takes a place in memory: a list [...], a dictionary {...}, and a string you build with + or نص(...). A string written between quotes as it is is not allocated.
  2. Free: when you are done with something you built, give it back with حرر(الاسم) (free), once, and do not use it afterwards.
  3. خاتمه: you write خاتمه حرر(الاسم) (defer free) next to the allocating line, and the thing is freed on its own when the block exits (the end of the program, or the end of each round of a loop). That way you do not forget it.
The three rules
# القاعدة 1: ما تبنيه يُحجز: قائمة، ونص بـ +
اسماء = ["ليلى", "عمر"]
تحية = "أهلاً يا " + اسماء[0]
اطبع(تحية)
# القاعدة 2: حرّر ما انتهيت منه، مرة واحدة
حرر(تحية)
# القاعدة 3: خاتمه تحرّر عند الخروج من الكتلة، بجانب سطر الحجز
خاتمه حرر(اسماء)
لكل ع في مدى(0, 3):
سطر = "سطر " + نص(ع)
خاتمه حرر(سطر)
اطبع(سطر)
Output recorded from the compilermeem --debug-mem 10ب_قواعد.م · meem 0.17.3+434e13f
أهلاً يا ليلىسطر 0سطر 1سطر 2

The example was run with the --debug-mem option, which prints at the end of the program everything that was allocated and not freed. It printed nothing, so everything allocated came back.

meem برنامج.م runs your program with the memory detector: it watches what you allocate and what you free. If you break the second rule, it stops the program with a message that names the line.

Using a list after freeing it:

Use after free
درجات = [90, 75]
حرر(درجات)
اطبع(طول(درجات))
Output recorded from the compilermeem 10ب_بعد_حرر.م · meem 0.17.3+434e13f
[خطأ تشغيل E0428] سطر 3:10 - الحاوية 'درجات' استُعملت بعد تحريرها (قيمتها لاشيء)  اطبع(طول(درجات))

Exit code: 3

In the error catalog: E0428

سطر 3:10 means line 3, character 10. E0428 is the error’s ID, explained in the error catalog.

If you reach the same list through another name, the detector also says where it was created and where it was freed:

The other name sees the freed list
درجات = [90, 75]
نسخة = درجات
حرر(درجات)
اطبع(نسخة[0])
Output recorded from the compilermeem 10ب_اسم_آخر.م · meem 0.17.3+434e13f
[خطأ تشغيل E0429] سطر 4:10 - استعمال القائمة أو القاموس بعد التحرير (أُنشئت في السطر 1 وحُرّرت في السطر 3)  اطبع(نسخة[0])

Exit code: 3

In the error catalog: E0429

نسخة = درجات does not copy the list: the two names are for one list, so when it was freed on line 3, nothing was left for نسخة. (E0429)

If you forget to free at the top level of the file, there is no error: everything returns on exit. But --debug-mem tells you what was left and its line:

Forgetting to free
درجات = [90, 75]
وصف = "عدد الدرجات: " + نص(طول(درجات))
اطبع(وصف)
Output recorded from the compilermeem --debug-mem 10ب_نسيان.م · meem 0.17.3+434e13f
عدد الدرجات: 2
[ذاكرة] معلومة: كتل لم تُحرَّر من الافتراضي: 3 (97 بايت)، تعود للنظام عند الخروج  سطر 1: كتلتان، 56 بايت  سطر 2: كتلة واحدة، 41 بايت  تلميح: الحجز المتكرر في سطر واحد (حلقة في الغالب) يُعاد بـ خاتمه حرر(...) بعد إنشائه، أو بساحة للمرحلة

Line 1 allocated the list (two blocks: its header and its data), and line 2 allocated the string.

Inside a function, though, forgetting is a compile error (since 0.14.0): a string or list you build in a local variable, and neither free nor return nor pass out of the function, is lost on every call, so the compiler rejects it with error E0472, and its hint says what to write: خاتمه حرر(ن) after the allocation, or ارجع ن (return) if the caller should own it.

Fix the program «forgetting to free» above so that --debug-mem prints nothing, using the third rule.

Show the solution
Solution
درجات = [90, 75]
خاتمه حرر(درجات)
وصف = "عدد الدرجات: " + نص(طول(درجات))
خاتمه حرر(وصف)
اطبع(وصف)
Output recorded from the compilermeem --debug-mem 10ب_حل.م · meem 0.17.3+434e13f
عدد الدرجات: 2
  • Using something after حرر: runtime error E0428 through the same name, and E0429 through another name. Move حرر after the last use, or use خاتمه حرر.
  • Freeing the same thing twice: through the same name, E0347 «تحرير حاوية قيمتها لاشيء» (freeing a container whose value is nothing), and through two names a «تحرير مزدوج» (double free) (E0350 for a list or dictionary, and E0431 for a string).
  • Building a string or a container in a local variable inside a function and then forgetting it: compile error E0472. Write خاتمه حرر(الاسم) next to the allocation, or return it.
  • Thinking that ب = ا copies the list: for an independent copy use نسخ(ا) (copy), and free each of them.

Memory in Meem · Memory in long loops