Chapterبەش 1 · Lectureوانە 1.4

Memory While a Program Runs: Stack, Heap and Garbage Collectionبیرگە لە کاتی کارکردنی بەرنامەدا: ستاکی بانگکردن، هیپی بیرگە و فڕێدانی بەجێماوەکان

Where a running program keeps its data: the call stack with one frame per call, the heap for values that live longer, and the references and pointers that lead to them; what garbage is; freeing memory by hand and its three classic bugs (leaks, dangling pointers, double frees); and automatic garbage collection: reference counting and its cycle problem, mark and sweep, generations, what Python, Java, JavaScript, Dart, PHP, Swift, C, C++ and Rust do in 2026, and the leaks that still happen with a collector.

کارا لە کوێ هەڵدەگرێت: بە چوارچێوەیەک بۆ هەر بانگکردنێک، بۆ ئەو نرخانەی زیاتر دەمێننەوە و ئەو و دەیانگەیەننە لایان؛ بەجێماو چییە؛ ئازادکردنەوەی بە دەست و سێ کلاسیکییەکەی (، ، )؛ و خۆکار: و کێشەی بازنەکان، ، نەوەکان، ئەوەی Python، Java، JavaScript، Dart، PHP، Swift، C، ⁦C++⁩ و Rust لە ⁦2026⁩دا دەیکەن و ئەو حاڵەتانەی کە لەگەڵ سیستەمی ڕوو دەدەن.

22 min read24 خولەک خوێندنەوە · 20 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Explain what the call stack and the heap hold, and why a value that must outlive a call goes on the heap.
  • Say in plain words what a reference and a pointer are, and when an object becomes garbage.
  • Describe manual memory management and recognise a memory leak, a dangling pointer and a double free in pseudocode.
  • Trace reference counting and mark and sweep on a small example, including a reference cycle.
  • Compare how popular languages free memory in 2026, and avoid the leaks that happen even with a garbage collector.
  • ڕوونی بکەیتەوە و چی هەڵدەگرن و بۆچی نرخێک کە دەبێت لە بانگکردنێک زیاتر بمێنێت دەچێتە .
  • بە زمانێکی سادە بڵێیت و چین و کەی دەبێت بە بەجێماو.
  • بەڕێوەبردنی دەستیی وەسف بکەیت و ، و لە بناسیتەوە.
  • و لەسەر نموونەیەکی بچووک هەنگاو بە هەنگاو جێبەجێ بکەیت، لەگەڵ .
  • بەراوردی ئەوە بکەیت کە زمانە باوەکان لە ⁦2026⁩دا چۆن ئازاد دەکەنەوە و خۆت لە بپارێزیت، تەنانەت لەگەڵ سیستەمی .

Where does a running program keep things?بەرنامەیەکی کارا شتەکانی لە کوێ هەڵدەگرێت؟

When you start a program, the operating system loads its instructions into RAM and gives it memory for its data. Every number the program calculates, every list of names and every picture it opens must sit somewhere in that memory while the program runs, and the space must be given back when it is no longer needed. This lesson shows how that works, whatever language you use.

کاتێک دەکەیتەوە، بار دەکاتە ناو RAM و بەشێک لە بۆ دەداتێ. هەر ژمارەیەک حیسابی دەکات، هەر لیستێکی ناو و هەر وێنەیەک کە دەیکاتەوە، دەبێت لە کاتی کارکردندا لە شوێنێکی ئەو بێت و کاتێک ئیتر پێویست نەما، شوێنەکەی دەبێت بگەڕێنرێتەوە. ئەم وانەیە پیشانت دەدات ئەمە چۆن کار دەکات، هەر زمانێک بەکار بهێنیت.

A running program divides its memory into a few areas. Two matter most to a programmer: the call stack, a neat pile of small boxes for the function calls that are running now, and the heap, a large area for everything that must live longer. The picture is a simplified map; real systems add more detail, but every language uses these two ideas.

کارا دابەش دەکات بۆ چەند ناوچەیەک. دوانیان بۆ بەرنامەساز گرنگترینن: ، کۆمەڵە سندوقێکی بچووکی ڕێک بۆ هەر کە ئێستا کار دەکات و ، ناوچەیەکی گەورە بۆ هەموو ئەو شتانەی دەبێت ماوەیەکی درێژتر بمێننەوە. وێنەکە نەخشەیەکی سادەکراوە؛ سیستەمە ڕاستەقینەکان وردەکاریی زیاتریان هەیە، بەڵام هەموو زمانێک ئەم دوو بیرۆکەیە بەکار دەهێنێت.

A tall bar shows one program's memory in RAM, divided into areas. At the bottom: code, the program's instructions; above it: global data, the global variables. Then the heap, which grows upwards when the program asks for room, for objects that live longer. In the middle: free memory. At the top: the call stack, which grows downwards with one frame for every running call. Arrows show the heap and the stack growing towards each other.ستوونێکی درێژ بیرگەی بەرنامەیەک لە RAMـدا پیشان دەدات کە بۆ چەند ناوچەیەک دابەش کراوە. لە خوارەوە: کۆد، ڕێکارنامەکانی بەرنامەکە؛ لە سەرییەوە: دراوەی گشتی، واتە گۆڕاوە گشتییەکان. پاشان هیپی بیرگە کە بۆ ئۆبجێکتی تەمەندرێژ کاتێک بەرنامەکە شوێن داوا دەکات بەرەو سەرەوە گەورە دەبێت. لە ناوەڕاستدا: بیرگەی بەتاڵ. لە سەرەوە: ستاکی بانگکردن کە بۆ هەر بانگکردنێکی کارا چوارچێوەیەک بەرەو خوارەوە گەورە دەبێت. تیرەکان پیشان دەدەن هیپی بیرگە و ستاکی بانگکردن بەرەو یەکتر گەورە دەبن.
One program's memory, simplified: the call stack and the heap grow towards each other. بە سادەیی: و بەرەو یەکتر گەورە دەبن.CêIT

The call stack: one frame per callستاکی بانگکردن: بۆ هەر بانگکردنێک چوارچێوەیەک

Each time a function is called, the program puts a new frame on top of the call stack. The frame holds the function's parameters, its local variables and the place to go back to when it finishes. When the function returns, its frame is taken off, and its variables disappear with it. Follow this bakery program: the main part calls bill, and bill calls discount.

هەر جارێک بانگ دەکرێت، چوارچێوەیەکی نوێ دەخاتە سەر . چوارچێوەکە ، و ئەو شوێنەی کە دوای تەواوبوون دەبێت بۆی بگەڕێتەوە هەڵدەگرێت. کاتێک دەگەڕێتەوە، چوارچێوەکەی لادەبرێت و ناویشی لەگەڵیدا نامێنن. سەیری ئەم نانەواخانە بکە: بەشی سەرەکی بانگی bill دەکات و bill بانگی discount دەکات.

A bakery bill in pseudocodeپسوولەی نانەواخانە بە نیمچەکۆدTEXT
FUNCTION discount(total)
    off ← total × 10 ÷ 100
    RETURN total − off
END FUNCTION

FUNCTION bill(count, price)
    total ← count × price
    IF count > 10 THEN
        total ← discount(total)
    END IF
    RETURN total
END FUNCTION

START
loaves ← 12
toPay ← bill(loaves, 250)
OUTPUT toPay
END
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 16

The call puts a frame for bill on the stack, with count = 12 and price = 250.

بانگکردنەکە چوارچێوەیەک بۆ bill دەخاتە سەر ، کە count = ⁦12⁩ و price = ⁦250⁩ـی تێدایە.

Lineدێڕی 9

12 loaves are more than 10, so bill calls discount: a third frame goes on top.

⁦12⁩ نان لە ⁦10⁩ زیاترە، بۆیە bill بانگی discount دەکات: چوارچێوەی سێیەم دەچێتە سەرەوە.

Lineدێڕی 3

RETURN takes the frame of discount off; its off is gone, and bill goes on with 2700.

RETURN چوارچێوەی discount لادەبات؛ offـەکەی نامێنێت و bill بە ⁦2700⁩ بەردەوام دەبێت.

