Skip to content
العربية

20. Allocators and arenas

To know where your program’s memory is allocated from, and to give it back yourself: string by string with حرر, or all at once with an arena; and to find what you forgot with --debug-mem, and what you used after giving it back with the detector.

  • Every string you build, and every list and dictionary, is allocated from an allocator. If you name no allocator, it comes from الافتراضي (the default).
  • Meem has no garbage collector and no reference counting (reference counting was removed in 0.7.0). What you do not free stays until the program exits, then it all returns to the system.
  • An arena (ساحة(الافتراضي)) is an allocator that collects everything allocated from it, and اعد(الساحة) (reset) gives it all back at once. Builtins that build something take the allocator as their last argument.
  • خاتمه اعد(ساحه) right after creating the arena guarantees it is given back when the block exits, whatever the exit.
An arena for each round
مجموع = 0
لكل دورة في مدى(0, 3):
ساحه = ساحة(الافتراضي)
خاتمه اعد(ساحه)
كلمات = تقسيم(دمج_نصوص("ا ب ج ", دورة, ساحه), " ", ساحه)
مجموع += طول(كلمات)
اطبع("كلمات في الدورات الثلاث: ", مجموع)
Output recorded from the compilermeem 16_ساحة.م · meem 0.17.3+434e13f
كلمات في الدورات الثلاث: 12

How to find what you forgot: 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:

A string built in a loop and never freed
لكل ع في مدى(0, 3):
ن = "سطر " + نص(ع)
اطبع(ن)
Output recorded from the compilermeem --debug-mem 16_تسرب.م · meem 0.17.3+434e13f
سطر 0سطر 1سطر 2
[ذاكرة] معلومة: كتل لم تُحرَّر من الافتراضي: 3 (75 بايت)، تعود للنظام عند الخروج  سطر 2: 3 كتل، 75 بايت  تلميح: الحجز المتكرر في سطر واحد (حلقة في الغالب) يُعاد بـ خاتمه حرر(...) بعد إنشائه، أو بساحة للمرحلة

Safety limits: indexing and arithmetic are always protected, and a list or dictionary used through the same name after حرر is error E0428 in every mode. Use after free through another name, or after اعد on its arena, is caught by the detector, which is on by default in a direct run (E0429); in a file built without --debug it is undefined behaviour. Here is a list from an arena used after اعد, in a direct run with no options:

An arena after اعد
ساحه = ساحة(الافتراضي)
ق = قائمة_من([1, 2, 3], ساحه)
اعد(ساحه)
اطبع(ق[0])
Output recorded from the compilermeem ذاكرة_ساحة_بعد_اعد.م · meem 0.17.3+434e13f
[خطأ تشغيل E0429] سطر 4:7 - استعمال القائمة أو القاموس بعد التحرير (أُنشئت في السطر 2 وحُرّرت في السطر 3)  اطبع(ق[0])

Exit code: 3

In the error catalog: E0429

Details in memory in Meem.

Fix the program above so that --debug-mem prints no report, and add up the lengths of the three strings.

Show the solution
Solution
مجموع = 0
لكل ع في مدى(0, 3):
ن = "سطر " + نص(ع)
خاتمه حرر(ن)
اطبع(ن)
مجموع += طول(ن)
اطبع("الأحرف: ", مجموع)
Output recorded from the compilermeem --debug-mem 16_حل.م · meem 0.17.3+434e13f
سطر 0سطر 1سطر 2الأحرف: 15
  • Forgetting to free in a long loop: no error, but memory grows until exit. --debug-mem reveals it by line, and --warn-unproven warns about it before running with warning E0471 «لا دليل على تحرير» (no proof of freeing), outside the default mode since 0.17.0. Inside a function, a local allocation that is neither freed nor passed out is compile error E0472.
  • Freeing the same thing twice through two names: a «double free» caught by the detector (E0350 for a list or dictionary, E0431 for a string, and E0421 for a slot). In a file built without the detector, it crashes the program.
  • Using a list or dictionary after حرر, or after اعد on its arena: a runtime error with the detector, and undefined behaviour in a file built without it. Do not touch what you have freed.

Memory in Meem · Memory reference · Allocation and performance of builtins