[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: 1765161220656809.mp4 (3.39 MB, 1920x1080)
3.39 MB
3.39 MB MP4
How do garbage collected languages even work? Like if you dont tell it to allocate memory and free it how the fuck does it know to use memory? Also, are these so called "memory safe" langs actually "memory safe"? How do I know that the garbage collector actually works? Its asking quite a lot of me to assume that every single potential use case of memory possible is covered and accounted for both at compile time and run time. If i sound retarded then feel free to correct me, im not a C elitist or anything like that but the concept of trusting a garbage collector to just work and make memory leaks never happen is just crazy to me, it gives me the same uneasy feeling I get when someone tells me to "just have faith in god"
>>
> from roots, walk every reference
> copy all reachable data to a new place
> fix references
> nuke old place
>>
>>107595201
God is going to collect you and >>>/trash/
>>
>>107595201
every object keeps a reference to another object it uses. An array for example might contain 128 objects, making it reference each.
The array itself can in turn also be referenced, for example via a local reference in the currently executed function.
Once the reference count of an object reaches 0, it is freed and all objects it referenced get dereferenced, allowing them to be freed too.
Now if you have very complex reference chains, an object can have a high reference count, but it is only freed if nothing has a reference to it anymore, preventing any use after free bugs (mostly).
However you can't just actually free() the memory of the object once the reference count reached 0, since that would fragment the heap a lot, which is very bad for performance and memory usage. A better way is to regularly schedule a garbage collection, which pauses the whole program, frees all objects with a reference count 0 all at once, so that you get nice big freed sequential heap blocks again.
>>
The GC first makes a list of everything that sparks joy
Then the GC holds each object in its hands
If the object is on the list of things that sparks joy, it is safe
Otherwise it goes in the trash
>>
>>107595263
more like
>maintain list of allocations
>from roots, walk every reference
>deallocate any list element not in the tree
but it's more complicated because of the whole generational thing. Allocation and GC in Java and C# are highly optimized for large numbers of short-lived objects.
>>
File: 1612821201764.jpg (115 KB, 1080x1350)
115 KB
115 KB JPG
>>107595201
What's the point of these questions? Google and wikipedia exists.
>>
>>107595457
I trust randoms on 4chan more than i trust google or wikipedia.
>>
>>107595457
usecase for search engines?
>>
>>107595201
Yes you can have memory leak in a GC application. It is easy to diagnose and fix though.

Any language in 2025 without GC is just tranny.
>>
>>107595201
GC languages don't do away with memory allocation. It's the deallocation part that's algorithmically determined. Every single object has an interface to mark whatever it can reach.
This is compared against everything ever allocated and the missing links can be removed.
>>
>>107595201
There's actually many different algorithms, see this: https://spin.atomicobject.com/visualizing-garbage-collection-algorithms/



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.