The call stack while discount runs, drawn as three boxes on top of each other. Bottom: main, with loaves = 12 and toPay not set yet. Middle: bill, with count = 12, price = 250, total = 3000, and a note that it returns to line 16. Top, highlighted as running now: discount, with total = 3000, off = 300, and a note that it returns to line 9. Beside the stack: a call pushes a new frame on top; a return pops the top frame.ستاکی بانگکردن لە کاتێکدا discount کار دەکات، وەک سێ سندوق لەسەر یەک. خوارەوە: main لەگەڵ loaves = 12 و toPay کە هێشتا نرخی نییە. ناوەڕاست: bill لەگەڵ count = 12، price = 250، total = 3000 و تێبینییەک کە بۆ دێڕی 16 دەگەڕێتەوە. سەرەوە، بە ڕەنگی جیاواز وەک ئەوەی ئێستا کار دەکات: discount لەگەڵ total = 3000، off = 300 و تێبینییەک کە بۆ دێڕی 9 دەگەڕێتەوە. لە تەنیشتییەوە: بانگکردن چوارچێوەیەکی نوێ دەخاتە سەرەوە؛ گەڕانەوە چوارچێوەی سەرەوە لادەبات.
While discount runs: three frames, the newest on top.لە کاتێکدا discount کار دەکات: سێ چوارچێوە، نوێترینیان لە سەرەوە.CêIT

The call stack: one frame per callستاکی بانگکردن: بۆ هەر بانگکردنێک چوارچێوەیەک

Why the stack is fast, and its limitبۆچی ستاکی بانگکردن خێرایە و سنوورەکەی

  • Fast and automatic. Making room only moves the top of the stack. Nobody searches for free space, and nothing is freed by hand: a frame disappears when its call ends.
  • Known sizes. The size of a frame is known when the function is called, so the stack suits values of a fixed size, such as a number.
  • Short lives. A local variable lives only while its call runs; after the return it is gone.
  • Small. The stack is much smaller than the heap. Too many waiting calls, usually a function that keeps calling itself, fill it up: a , which stops the program.
  • خێرا و خۆکار. شوێنکردنەوە تەنیا سەری دەجووڵێنێت. کەس بە دوای شوێنی ئازاددا ناگەڕێت و هیچ شتێک بە دەست ئازاد ناکرێتەوە: چوارچێوەیەک کاتێک بانگکردنەکەی تەواو دەبێت نامێنێت.
  • قەبارەی زانراو. قەبارەی چوارچێوەیەک کاتی بانگکردنی دەزانرێت، بۆیە بۆ ئەو نرخانە گونجاوە کە قەبارەیان جێگیرە، وەک ژمارەیەک.
  • تەمەنی کورت. تەنیا تا بانگکردنەکەی کار دەکات دەژی؛ دوای گەڕانەوە نامێنێت.
  • بچووک. زۆر لە بچووکترە. بانگکردنی چاوەڕوانی زۆر، زۆرجار کە بێ وەستان بانگی خۆی دەکاتەوە، پڕی دەکەن: ، کە دەوەستێنێت.

The heap: room for things that live longerهیپی بیرگە: شوێن بۆ ئەو شتانەی زیاتر دەمێننەوە

Some values must outlive the call that made them, or have a size that nobody knows in advance: a class list that grows, a photo, a chat history. They go to the heap. The program asks for a piece of the heap, which is called memory allocation, and gets back where that piece is. The piece stays reserved until it is freed, not when a function ends.

هەندێک نرخ دەبێت لەو بانگکردنە زیاتر بمێنن کە دروستی کردوون، یان قەبارەیەکیان هەیە کە کەس پێشوەخت نایزانێت: لیستی پۆلێک کە گەورە دەبێت، وێنەیەک، مێژووی چاتێک. ئەمانە دەچنە . پارچەیەک لە داوا دەکات، کە پێی دەوترێت و شوێنی ئەو پارچەیە وەردەگرێتەوە. پارچەکە تا ئازاد دەکرێتەوە بۆی دەمێنێتەوە، نەک تا تەواو دەبێت.

The storerooms of a bazaar khanکۆگاکانی خانێکی بازاڕ

In an old bazaar khan, a trader rents a storeroom and gets a receipt with the room number. The goods stay in the room for as long as the room is rented, wherever the trader goes. The receipt does not hold the goods; it tells you where they are. When the trader gives the room back, the khan can rent it to someone else.

لە خانێکی کۆنی بازاڕدا، بازرگانێک کۆگایەک بە کرێ دەگرێت و پسوولەیەک بە ژمارەی ژوورەکەوە وەردەگرێت. کاڵاکان تا ژوورەکە بە کرێ گیرابێت تێیدا دەمێننەوە، بازرگانەکە بچێتە هەر کوێیەک. پسوولەکە کاڵاکان هەڵناگرێت؛ پێت دەڵێت لە کوێن. کاتێک بازرگانەکە ژوورەکە دەگەڕێنێتەوە، خانەکە دەتوانێت بیداتە کەسێکی تر.

Where the analogy stops workingسنووری ئەم نموونەیە

A khan keeps a written list of rented rooms. A program's heap must also keep track of which pieces are free, and that bookkeeping costs time, which the stack does not need.

خان لیستێکی نووسراوی ژوورە بە کرێدراوەکانی هەیە. دەبێت بزانێت کام پارچانە بەتاڵن و ئەم ژمێریارییە کاتی دەوێت، کە پێویستی پێی نییە.

Two columns side by side. Call stack: made and freed automatically with each call, very fast, last in first out, small, holds frames with parameters and local variables, and lives only as long as the call. Heap: the program asks for room, any size and any order, large, holds objects, lists and long text, and lives until it is freed by hand or by the garbage collector.دوو ستوون لە تەنیشت یەک. ستاکی بانگکردن: لەگەڵ هەر بانگکردنێک بە شێوەی خۆکار دروست و ئازاد دەبێت، زۆر خێرایە، دوایین شت یەکەم دەردەچێت، بچووکە، چوارچێوە بە پارامیتەرەکان و گۆڕاوە ناوخۆییەکانەوە هەڵدەگرێت و تەنیا تا بانگکردنەکە دەژی. هیپی بیرگە: بەرنامەکە شوێن داوا دەکات، هەر قەبارە و هەر ڕیزبەندییەک، گەورەیە، ئۆبجێکتەکان، لیست و دەقی درێژ هەڵدەگرێت و تا بە دەست یان بە فڕێدانی بەجێماوەکان ئازاد دەکرێتەوە دەژی.
The same RAM, two ways of using it.هەمان ، دوو ڕێگای بەکارهێنان.CêIT

The heap: room for things that live longerهیپی بیرگە: شوێن بۆ ئەو شتانەی زیاتر دەمێننەوە

Example: a list that outlives its functionنموونە: لیستێک کە لە کردارەکەی زیاتر دەمێنێت

makeClass builds a list of students and returns it. Watch what happens to the frame and to the list.

makeClass لیستێکی خوێندکاران دروست دەکات و دەیگەڕێنێتەوە. سەیر بکە چی بەسەر چوارچێوەکە و لیستەکە دێت.

A list made inside a functionلیستێک کە لە ناو کردارێکدا دروست دەکرێتTEXT
FUNCTION makeClass()
    names ← NEW LIST
    ADD "Aram" TO names
    ADD "Shilan" TO names
    RETURN names
END FUNCTION

START
myClass ← makeClass()
OUTPUT LENGTH(myClass)
END
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

NEW LIST makes the list on the heap. names, in the frame of makeClass, only holds where the list is.

NEW LIST لیستەکە لە دروست دەکات. names لە چوارچێوەی makeClassـدا تەنیا شوێنی لیستەکە هەڵدەگرێت.

Lineدێڕی 5

Only that reference is returned. The frame of makeClass is taken off the stack, but the list stays on the heap.

تەنیا ئەو دەگەڕێنرێتەوە. چوارچێوەی makeClass لە لادەبرێت، بەڵام لیستەکە لە دەمێنێتەوە.

Lineدێڕی 9

myClass in the main frame now refers to the same list.

ئێستا myClass لە چوارچێوەی بەشی سەرەکیدا ئاماژە بە هەمان لیست دەکات.

Left, the call stack: the frame of makeClass is drawn with a dashed outline and the word popped; its variable names pointed to the list. Below it, the main frame with myClass, whose arrow crosses to the right. Right, the heap: a list box holding Aram and Shilan. The words below say the frame is gone, but the list stays because myClass still refers to it.چەپ، ستاکی بانگکردن: چوارچێوەی makeClass بە هێڵی پچڕپچڕ و وشەی «لابرا» کێشراوە؛ گۆڕاوەکە names ئاماژەی بە لیستەکە دەکرد. لە خوارییەوە چوارچێوەی بەشی سەرەکی لەگەڵ myClass کە تیرەکەی بۆ لای ڕاست دەچێت. ڕاست، هیپی بیرگە: سندوقی لیستێک کە Aram و Shilanـی تێدایە. نووسینی خوارەوە دەڵێت چوارچێوەکە نەما، بەڵام لیستەکە دەمێنێتەوە چونکە myClass هێشتا ئاماژەی پێ دەکات.
The frame is gone; the list lives on in the heap.چوارچێوەکە نەما؛ لیستەکە لە دەژی.CêIT

