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.
Explanation
Section titled “Explanation”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:
- 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. - Free: when you are done with something you built, give it back with
حرر(الاسم)(free), once, and do not use it afterwards. - خاتمه: 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.
# القاعدة 1: ما تبنيه يُحجز: قائمة، ونص بـ +اسماء = ["ليلى", "عمر"]تحية = "أهلاً يا " + اسماء[0]اطبع(تحية)# القاعدة 2: حرّر ما انتهيت منه، مرة واحدةحرر(تحية)# القاعدة 3: خاتمه تحرّر عند الخروج من الكتلة، بجانب سطر الحجزخاتمه حرر(اسماء)لكل ع في مدى(0, 3): سطر = "سطر " + نص(ع) خاتمه حرر(سطر) اطبع(سطر)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.
How the detector tells you
Section titled “How the detector tells you”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:
درجات = [90, 75]حرر(درجات)اطبع(طول(درجات))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:
درجات = [90, 75]نسخة = درجاتحرر(درجات)اطبع(نسخة[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:
درجات = [90, 75]وصف = "عدد الدرجات: " + نص(طول(درجات))اطبع(وصف)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.
Exercise
Section titled “Exercise”Fix the program «forgetting to free» above so that --debug-mem prints nothing, using the third rule.
Show the solution
درجات = [90, 75]خاتمه حرر(درجات)وصف = "عدد الدرجات: " + نص(طول(درجات))خاتمه حرر(وصف)اطبع(وصف)Common mistakes
Section titled “Common mistakes”- 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.
See also
Section titled “See also”Memory in Meem · Memory in long loops