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.
Explanation
Section titled “Explanation”- 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.
مجموع = 0لكل دورة في مدى(0, 3): ساحه = ساحة(الافتراضي) خاتمه اعد(ساحه) كلمات = تقسيم(دمج_نصوص("ا ب ج ", دورة, ساحه), " ", ساحه) مجموع += طول(كلمات)اطبع("كلمات في الدورات الثلاث: ", مجموع)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:
لكل ع في مدى(0, 3): ن = "سطر " + نص(ع) اطبع(ن)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:
ساحه = ساحة(الافتراضي)ق = قائمة_من([1, 2, 3], ساحه)اعد(ساحه)اطبع(ق[0])Exit code: 3
In the error catalog: E0429
Details in memory in Meem.
Exercise
Section titled “Exercise”Fix the program above so that --debug-mem prints no report, and add up the lengths of the three strings.
Show the solution
مجموع = 0لكل ع في مدى(0, 3): ن = "سطر " + نص(ع) خاتمه حرر(ن) اطبع(ن) مجموع += طول(ن)اطبع("الأحرف: ", مجموع)Common mistakes
Section titled “Common mistakes”- Forgetting to free in a long loop: no error, but memory grows until exit.
--debug-memreveals it by line, and--warn-unprovenwarns 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.
See also
Section titled “See also”Memory in Meem · Memory reference · Allocation and performance of builtins