Each language decides the details. In Java, a local int sits inside the frame while every object and array is on the heap; in Python every value, even a number, is an object on the heap, and frames hold only references to them.

هەر زمانێک وردەکارییەکان خۆی دیاری دەکات. لە Javaـدا intـێکی ناوخۆیی لە ناو چوارچێوەکەدایە، بەڵام هەموو و لە ؛ لە Pythonـدا هەموو نرخێک، تەنانەت ژمارەیەکیش، لە و چوارچێوەکان تەنیا بۆ ئەوان هەڵدەگرن.

References and pointers in plain wordsئاماژەکان و ئاماژەدەرەکان بە زمانێکی سادە

A variable in a frame cannot hold a whole list from the heap. It holds where the list is: its address, a number for a place in memory (memory addresses). When a language lets you see that address and even calculate with it, the variable is a pointer, as in C and C++. Most other languages give you a reference instead: it leads to the object, but the language hides the address and checks how you use it.

لە ناو چوارچێوەیەکدا ناتوانێت هەموو لیستێکی هەڵبگرێت. ئەوەی هەڵدەگرێت شوێنی لیستەکەیە: ، ژمارەیەک بۆ شوێنێک لە (ناونیشانەکانی بیرگە). کاتێک زمانێک ڕێگەت دەدات ئەو ناونیشانە ببینیت و تەنانەت حیسابی پێ بکەیت، ، وەک لە C و ⁦C++⁩ـدا. زۆربەی زمانەکانی تر لە جیاتی ئەوە دەدەن: دەتباتە لای ، بەڵام زمانەکە ناونیشانەکە دەشارێتەوە و چاودێریی ئەوە دەکات چۆن بەکاری دەهێنیت.

Two variables can refer to the same object. A change made through one is then seen through the other, because there is only one object: see Two names, one list and Two variables, one object. A reference to nothing is null (Python writes None).

دوو دەتوانن ئاماژە بە هەمان بکەن. ئەو کاتە گۆڕانکارییەک لە ڕێگەی یەکێکیانەوە لە ئەویتریشدا دەبینرێت، چونکە تەنیا یەک هەیە: سەیری دوو ناو، یەک لیست و دوو گۆڕاو، یەک ئۆبجێکت بکە. بۆ هیچ شتێک (Python دەینووسێت None).

Left, the call stack with three variables. a and b both hold arrows that lead to the same list object at address 5000 in the heap. c holds null and points nowhere. Below, a pointer p shows the number 5000 itself, labelled: a pointer is the address as a number, and C and C++ can calculate with it.چەپ، ستاکی بانگکردن بە سێ گۆڕاوەوە. a و b هەردووکیان تیرێک هەڵدەگرن کە دەچێتە سەر هەمان ئۆبجێکتی لیست لە ناونیشانی 5000 لە هیپی بیرگەدا. c بێنرخ هەڵدەگرێت و ئاماژە بە هیچ ناکات. لە خوارەوە، ئاماژەدەرێک بە ناوی p خودی ژمارە 5000 پیشان دەدات، لەگەڵ تێبینییەک: ئاماژەدەر ناونیشانەکەیە وەک ژمارە و C و C++ دەتوانن حیسابی پێ بکەن.
a and b: two references to one object. p: a pointer whose value is the address.a و b: دوو بۆ یەک . p: کە نرخەکەی ناونیشانەکەیە.CêIT

What is garbage?بەجێماو چییە؟

An object is garbage when the program can no longer reach it: no variable and no living object refers to it, directly or through a chain of references. The program can never use it again, but it still takes up room on the heap until something frees it.

بەجێماوە کاتێک ئیتر ناتوانێت پێی بگات: هیچ و هیچ زیندوو ئاماژەی پێ ناکات، نە ڕاستەوخۆ و نە بە زنجیرەیەک . هەرگیز ناتوانێت دووپاتە بەکاری بهێنێتەوە، بەڵام تا شتێک ئازادی دەکاتەوە هێشتا شوێن لە دەگرێت.

Making garbageدروستکردنی بەجێماوTEXT
basket ← NEW LIST
ADD "apples" TO basket
basket ← NEW LIST
ADD "grapes" TO basket
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

basket now refers to a new list. Nothing refers to the list with apples any more: it is garbage.

ئێستا basket ئاماژە بە لیستێکی نوێ دەکات. ئیتر هیچ شتێک ئاماژە بە لیستی سێوەکان ناکات: بووە بە بەجێماو.

Where does the search for living objects start? At the roots: the variables in every frame on the call stack, the global variables and a few references the system keeps for itself. An object that can be reached from a root by following references is alive; everything else is garbage.

گەڕان بە دوای زیندوودا لە کوێوە دەست پێ دەکات؟ لە : هەموو چوارچێوەیەکی سەر ، و چەند تر کە سیستەمەکە بۆ خۆی ڕایاندەگرێت. کە بە شوێنکەوتنی لە پێی بگەیت زیندووە؛ هەموو شتەکانی تر بەجێماون.

Left, the roots: basket in a stack frame and a global variable settings. basket points to the list with grapes; settings points to a config object, which points to a language object. These three are alive. Right, without any arrow from a root: the old list with apples and two objects X and Y that point only to each other. All three are greyed out and labelled garbage.چەپ، ڕەگەکان: basket لە چوارچێوەیەکی ستاکی بانگکردندا و گۆڕاوێکی گشتی بە ناوی settings. basket ئاماژە بە لیستی ترێ دەکات؛ settings ئاماژە بە ئۆبجێکتێکی ڕێکخستن دەکات کە ئاماژە بە ئۆبجێکتێکی زمان دەکات. ئەم سێیە زیندوون. ڕاست، بێ هیچ تیرێک لە ڕەگێکەوە: لیستە کۆنەکەی سێو و دوو ئۆبجێکت بە ناوی X و Y کە تەنیا ئاماژە بە یەکتر دەکەن. هەر سێکیان کاڵ کراونەتەوە و نووسراوە بەجێماو.
Reachable from a root: alive. Not reachable: garbage, even two objects that point at each other.گەیشتن لە : زیندوو. نەگەیشتن: بەجێماو، تەنانەت دوو کە ئاماژە بە یەکتر دەکەن.CêIT

Freeing memory by handئازادکردنەوەی بیرگە بە دەست

In C, and in older C++, the programmer does both jobs: asks for heap memory and gives it back. C's malloc asks for a number of bytes and returns a pointer; free gives the memory back. In pseudocode:

لە Cـدا و لە ⁦C++⁩ـی کۆندا، بەرنامەساز هەردوو کارەکە دەکات: داوا دەکات و دەیگەڕێنێتەوە. mallocـی C ژمارەیەک داوا دەکات و دەگەڕێنێتەوە؛ free دەگەڕێنێتەوە. بە :

Allocate, use, freeتەرخانکردن، بەکارهێنان، ئازادکردنەوەTEXT
p ← ALLOCATE(100)          // in C: p = malloc(100);
WRITE "Salam" AT p
OUTPUT TEXT AT p
FREE(p)                    // in C: free(p);
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

100 bytes of the heap are reserved; p gets their address.

⁦100⁩ لە تەرخان دەکرێت؛ p ناونیشانەکەیان وەردەگرێت.

Lineدێڕی 4

The 100 bytes go back to the heap and can be given to the next request.

⁦100⁩ دەگەڕێنەوە بۆ و دەتوانرێت بدرێنە داواکاریی داهاتوو.

This gives full control: memory is freed exactly when you say, with no surprise pauses, which is why operating systems and small devices are often written this way. The price: every allocation must be freed exactly once, at the right time, and people make mistakes.

ئەمە کۆنترۆڵی تەواو دەدات: ڕێک ئەو کاتە ئازاد دەکرێتەوە کە تۆ دەڵێیت، بێ وەستانی چاوەڕواننەکراو، بۆیە و ئامێرە بچووکەکان زۆرجار بەم شێوەیە دەنووسرێن. نرخەکەی: هەر دەبێت ڕێک یەک جار و لە کاتی خۆیدا ئازاد بکرێتەوە و مرۆڤ هەڵە دەکات.

