How Garbage Collection Works

  Рет қаралды 14,123

Tom Delalande

Tom Delalande

Күн бұрын

Пікірлер
@RazaSyed12345
@RazaSyed12345 3 ай бұрын
I have been bamboozled, I thought I was gonna learn how solid waste management companies are created and run, and paid for by local governments and gated communities
@ficolas2
@ficolas2 3 ай бұрын
Goblin mode on
@_modiX
@_modiX 3 ай бұрын
whats wrong?
@ficolas2
@ficolas2 3 ай бұрын
​@@_modiX Just an Easter egg, try to find it if you are bored hehe
@GavinBogie
@GavinBogie 3 ай бұрын
@@_modiX 3:50 read
@_modiX
@_modiX 3 ай бұрын
@@GavinBogie thank you, that's hilarious
@Hellbending
@Hellbending 2 ай бұрын
Spicy
@addcoding8150
@addcoding8150 4 ай бұрын
I think it heavily depends on discipline. There are C-code bases where even Valgrind and other analyzers can't protect you from leaking. But there are others where the lifetime model is thought through and you never have to worry about management. Game devs have memory management for games figured out at this point and found easy patterns for their use case. I wonder if, when web devs put their heads together, we also can find those patterns that make manual mem management easy.
@no_name4796
@no_name4796 3 ай бұрын
The real problem isn't that the program itself doesn't leak, but that gc languages are pratically always safer to memory bugs, which would creat vulnerabilities, which can be exploited by hackers And gc languages (or languages like rust, where memory is handled by the compiler, by forcing certain rules) are in general safer to those type of vulnerabilities, simply because the gc (or borrow checker) are extremely tested and inspected So basically it just comes down to "use what the prog language offers you, because it's 100% gonna be more reliable then whatever code you can produce" Lol, hope you enjoyed my ted talk 🤓
@nashiora
@nashiora 3 ай бұрын
@@no_name4796 Garbage collection is definitely not the reason that many garbage collected languages appear to have fewer memory bugs. Having a runtime present to check that things are sane is one such thing, and many garbage collected languages also have some form of runtime. You can do exactly the same as what they're runtime is doing by generating that code from the compiler, or inserting a lightweight runtime in (even C technically does this, since libc does setup and shutdown of some key things, it just can't take it as far because of its design decisions). The way you create/destroy things in these languages is also often far more restricted, giving the runtime (yes, again, the *runtime*) more control over how memory is given out and how it can be used. These exact same guarantees can be done in non-GC, non-"managed" languages; they just historically haven't been done. Rust achieves much of this safety even without the borrow checker, which is a completely separate thing to this part of the issue. It restricts how you create and store memory, and it can easily put bounds checks in its container types or when using primitives. That's just the kind of thing that was put in place during design, which is hard for other languages to retroactively add for compatibility reasons. C++'s standard library should have those guarantees in its public API too, but C++ gives you a *lot* of arbitrary control over memory regardless so you can still break those guarantees if you try. Using "modern" C++ with "modern" data structures gives you exactly the same benefits as GC'd languages or a language like Rust without most of the drawbacks, it just takes discipline to not use the parts that aren't as savory, which yes is a downside. In most cases, it's *really* not hard to create a domain-specific memory management paradigm for your application in a non-"managed"/non-GC'd language, it just takes a little thought and discipline to do right. Programmers coming from a GC'd language or similar just haven't had to think about it at all, or if they have it's in a very different context (see people having to resort to object pooling to avoid massive amounts of allocations that will freeze up the GC, they're *still* having to consider manual memory management and are actively having to work around and *against* the GC in some cases). GC isn't the solution. Rust's borrow checker isn't the solution. Those are additional tools provided by those languages *on top of* other memory safety guarantees done in ways unrelated to what automatic memory reclamation system is in place. Devs in non-managed languages have solved this problem in many different ways, for many many decades, because it's not impossible to solve and it's necessary to do differently for different domains and use cases. GC isn't one-size-fits-all, and neither is the borrow checker. You can do less wrong by default, but it's not the best solution for many people and you end up having to fight it, often also removing many safety guarantees provided by the language in the process. I don't know where I was going with this other than "GC isn't the important thing to talk about here" at the end of the day. Every single language, regardless of its feature set, eventually requires programmers to think about memory manually. If you can build your own safety measures in the process, you don't have to fight those provided by the language or its runtime and accidentally cause more issues.
@shauas4224
@shauas4224 3 ай бұрын
Would you mind sharing some resources on how game dev memory management was "solved"? Would be really curious to read
@raphael.e.diener
@raphael.e.diener 3 ай бұрын
​@@shauas4224I'm sure you can find it by searching on KZbin yourself, but here is the quick summary: There are 3 types of lifetimes: frame bound and persistent over frames and persistent for the game. You don't need to care about deallocation for game persistent assets, since they should exist for the entire program and then get reclaimed by the os. Assets that life for only one frame is also easy. Have an arena allocator that you pass into the functions and allocate on there. If the frame is over, reset the "beginning" pointer back to 0 and you don't even need to wait for the os on the next frame since the buffer will have the right size and be allocated. Frame persistence is a tad more tricky and depends on the game. If your game is simple, simply have a dynamic array/vec for every type of thing you have in your scenes. When it gets more complicated most people create an asset manager which does the same but behind an abstraction. The asset manager itself will persist for your programs lifetime, hence the os will take care of that you could just make it a global, or pass it as an argument if you care about architecture. Those 3 enable you to always free in the same code block where you do the allocation and you pretty much only do allocation in 2 places: the setup routine and the main game loop. This makes things very clear and structured
@My0ldFr13nd
@My0ldFr13nd 3 ай бұрын
I like garbage collection because I'm garbage at coding
@paulkanja
@paulkanja 3 ай бұрын
****gets collected****
@SlinkyD
@SlinkyD 3 ай бұрын
Trash Dev gang 🫳⌨️🗑
@ridass.7137
@ridass.7137 3 ай бұрын
I want to know what my garbage collector is doing while I creep around my home and act like a goblin
@Duckless137
@Duckless137 3 ай бұрын
Judging you
@nordgaren2358
@nordgaren2358 3 ай бұрын
Not all strings are stored in the heap. In some languages like C++, there is "Short String Optimization", which puts the string into the string structure itself with a union, if it's short enough. That gets stored wherever the string object is stored, which can be in the stack. C compiler will also put strings in the stack, if they are short enough
@leonss2356
@leonss2356 3 ай бұрын
"you can choose to allocate and free heap memory yourself, but this usually increases the chance of a memory leak" *GC users holding onto references and leaking a shit ton of memory because they refuse to learn how anything actually works
@Nerdimo
@Nerdimo 3 ай бұрын
Go build a web app in C++ or C
@segganew
@segganew 3 ай бұрын
These days, GC is super advanced. ZGC, Java's newest garbage collector, attains constant-time sub-millisecond collection time, and very few pauses.
@mattmurphy7030
@mattmurphy7030 3 ай бұрын
Java has always been at the forefront of garbage collection technology 💪
@ItIsJan
@ItIsJan 3 ай бұрын
depends on the context. e.G: with applications that have very high object allocation rate (eg minecraft server with many players), gc cycles can easily reach a few houndred, if nout thousand MS
@segganew
@segganew 3 ай бұрын
@@ItIsJan not with ZGC
@PreposterousPixel
@PreposterousPixel 3 ай бұрын
For char* get_name(), wouldn’t a copy of the pointer be passed out of the function, allowing name (which is a pointer to data, not data itself) to be deleted by the compiler? That way there’s still a reference to the result of read_user_input() which would be managed by the larger scope. I think that’s kinda what Swift does with reference counting
@janPiki273
@janPiki273 3 ай бұрын
1:24 anarchy chess reference
@qcg_kzre
@qcg_kzre 3 ай бұрын
amazing video, this taught me a lot about topic of this video
@Quantum_Nebula
@Quantum_Nebula 3 ай бұрын
Would you be willing to do a tutorial on how you make these modern and fancy animated videos?
@tom-delalande
@tom-delalande 3 ай бұрын
I use something called motion canvas. It’s a library created by a KZbinr called @aarthificial who has some excellent tutorials for it.
@Quantum_Nebula
@Quantum_Nebula 3 ай бұрын
@@tom-delalande oh this is wonderful lol thank you sir!
@LambdaCreates
@LambdaCreates 3 ай бұрын
1:23 r/AnarchyChess reference? btw I did see something like a chess move rating on the video about the best shell
@ozzy-fr7vj
@ozzy-fr7vj 3 ай бұрын
Arena allocation brother
@dvhh
@dvhh 3 ай бұрын
Even without going that far, smart pointers are pretty ok
@revengerwizard
@revengerwizard 3 ай бұрын
Mixed mark and sweep, incremental and generational garbage collection?
@somedudenamedanthony
@somedudenamedanthony 3 ай бұрын
Could you possible make a video on how arena/linear allocation works?
@HamzaDeveloper-j1u
@HamzaDeveloper-j1u 3 ай бұрын
If you don't make garbage, you don't need a garbage collector! 😌
@tru2thastyle
@tru2thastyle Ай бұрын
What does that even mean? 😂
@thezipcreator
@thezipcreator 3 ай бұрын
0:39 this isn't true with all GCed languages; pony for example always runs the GC after a function terminates (unless you run it manually)
@tobzdaman619
@tobzdaman619 3 ай бұрын
Thanks for the video, learned a lot :)
@JakobRossner-qj1wo
@JakobRossner-qj1wo 3 ай бұрын
what happened at the end?
@prawnydagrate
@prawnydagrate Күн бұрын
1:35 gold lol
@_modiX
@_modiX 3 ай бұрын
2:00 This is what I don't really understand and I was hoping you could explain it to me. If I return some pointer, I cannot free its heap on the end of its scope obviously, but why cannot simply the parent where this pointer is received at free it on its end of its lifecycle?
@notSoAnonyymus
@notSoAnonyymus 3 ай бұрын
You want rust.
@mattmurphy7030
@mattmurphy7030 3 ай бұрын
You can, obviously. It’s just knowing when you need to do this and remembering to do it that’s tricky.
@martindespagna
@martindespagna 3 ай бұрын
goblin mode activated
@SlinkyD
@SlinkyD 3 ай бұрын
5/7 🤣🥰🙃😊😅🤣🤣 So reckless,I like.
@incrediblemhi
@incrediblemhi 3 ай бұрын
🦀
@tru2thastyle
@tru2thastyle Ай бұрын
“Big performance cost.” Things like this are over exaggerated.
@hearteyedgirl
@hearteyedgirl 3 ай бұрын
goblin
@knkootbaoat6759
@knkootbaoat6759 3 ай бұрын
interesting heap values 😅. props for no please subscribe, follow ytbers classic during the video
Garbage Collection (Mark & Sweep) - Computerphile
16:22
Computerphile
Рет қаралды 250 М.
Authentication is a developer nightmare
15:58
Tom Delalande
Рет қаралды 61 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
What's new in Flutter 3.27 & Dart 3.6
17:51
SpeedKodi
Рет қаралды 1,8 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 1,4 МЛН
The Most Valuable File Format You've Never Heard Of
15:33
Acerola
Рет қаралды 548 М.
Python 3.13 vs. Julia 1.11 with Word Frequencies
5:56
EKB PhD
Рет қаралды 1,9 М.
The cloud is over-engineered and overpriced (no music)
14:39
Tom Delalande
Рет қаралды 697 М.
The Hidden Engineering of Landfills
17:04
Practical Engineering
Рет қаралды 7 МЛН
10 weird algorithms
9:06
Fireship
Рет қаралды 1,3 МЛН
Why Lego Is So Expensive | So Expensive | Business Insider
28:18
Business Insider
Рет қаралды 6 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН