Збирання сміття: Автоматична чистка пам'яті

Процес автоматичного звільнення пам'яті від об'єктів, що не використовуються.

У мовах програмування високого рівня (Java, C#, Python, JS) розробнику не потрібно вручну видаляти об'єкти. Збирач сміття періодично сканує пам'ять і вивільняє місце. Це запобігає витокам пам'яті, хоча іноді може спричиняти короткі паузи в роботі програми ('Stop-the-world').

        graph LR
  Center["Збирання сміття: Автоматична чистка пам'яті"]:::main
  Rel_memory_management["memory-management"]:::related -.-> Center
  click Rel_memory_management "/terms/memory-management"
  Rel_cpu_cache["cpu-cache"]:::related -.-> Center
  click Rel_cpu_cache "/terms/cpu-cache"
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧒 Простими словами

Уявіть, що ви граєте іграшками. Коли ви кидаєте іграшку і більше нею не граєтеся, приходить робот-прибиральник і складає її в ящик, щоб у кімнаті було місце для нових ігор. Це і є збирання сміття.

🤓 Expert Deep Dive

Tracing GC (like Mark-Sweep) builds a graph of all reachable objects starting from 'roots' (stack, globals). Refence Counting (used in Python/Swift) immediately deletes objects when their count hits zero. Generational GC leverages the 'weak generational hypothesis': that most objects die young. It divides the heap into 'Eden', 'Survivor', and 'Tenured' spaces, collecting the younger spaces more frequently.

📚 Джерела