Three steps from left to right along a strip of heap memory. 1, ALLOCATE(100): the place from 5000 to 5099 is coloured as reserved and p holds 5000. 2, use: Salam is written at p. 3, FREE(p): the place is white again and marked free, ready for the next request; p still shows 5000, with a warning that it is now old.سێ هەنگاو لە چەپەوە بۆ ڕاست لەسەر پارچەیەک لە هیپی بیرگە. 1، ALLOCATE(100): شوێنی 5000 تا 5099 ڕەنگ کراوە وەک تەرخانکراو و p ژمارەی 5000 هەڵدەگرێت. 2، بەکارهێنان: Salam لە p دەنووسرێت. 3، FREE(p): شوێنەکە دووپاتە سپی بووەتەوە و نووسراوە بەتاڵ، ئامادەیە بۆ داواکاریی داهاتوو؛ p هێشتا 5000 پیشان دەدات، لەگەڵ ئاگادارییەک کە ئێستا کۆنە.
Manual memory: every ALLOCATE needs exactly one FREE. دەستی: هەر ALLOCATE پێویستی بە ڕێک یەک FREE هەیە.CêIT

Freeing memory by handئازادکردنەوەی بیرگە بە دەست

Three classic mistakesسێ هەڵەی کلاسیکی

Back at the khan: a trader can forget to give a room back, give it back but keep using the old receipt, or give the same room back twice. Programs make the same three mistakes:

با بگەڕێینەوە بۆ خانەکە: بازرگانێک دەتوانێت گەڕاندنەوەی ژوورێک لەبیر بکات، بیگەڕێنێتەوە بەڵام هێشتا پسوولە کۆنەکە بەکار بهێنێت، یان هەمان ژوور دوو جار بگەڕێنێتەوە. هەمان سێ هەڵە دەکەن:

  • : memory that is never freed. The program's memory use grows until it slows down or stops; a server that runs for months is hit hardest.
  • : a pointer used after FREE. The memory may already belong to something else, so the program reads wrong data, crashes or overwrites another value.
  • : the same memory freed twice. The heap's list of free places breaks, and one place can later be handed out to two owners.
  • : کە هەرگیز ئازاد ناکرێتەوە. بەکارهێنانی زیاد دەبێت تا خاو دەبێت یان دەوەستێت؛ کە مانگەها کار دەکات زۆرتر زیانی پێ دەگات.
  • : کە دوای FREE بەکار دێت. لەوانەیە ئێستا هی شتێکی تر بێت، بۆیە هەڵە دەخوێنێتەوە، دەوەستێت یان نرخێکی تر دەسڕێتەوە.
  • : هەمان دوو جار ئازاد دەکرێتەوە. لیستی شوێنە ئازادەکانی تێک دەچێت و دواتر لەوانەیە یەک شوێن بدرێتە دوو خاوەن.
Three panels with pseudocode and a picture of the heap. 1 Memory leak: inside REPEAT 1000 TIMES, p ← ALLOCATE(1 MB) with no FREE; the heap fills with blocks that nothing points to, about 1000 MB lost. 2 Dangling pointer: p ← ALLOCATE(100), FREE(p), WRITE Hi AT p; p's arrow still leads to a place now marked free or given to someone else. 3 Double free: q ← p, FREE(p), FREE(q); two arrows lead to one block that is freed twice.سێ بەش بە نیمچەکۆد و وێنەی هیپی بیرگە. 1 دزەکردنی بیرگە: لە ناو REPEAT 1000 TIMES، p ← ALLOCATE(1 MB) بێ FREE؛ هیپی بیرگە پڕ دەبێت لە پارچەی وا کە هیچ شتێک ئاماژەیان پێ ناکات، نزیکەی 1000 MB لەدەست دەچێت. 2 ئاماژەدەری هەڵواسراو: p ← ALLOCATE(100)، FREE(p)، WRITE Hi AT p؛ تیرەکەی p هێشتا دەچێتە سەر شوێنێک کە ئێستا بەتاڵە یان دراوەتە کەسێکی تر. 3 ئازادکردنەوەی دووجارە: q ← p، FREE(p)، FREE(q)؛ دوو تیر دەچنە سەر یەک پارچە کە دوو جار ئازاد دەکرێتەوە.
Forget to free, use after free, free twice.ئازادکردنەوە لەبیرکردن، بەکارهێنان دوای ئازادکردنەوە، دوو جار ئازادکردنەوە.CêIT

Garbage collection: memory freed for youفڕێدانی بەجێماوەکان: بیرگە بۆت ئازاد دەکرێتەوە

Most languages today free heap memory for you. A part of the language's run-time system, the garbage collector, finds the objects that have become garbage and frees them. John McCarthy invented garbage collection around 1959 for the language Lisp. Collectors use two main ideas, often together:

زۆربەی زمانەکانی ئەمڕۆ بۆت ئازاد دەکەنەوە. بەشێک لە سیستەمی کارکردنی زمانەکە، واتە سیستەمی ، ئەو دەدۆزێتەوە کە بوونەتە بەجێماو و ئازادیان دەکاتەوە. John McCarthy نزیکەی ساڵی ⁦1959⁩ بۆ زمانی Lisp داهێنا. ئەم سیستەمانە دوو بیرۆکەی سەرەکی بەکار دەهێنن، زۆرجار پێکەوە:

  • Reference counting: every object counts the references to it and is freed when the count reaches 0.
  • Tracing: from time to time, follow the references from the roots, keep everything you reach and free the rest. The basic tracing method is mark and sweep.
  • : هەر خۆی دەژمێرێت و کاتێک ژمارەکە دەگاتە 0 ئازاد دەکرێتەوە.
  • شوێنکەوتن: جار جار لە شوێن بکەوە، هەموو ئەوەی پێی دەگەیت بیهێڵەرەوە و ئەوانی تر ئازاد بکەرەوە. ڕێگای بنەڕەتیی شوێنکەوتن ـە.
A tree titled freeing heap memory with three branches. By hand: C with malloc and free. By rules the compiler checks: Rust ownership and C++ RAII, which free memory at a known point. Automatically, by garbage collection, which splits into reference counting, used by Python, PHP and Swift, and tracing such as mark and sweep, used by Java, JavaScript and Dart. Python and PHP add a cycle collector to their reference counting.دارێک بە ناونیشانی ئازادکردنەوەی هیپی بیرگە بە سێ لق. بە دەست: C بە malloc و free. بە یاسا کە کۆمپایلەرەکە دەیانپشکنێت: خاوەندارێتیی Rust و RAIIـی C++، کە بیرگە لە خاڵێکی زانراودا ئازاد دەکەنەوە. بە شێوەی خۆکار، بە فڕێدانی بەجێماوەکان، کە دابەش دەبێت بۆ ژماردنی ئاماژەکان، کە Python، PHP و Swift بەکاری دەهێنن و شوێنکەوتن وەک نیشانەکردن و ماڵین، کە Java، JavaScript و Dart بەکاری دەهێنن. Python و PHP سیستەمێکی کۆکردنەوەی بازنەکانیش زیاد دەکەن.
Three ways to give heap memory back.سێ ڕێگا بۆ گەڕاندنەوەی .CêIT

You still decide what to keep: a collector frees only the objects that nobody can reach.

هێشتا تۆ بڕیار دەدەیت چی بمێنێتەوە: سیستەمی تەنیا ئەو ئازاد دەکاتەوە کە کەس پێیان ناگات.

Reference countingژماردنی ئاماژەکان

With reference counting, every object on the heap carries a number: how many references point to it. A new reference adds 1; when a reference is changed or disappears, 1 is taken away. When the count reaches 0, nobody can reach the object, so it is freed at once.

لە هەر لە ژمارەیەک هەڵدەگرێت: چەند بۆی هەیە. نوێ 1 زیاد دەکات؛ کاتێک دەگۆڕێت یان نامێنێت، 1 کەم دەکرێتەوە. کاتێک ژمارەکە دەگاتە 0، کەس ناتوانێت بە بگات، بۆیە دەستبەجێ ئازاد دەکرێتەوە.

