memory management - What is garbage collection and how does it work? -


forgetting languages support this, garbage collection about? how implemented in programming languages? how works , why it's beneficial? why languages support (and don't support) it?

have ever used c programming language? might notice that, unless know how memory need ahead of time, have malloc() , free() , release memory, respectively. wouldn't cool if language did you?

enter garbage collection. gc algorithm traverses through currently-used memory , gets rid of stuff that's not being used. easy 1 learn how works mark/sweep algorithm. marks objects being used, sweeps away stuff no longer referenced or in scope. that's beneficial because (the programmer) don't have worry memory handling! (by way, don't use mark/sweep; of 2016, java uses more nuanced gc1 algorithm. can see gc algorithms java has available here)

except still do. may program data structure technically can still access part of memory, though semantically it's not possible nor done in lifetime of code. that's not computed @ compile-time or run-time, bit of unused memory still hangs around. , might "whatever let gc handle it" or worse, i'm ignorant of gc , make objects wherever please. leads "memory leaks", garbage collection made prevent in first place!

as why languages don't support it, either developers believe it's waste of cpu cycles, or find memory leaks @ compile-time (somehow??), or no gc algorithms known when language created.


Comments