Counting referencesژماردنی ئاماژەکانTEXT
a ← NEW LIST        // the list's count: 1
b ← a               // 2
a ← NULL            // 1
b ← NULL            // 0: freed now
Four steps in a row, each with variables a and b and one list with a count badge. After a ← NEW LIST, a points to the list and the count is 1. After b ← a, both point to it and the count is 2. After a ← NULL, only b points to it and the count is 1. After b ← NULL, nothing points to it, the count is 0 and the list is freed at once.چوار هەنگاو لە ڕیزێکدا، هەر یەکە بە گۆڕاوەکانی a و b و لیستێک کە نیشانەی ژمارەی هەیە. دوای a ← NEW LIST، a ئاماژە بە لیستەکە دەکات و ژمارەکە 1ـە. دوای b ← a، هەردووکیان ئاماژەی پێ دەکەن و ژمارەکە 2ـە. دوای a ← NULL، تەنیا b ئاماژەی پێ دەکات و ژمارەکە 1ـە. دوای b ← NULL، هیچ شتێک ئاماژەی پێ ناکات، ژمارەکە 0ـە و لیستەکە دەستبەجێ ئازاد دەکرێتەوە.
The count goes up and down with the references; at 0 the list is freed.ژمارەکە لەگەڵ سەر و خوار دەبێت؛ لە 0ـدا لیستەکە ئازاد دەکرێتەوە.CêIT

The good side: memory comes back at once and evenly, without long pauses. The cost: every assignment must update counts. The standard Python (CPython), PHP and Swift all use reference counting.

لایەنی باش: دەستبەجێ و بە ڕێکی دەگەڕێتەوە، بێ وەستانی درێژ. تێچووەکەی: هەموو دانانێک دەبێت ژمارەکان نوێ بکاتەوە. Pythonـی ستاندارد ⁦(CPython)⁩، PHP و Swift هەموویان بەکار دەهێنن.

Reference countingژماردنی ئاماژەکان

The weak spot: reference cyclesلاوازییەکەی: بازنەکانی ئاماژە

Two objects can refer to each other: a person object keeps its flat, and the flat object keeps its tenant. When the program drops both variables, each object still has one reference, from the other one. The counts stay at 1 forever, so pure reference counting never frees them: a reference cycle leaks.

دوو دەتوانن ئاماژە بە یەکتر بکەن: کەسێک خانووەکەی ڕادەگرێت و خانووەکەش کرێچییەکەی. کاتێک هەردوو وازلێدێنێت، هەر هێشتا یەک هەیە، لە ئەویترەوە. ژمارەکان بۆ هەمیشە لە 1ـدا دەمێننەوە، بۆیە بە تەنیا هەرگیز ئازادیان ناکاتەوە: دەبێتە .

Two objects in a ringدوو ئۆبجێکت لە بازنەیەکداTEXT
x ← NEW PERSON
y ← NEW FLAT
x.home ← y          // the flat's count: 2
y.tenant ← x        // the person's count: 2
x ← NULL            // person: 1
y ← NULL            // flat: 1, nobody can reach them, but no count is 0
A person object and a flat object, joined by two arrows: the person's home leads to the flat and the flat's tenant leads to the person. Each object shows a count of 1. On the left, the variables x and y are crossed out and hold NULL, so no arrow comes in from outside. The words below say: nobody can reach them, but neither count is 0.ئۆبجێکتێکی کەس و ئۆبجێکتێکی خانوو کە بە دوو تیر بەیەکەوە بەستراون: homeـی کەسەکە دەچێتە سەر خانووەکە و tenantـی خانووەکە دەچێتە سەر کەسەکە. هەر ئۆبجێکتێک ژمارەی 1 پیشان دەدات. لە چەپ، گۆڕاوەکانی x و y هێڵیان بەسەردا هاتووە و NULL هەڵدەگرن، بۆیە هیچ تیرێک لە دەرەوە نایەت. نووسینی خوارەوە دەڵێت: کەس پێیان ناگات، بەڵام هیچ ژمارەیەک 0 نییە.
A reference cycle: unreachable, yet each count is 1.: کەس پێی ناگات، بەڵام هەر ژمارەیەک 1ـە.CêIT

Languages fix this in two ways. Python and PHP add a cycle collector that looks for groups of objects reachable only from each other and frees them. Swift asks the programmer to mark one side as a weak reference (weak), which does not count, so the cycle never forms.

زمانەکان ئەمە بە دوو ڕێگا چارەسەر دەکەن. Python و PHP سیستەمێکی کۆکردنەوەی بازنەکان زیاد دەکەن کە بە دوای ئەو کۆمەڵە دەگەڕێت کە تەنیا لە یەکترەوە دەگەیەنرێن و ئازادیان دەکاتەوە. Swift داوا لە بەرنامەساز دەکات کە لایەکیان وەک (weak) نیشانە بکات، کە ناژمێردرێت، بۆیە بازنەکە هەرگیز دروست نابێت.

Tracing: mark and sweepشوێنکەوتن: نیشانەکردن و ماڵین

A tracing collector does not count. From time to time it works in two phases. Mark: start at the roots, follow every reference and mark each object you reach. Sweep: walk through the whole heap and free every object without a mark, then clear the marks for next time. A ring of objects that no root reaches is never marked, so cycles are no problem.

سیستەمێکی شوێنکەوتن ناژمێرێت. جار جار بە دوو قۆناغ کار دەکات. نیشانەکردن: لە دەست پێ بکە، شوێن هەموو بکەوە و هەر کە پێی دەگەیت نیشانە بکە. ماڵین: بە هەموو بڕۆ و هەر کە نیشانەی نییە ئازاد بکەرەوە، پاشان نیشانەکان بۆ جاری داهاتوو بسڕەوە. بازنەیەک لە کە هیچ پێی ناگات هەرگیز نیشانە ناکرێت، بۆیە بازنەکان کێشە نین.

Animation in six steps. 1, the heap holds objects A to G; two roots, a variable in a stack frame and a global variable, have arrows into it. 2, mark: from the first root, A is marked, then B through A's reference. 3, from the second root, C is marked, then F through C. 4, marking is over: A, B, C and F are marked; D and E point only to each other and G points to A, but nothing reaches D, E or G. 5, sweep: D, E and G are freed and their places become free space. 6, the marks are cleared; the heap holds A, B, C and F and the free space is ready for new objects.
Animation in six steps. 1, the heap holds objects A to G; two roots, a variable in a stack frame and a global variable, have arrows into it. 2, mark: from the first root, A is marked, then B through A's reference. 3, from the second root, C is marked, then F through C. 4, marking is over: A, B, C and F are marked; D and E point only to each other and G points to A, but nothing reaches D, E or G. 5, sweep: D, E and G are freed and their places become free space. 6, the marks are cleared; the heap holds A, B, C and F and the free space is ready for new objects.
Mark everything reachable, then sweep the rest.هەموو ئەوەی دەگەیەنرێت نیشانە بکە، پاشان ئەوانی تر بماڵە.CêIT

The cost: a simple collector stops the program while it works, a pause. Modern collectors in Java, JavaScript and Dart do much of the work in small steps or on other processor cores while the program runs, so pauses are short.

تێچووەکەی: سیستەمێکی سادە لە کاتی کارکردنیدا دەوەستێنێت، واتە وەستانێک. سیستەمە نوێیەکانی Java، JavaScript و Dart زۆربەی کارەکە بە هەنگاوی بچووک یان لەسەر ناوکەکانی تری دەکەن لە کاتێکدا کار دەکات، بۆیە وەستانەکان کورتن.

Generations: most objects die youngنەوەکان: زۆربەی ئۆبجێکتەکان بە گەنجی دەمرن

Collector designers noticed a pattern: most objects are needed only for a moment, such as a temporary piece of text or the list for one calculation, while a few live for the whole run. A generational collector therefore splits the heap into a young area, where new objects are made, and an old area. It cleans the young area often, which is quick because most of it is already garbage. Objects that survive a few cleanings move up to the old area, which is cleaned only now and then.

دیزاینەرانی ئەم سیستەمانە شتێکیان تێبینی کرد: زۆربەی تەنیا بۆ ساتێک پێویستن، وەک پارچە دەقێکی کاتی یان لیستی یەک حیسابکردن، بەڵام کەمێکیان بە درێژاییی کارکردنەکە دەمێننەوە. بۆیە دابەش دەکات بۆ ناوچەیەکی گەنج، کە نوێی تێدا دروست دەکرێت و ناوچەیەکی پیر. ناوچە گەنجەکە زوو زوو پاک دەکاتەوە، کە خێرایە چونکە زۆربەی ئەوەی تێیدایە پێشتر بووە بە بەجێماو. ئەو لە چەند پاککردنەوەیەک ڕزگاریان دەبێت دەچنە ناوچە پیرەکە، کە تەنیا جار جار پاک دەکرێتەوە.

Two boxes. Left, the young area: many small new objects, most of them crossed out as garbage, labelled cleaned often and fast. An arrow labelled survivors move up leads to the right box, the old area: a few long-lived objects such as the loaded map and the settings, labelled cleaned rarely.دوو سندوق. چەپ، ناوچە گەنجەکە: زۆر ئۆبجێکتی نوێی بچووک کە زۆربەیان وەک بەجێماو هێڵیان بەسەردا هاتووە، لەگەڵ تێبینیی زوو زوو و بە خێرایی پاک دەکرێتەوە. تیرێک بە ناوی ڕزگاربووان دەچنە سەرەوە دەچێتە سندوقی ڕاست، ناوچە پیرەکە: چەند ئۆبجێکتێکی تەمەندرێژ وەک نەخشە بارکراوەکە و ڕێکخستنەکان، لەگەڵ تێبینیی بە دەگمەن پاک دەکرێتەوە.
Clean the young area often; move survivors to the old area.ناوچە گەنجەکە زوو زوو پاک بکەرەوە؛ ڕزگاربووان بگوازەرەوە بۆ ناوچە پیرەکە.CêIT

Java's collectors, JavaScript engines such as V8 (in Chrome and Node.js) and Dart all work by generations. CPython's cycle collector sorts objects into three generations too.

سیستەمەکانی Java، بزوێنەرەکانی JavaScript وەک ⁦V8⁩ (لە Chrome و ⁦Node.js⁩ـدا) و Dart هەموویان بەپێی نەوە کار دەکەن. سیستەمی کۆکردنەوەی بازنەکانی CPythonـیش دابەش دەکات بۆ سێ نەوە.

How popular languages do it (2026)زمانە باوەکان چۆن دەیکەن ⁦(2026)⁩

Every language makes its own choice. The table shows the main approach of each one as of November 2026, checked in its official documentation.

هەر زمانێک هەڵبژاردنی خۆی هەیە. خشتەکە ڕێگای سەرەکیی هەر یەکێکیان پیشان دەدات، وەک لە تشرینی دووەمی ⁦2026⁩دا لە بەڵگەنامە فەرمییەکانیاندا پشکنراوە.

Freeing heap memory in nine languages, November 2026ئازادکردنەوەی لە نۆ زماندا، تشرینی دووەمی ⁦2026⁩
LanguageزمانHow heap memory is freed چۆن ئازاد دەکرێتەوەGood to knowباشە بیزانیت
Python (CPython)Reference counting and a cycle collector و سیستەمی کۆکردنەوەی بازنەکانMost objects are freed as soon as their count reaches 0; the cycle collector sorts container objects into three generations.زۆربەی هەر کە ژمارەکەیان گەیشتە 0 ئازاد دەکرێنەوە؛ سیستەمی کۆکردنەوەی بازنەکان هەڵگر دابەش دەکات بۆ سێ نەوە.
Java (HotSpot JVM)Tracing, generational collectorsشوێنکەوتن، G1 is the default. In JDK 25 and 26 a machine with one processor or less than 1792 MB of memory gets the simple Serial collector instead; from JDK 27 (September 2026) G1 is the default everywhere. ZGC keeps pauses very short.⁦G1⁩ بنەڕەتییە. لە ⁦JDK 25⁩ و ⁦26⁩ـدا ئامێرێک کە یەک پرۆسێسەر یان کەمتر لە ⁦1792 MB⁩ هەبێت لە جیاتی ئەوە سیستەمی سادەی Serial وەردەگرێت؛ لە ⁦JDK 27⁩ـەوە (ئەیلوولی ⁦2026) G1⁩ لە هەموو شوێنێک بنەڕەتییە. ZGC وەستانەکان زۆر کورت دەکاتەوە.
JavaScriptTracing (mark and sweep), generationalشوێنکەوتن ()، بەپێی نەوەNo modern engine uses reference counting. V8 (Chrome, Node.js) copies surviving young objects and marks the old area in the background.هیچ بزوێنەرێکی نوێ بەکار ناهێنێت. ⁦V8 (Chrome⁩، ⁦Node.js⁩) گەنجە ڕزگاربووەکان کۆپی دەکات و ناوچە پیرەکە لە پشتەوە نیشانە دەکات.
Dart (Flutter)Tracing, generationalشوێنکەوتن، بەپێی نەوەTwo generations: a fast copying collector for the young one, concurrent mark-sweep or mark-compact for the old one (why Flutter uses Dart).دوو نەوە: سیستەمێکی خێرای کۆپیکردن بۆ نەوە گەنجەکە و یان نیشانەکردن و پەستاندن بە هاوکاتی بۆ نەوە پیرەکە (بۆچی Flutter، Dart بەکار دەهێنێت).
PHPReference counting and a cycle collector و سیستەمی کۆکردنەوەی بازنەکانSince PHP 5.3. The cycle collector is on by default and runs when 10,000 possible cycle roots have been recorded.لە ⁦PHP 5.3⁩ـەوە. سیستەمی کۆکردنەوەی بازنەکان بە شێوەی بنەڕەتی چالاکە و کاتێک ⁦10,000⁩ ئەگەریی بازنە تۆمار دەکرێن کار دەکات.
SwiftAutomatic reference counting (ARC) خۆکار ⁦(ARC)⁩No cycle collector: the programmer breaks cycles with weak or unowned references.سیستەمی کۆکردنەوەی بازنەکانی نییە: بەرنامەساز بازنەکان بە weak یان unowned دەشکێنێت.
CBy hand: malloc and freeبە دەست: malloc و freeNothing is freed automatically; a mistake is a leak, a dangling pointer or a double free.هیچ شتێک بە شێوەی خۆکار ئازاد ناکرێتەوە؛ هەڵەیەک دەبێتە ، یان .
C++By hand, but mostly through RAIIبە دەست، بەڵام زۆرتر لە ڕێگەی RAIIـەوەWhen an object goes out of scope, its clean-up code frees what it owns; std::unique_ptr and std::shared_ptr (which counts references) do this for heap memory.کاتێک لە دەردەچێت، کۆدی پاککردنەوەکەی ئەوەی هی خۆیەتی ئازادی دەکاتەوە؛ std::unique_ptr و std::shared_ptr (کە دەژمێرێت) ئەمە بۆ دەکەن.
RustOwnership checked by the compiler; no garbage collectorخاوەندارێتی کە دەیپشکنێت؛ بێ Each value has one owner; when the owner goes out of scope, the value is dropped (freed). A double free is stopped before the program runs.هەر نرخێک یەک خاوەنی هەیە؛ کاتێک خاوەنەکە لە دەردەچێت، نرخەکە فڕێ دەدرێت (ئازاد دەکرێتەوە). پێش کارکردنی ڕێگری لێ دەکرێت.

Leaks that still happen with a garbage collectorدزەکردنی بیرگە لەگەڵ فڕێدانی بەجێماوەکانیشدا

A collector frees only what nobody can reach. If your program still holds a reference to an object it will never use again, that object is not garbage. It is a leak that the collector cannot see. Typical causes:

سیستەمی تەنیا ئەوە ئازاد دەکاتەوە کە کەس پێی ناگات. ئەگەر هێشتا بۆ هەڵبگرێت کە هەرگیز دووپاتە بەکاری ناهێنێتەوە، ئەو بەجێماو نییە. کە سیستەمەکە نایبینێت. هۆکارە باوەکان:

  • A list or store that only grows, such as a global list of every message ever shown, or a store of saved results with no limit.
  • Listeners that are never removed: a screen registers a listener with a long-lived object and is then closed, but the listener still refers to the screen. Flutter widgets free such things in dispose().
  • Old data in long-lived objects: a field of an object that lives for the whole run still points to data from long ago.
  • لیستێک یان کۆگایەک کە تەنیا گەورە دەبێت، وەک لیستێکی گشتیی هەموو ئەو پەیامانەی تا ئێستا پیشان دراون، یان کۆگایەکی ئەنجامە هەڵگیراوەکان بێ سنوور.
  • ئەو هەرگیز لاناچن: شاشەیەک لای تەمەندرێژ تۆمار دەکات و پاشان دادەخرێت، بەڵام هێشتا ئاماژە بە شاشەکە دەکات. Flutter ئەم شتانە لە dispose()ـدا ئازاد دەکەنەوە.
  • کۆن لە تەمەندرێژدا: کە بە درێژاییی کارکردنەکە دەژی هێشتا ئاماژە بە زۆر کۆن دەکات.
A root, a global variable called history, points to a long list. The list points to many old screen objects. The newest one is in use; the older ones are greyed out and labelled: never used again, but still reachable, so the collector keeps them. The memory bar beside them keeps growing.ڕەگێک، گۆڕاوێکی گشتی بە ناوی history، ئاماژە بە لیستێکی درێژ دەکات. لیستەکە ئاماژە بە زۆر ئۆبجێکتی شاشەی کۆن دەکات. نوێترینیان بەکار دێت؛ کۆنەکان کاڵ کراونەتەوە لەگەڵ تێبینیی: هەرگیز دووپاتە بەکار نایەنەوە، بەڵام هێشتا دەگەیەنرێن، بۆیە سیستەمەکە دەیانهێڵێتەوە. ستوونی بیرگە لە تەنیشتیانەوە هەر گەورە دەبێت.
Useless but reachable: a leak the collector cannot see.بێسوود بەڵام گەیشتنی پێ دەکرێت: کە سیستەمەکە نایبینێت.CêIT

Leaks that still happen with a garbage collectorدزەکردنی بیرگە لەگەڵ فڕێدانی بەجێماوەکانیشدا

Good habitsخووە باشەکان

  • Keep variables in the smallest scope: a local variable lets its object go when the call ends.
  • Remove listeners and close files and connections when you are done (close every file).
  • Give stores of saved results and histories a limit, or use weak references where the language offers them.
  • Set a long-lived reference to null when you no longer need what it leads to.
  • Do not call the collector by hand to “fix” memory; find what still holds the references. Memory tools, such as the Memory view of Flutter DevTools, show which path keeps an object alive.
  • Without a collector (C): free every allocation exactly once, then set the pointer to NULL.
  • لە بچووکترین ڕابگرە: کاتێک بانگکردنەکە تەواو دەبێت بەر دەدات.
  • کاتێک تەواو بوویت لاببە و و پەیوەندییەکان دابخە (هەموو فایلێک دابخە).
  • سنوورێک بۆ کۆگای ئەنجامە هەڵگیراوەکان و مێژووەکان دابنێ، یان لەو شوێنانەی زمانەکە دەیدات بەکار بهێنە.
  • تەمەندرێژ بکە بە کاتێک ئیتر پێویستت بەوە نییە کە ئاماژەی پێ دەکات.
  • سیستەمی بە دەست بانگ مەکە بۆ «چاککردنی» ؛ بیدۆزەرەوە چی هێشتا ڕادەگرێت. ئامرازەکانی ، وەک بەشی Memoryـی Flutter DevTools، پیشان دەدەن کام ڕێگا بە زیندوویی دەهێڵێتەوە.
  • بێ سیستەمی ⁦(C)⁩: هەر ڕێک یەک جار ئازاد بکەرەوە، پاشان بکە بە NULL.

Common mistakesهەڵە باوەکان

MistakeهەڵەWhat is trueڕاستییەکە
“A garbage collector means my program cannot leak memory.”«سیستەمی واتە ناتوانێت هەبێت.»It frees only unreachable objects; references you forget to drop still leak.تەنیا نەگەیەنراو ئازاد دەکاتەوە؛ ئەو لەبیرت دەچێت لایانبەیت هێشتا دەبنە .
“The stack and the heap are different chips.”« و دوو پارچەی جیاوازن.»Both are areas of the same RAM; the program uses them in different ways.هەردووکیان ناوچەی هەمان ؛ بە دوو شێوەی جیاواز بەکاریان دەهێنێت.
“The memory heap is the heap data structure.”« و یەک شتن.»Same English word, different ideas: the memory heap is an area of memory; the heap data structure is a tree that keeps the smallest item on top.هەمان وشەی ئینگلیزی، دوو بیرۆکەی جیاواز: ناوچەیەکی ؛ هیپ دارێکە کە بچووکترین دانە لە سەرەوە دەهێڵێتەوە.
“FREE(p) also empties p.”«FREE(p) pـیش بەتاڵ دەکاتەوە.»p still holds the old address; set it to NULL yourself.p هێشتا ناونیشانە کۆنەکە هەڵدەگرێت؛ خۆت بیکە بە NULL.
“An object made inside a function is lost when the function returns.”« کە لە ناو دروست دەکرێت لەگەڵ گەڕانەوەی لەدەست دەچێت.»Only the frame is removed. The object on the heap lives on while something refers to it.تەنیا چوارچێوەکە لادەبرێت. لە تا شتێک ئاماژەی پێ بکات دەژی.
“Calling the collector by hand often makes a program faster.”«بانگکردنی سیستەمی بە دەست زۆرجار خێراتر دەکات.»Usually it makes it slower: collectors choose good moments themselves.زۆرجار خاوتری دەکات: ئەم سیستەمانە خۆیان کاتی باش هەڵدەبژێرن.
“Reference counting frees everything.”« هەموو شتێک ئازاد دەکاتەوە.»Reference cycles need a cycle collector or weak references. پێویستیان بە سیستەمی کۆکردنەوەی بازنەکان یان هەیە.

Exercisesڕاهێنانەکان

ExerciseڕاهێنانThe bakery's call stackستاکی بانگکردنی نانەواخانەکە

Use the bakery program from The call stack. (a) How many frames are on the call stack while line 2 runs, and what does each hold? (b) Which variables still exist when line 17 runs?

نانەواخانەکە لە ستاکی بانگکردن بەکار بهێنە. ⁦(a)⁩ لە کاتێکدا دێڕی 2 کار دەکات چەند چوارچێوە لەسەر و هەر یەکەیان چی هەڵدەگرێت؟ ⁦(b)⁩ کاتێک دێڕی ⁦17⁩ کار دەکات کام هێشتا ماون؟

Show answerوەڵامەکە پیشان بدە

(a) Three frames. Top: discount with total = 3000 and, once line 2 has run, off = 300. Middle: bill with count = 12, price = 250, total = 3000, waiting at line 9. Bottom: the main part with loaves = 12, waiting at line 16 (toPay has no value yet). (b) Only the main frame is left: loaves = 12 and toPay = 2700. The frames of discount and bill were removed when they returned.

⁦(a)⁩ سێ چوارچێوە. سەرەوە: discount بە total = ⁦3000⁩ و دوای جێبەجێبوونی دێڕی 2، off = ⁦300⁩. ناوەڕاست: bill بە count = ⁦12⁩، price = ⁦250⁩، total = ⁦3000⁩، کە لە دێڕی 9دا چاوەڕێ دەکات. خوارەوە: بەشی سەرەکی بە loaves = ⁦12⁩، کە لە دێڕی ⁦16⁩دا چاوەڕێ دەکات (toPay هێشتا نرخی نییە). ⁦(b)⁩ تەنیا چوارچێوەی بەشی سەرەکی ماوە: loaves = ⁦12⁩ و toPay = ⁦2700⁩. چوارچێوەکانی discount و bill کاتی گەڕانەوەیان لابران.

ExerciseڕاهێنانCount the referencesئاماژەکان بژمێرە

Write the count of each list after every line. When is a list freed?

ژمارەی هەر لیستێک دوای هەر دێڕێک بنووسە. کەی لیستێک ئازاد دەکرێتەوە؟

TEXT
a ← NEW LIST        // list 1
b ← NEW LIST        // list 2
c ← a
a ← b
c ← NULL
Show answerوەڵامەکە پیشان بدە
After lineدوای دێڕیList 1لیستی 1List 2لیستی 2
11 (a)—
21 (a)1 (b)
32 (a, c)1 (b)
41 (c)2 (a, b)
50: freed0: ئازاد کرایەوە2 (a, b)

List 1 is freed at line 5, at the moment c lets go of it. List 2 is still alive at the end, with two references.

لیستی 1 لە دێڕی 5دا ئازاد دەکرێتەوە، هەر ئەو ساتەی c بەری دەدات. لیستی 2 لە کۆتاییدا هێشتا زیندووە، بە دوو .

ExerciseڕاهێنانMark and sweep by handنیشانەکردن و ماڵین بە دەست

The roots are r1 and r2. r1 → A, A → B, r2 → C, D → E, E → D, F → A. Which objects are marked, and which are swept? What would reference counting do with D, E and F?

⁦r1⁩ و ⁦r2⁩ن. ⁦r1⁩ ئاماژە بە A دەکات، A بە B، ⁦r2⁩ بە C، D بە E، E بە D و F بە A. کام نیشانە دەکرێن و کامیان دەماڵرێن؟ چی لە D، E و F دەکات؟

Hintئاماژە

Follow arrows only in their direction, starting at r1 and r2.

تەنیا بە ئاراستەی تیرەکان بڕۆ، لە ⁦r1⁩ و ⁦r2⁩ـەوە دەست پێ بکە.

Show answerوەڵامەکە پیشان بدە

Marked: A (from r1), B (through A), C (from r2). Swept: D and E, which point only to each other, and F. F points to A, but nothing points to F, and marking follows arrows only away from the roots. A reference-counting system would free F (its count is 0) but never D and E (each has a count of 1).

نیشانەکراو: A (لە ⁦r1⁩ـەوە)، B (لە ڕێگەی Aـەوە)، C (لە ⁦r2⁩ـەوە). ماڵراو: D و E، کە تەنیا ئاماژە بە یەکتر دەکەن و ⁦F. F⁩ ئاماژە بە A دەکات، بەڵام هیچ شتێک ئاماژە بە F ناکات و نیشانەکردن تەنیا بە ئاراستەی دوورکەوتنەوە لە شوێن تیرەکان دەکەوێت. سیستەمێکی F ئازاد دەکاتەوە (ژمارەکەی 0ـە)، بەڵام هەرگیز D و E نا (هەر یەکەیان ژمارەی 1ـی هەیە).

ExerciseڕاهێنانFind the bugباگەکە بدۆزەرەوە

This program runs on a language without a garbage collector. What is wrong, how much memory is lost, and how do you fix it?

ئەم لەسەر زمانێک کار دەکات کە سیستەمی نییە. چی هەڵەیە، چەند لەدەست دەچێت و چۆن چاکی دەکەیتەوە؟

TEXT
FUNCTION makeGreeting()
    p ← ALLOCATE(50)
    WRITE "Hello" AT p
    RETURN p
END FUNCTION

START
REPEAT 100 TIMES
    g ← makeGreeting()
    OUTPUT TEXT AT g
END REPEAT
END
Show answerوەڵامەکە پیشان بدە

A memory leak: every call to makeGreeting allocates 50 bytes that are never freed, so the loop loses 100 × 50 = 5000 bytes. Free the greeting once it has been shown, and do not use g after that:

: هەر بانگکردنێکی makeGreeting ⁦50⁩ تەرخان دەکات کە هەرگیز ئازاد ناکرێتەوە، بۆیە ⁦100 × 50 = 5000⁩ لەدەست دەدات. دوای پیشاندانی سڵاوەکە ئازادی بکەرەوە و دوای ئەوە g بەکار مەهێنە:

TEXT
REPEAT 100 TIMES
    g ← makeGreeting()
    OUTPUT TEXT AT g
    FREE(g)
    g ← NULL
END REPEAT
ExerciseڕاهێنانA leak in a weather appدزەکردنی بیرگە لە ئەپێکی کەشوهەوادا

Shilan's weather app adds a listener to the location service every time the forecast screen opens and never removes it. After the screen has been opened 200 times, the app uses a lot of memory, although it has a garbage collector. Explain why, and fix it.

ئەپی کەشوهەوای Shilan هەر جارێک شاشەی پێشبینی دەکرێتەوە لای خزمەتگوزاریی شوێن زیاد دەکات و هەرگیز لای نابات. دوای ئەوەی شاشەکە ⁦200⁩ جار کرایەوە، ئەپەکە زۆر بەکار دەهێنێت، هەرچەندە سیستەمی هەیە. ڕوونی بکەرەوە بۆچی و چاکی بکەرەوە.

Show answerوەڵامەکە پیشان بدە

The location service lives for the whole run. Every opening adds one more listener, and each listener still refers to its old, closed screen, so all 200 screens stay reachable from a root: the collector may not free them. Fix: remove the listener when the screen closes (in Flutter, in dispose()), or add it only once.

خزمەتگوزاریی شوێن بە درێژاییی کارکردنەکە دەژی. هەر کردنەوەیەک تر زیاد دەکات و هەر هێشتا ئاماژە بە شاشە کۆن و داخراوەکەی دەکات، بۆیە هەموو ⁦200⁩ شاشەکە لە دەگەیەنرێن: سیستەمەکە بۆی نییە ئازادیان بکاتەوە. چارەسەر: کاتێک شاشەکە دادەخرێت لاببە (لە Flutterـدا لە dispose()ـدا)، یان تەنیا یەک جار زیادی بکە.

Summaryپوختە

  • A running program keeps its data in RAM, mainly in two areas: the call stack and the heap.
  • Every call puts a frame with parameters, local variables and the return place on the stack; the return takes it off. Fast, automatic, small.
  • The heap keeps objects that must outlive a call; variables hold references (or, in C and C++, pointers) to them.
  • An object that no root can reach is garbage.
  • By hand (C): every ALLOCATE needs exactly one FREE; mistakes are leaks, dangling pointers and double frees.
  • Reference counting frees at count 0 but needs help with cycles; mark and sweep keeps what the roots reach; generational collectors clean the young area often.
  • A collector cannot free what you still refer to: drop references, remove listeners, limit stores.
  • کارا لە هەڵدەگرێت، بە تایبەتی لە دوو ناوچەدا: و .
  • هەر بانگکردنێک چوارچێوەیەک بە ، و شوێنی گەڕانەوەوە دەخاتە سەر ؛ گەڕانەوە لای دەبات. خێرا، خۆکار، بچووک.
  • ئەو هەڵدەگرێت کە دەبێت لە بانگکردنێک زیاتر بمێنن؛ (یان لە C و ⁦C++⁩ـدا ) بۆیان هەڵدەگرن.
  • کە هیچ پێی ناگات بەجێماوە.
  • بە دەست ⁦(C)⁩: هەر ALLOCATE پێویستی بە ڕێک یەک FREE هەیە؛ هەڵەکان ، و ن.
  • لە ژمارە 0ـدا ئازاد دەکاتەوە بەڵام بۆ یارمەتیی پێویستە؛ ئەوە دەهێڵێتەوە کە پێی دەگەن؛ ناوچە گەنجەکە زوو زوو پاک دەکاتەوە.
  • سیستەمی ناتوانێت ئەوە ئازاد بکاتەوە کە هێشتا ئاماژەی پێ دەکەیت: لاببە، لاببە، کۆگاکان سنووردار بکە.
Review questionsپرسیارەکانی پێداچوونەوە
Why can a frame be freed without a garbage collector?بۆچی چوارچێوەیەک بێ سیستەمی فڕێدانی بەجێماوەکان ئازاد دەکرێتەوە؟

Its life ends exactly when its call returns, so the program simply removes the top frame. Heap objects have no such fixed end.

تەمەنی ڕێک ئەو کاتە کۆتایی دێت کە بانگکردنەکەی دەگەڕێتەوە، بۆیە بە سادەیی چوارچێوەی سەرەوە لادەبات. کۆتاییەکی جێگیری وایان نییە.

What is the difference between a reference and a pointer?جیاوازیی نێوان ئاماژە و ئاماژەدەر چییە؟

Both lead to an object. A pointer is the memory address itself, which C and C++ let you calculate with; a reference hides the address, and the language checks how you use it.

هەردووکیان دەتبەنە لای . خودی کە C و ⁦C++⁩ ڕێگەت دەدەن حیسابی پێ بکەیت؛ ناونیشانەکە دەشارێتەوە و زمانەکە چاودێریی ئەوە دەکات چۆن بەکاری دەهێنیت.

Which handles cycles: reference counting or mark and sweep?کامیان بازنەکان چارەسەر دەکات: ژماردنی ئاماژەکان یان نیشانەکردن و ماڵین؟

Mark and sweep: a ring that no root reaches is never marked. Reference counting needs a cycle collector or weak references.

: بازنەیەک کە هیچ پێی ناگات هەرگیز نیشانە ناکرێت. پێویستی بە سیستەمی کۆکردنەوەی بازنەکان یان هەیە.

Next: plan programs step by step in Algorithms, Pseudocode and Flowcharts.

دواتر: هەنگاو بە هەنگاو پلان بکە لە ئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Algorithms, Pseudocode and Flowchartsئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.