How to Prevent All Memory Leaks
By Tsoding
Summary
## Key takeaways - **Memory leak definition is fuzzy**: Defining memory leak as memory not freed before program exit is useless because programs often allocate a fixed amount upfront and exit without deallocating, yet it causes no problems. A better definition is unreachable memory like in garbage collectors, but it has issues too. [00:11], [00:43] - **D lang GC fails on XOR linked lists**: D's garbage collector scans for integers resembling valid pointers to mark objects reachable, but XOR linked lists store XOR of pointers, which are invalid pointers. This tricks the GC into considering objects unreachable and cleaning them up prematurely. [01:26], [01:48] - **JS leaks via forgotten callbacks**: In JavaScript, forgetting to clean up callbacks that hold references keeps objects reachable despite the garbage collector. Memory usage grows because things that should be unreachable remain reachable. [02:08], [02:18] - **Leaks are subjective programmer intent**: Memory leak is more like an intent, which is subjective, not a clear-cut thing. When you think deeply about what a memory leak is, it becomes a fuzzy, social construct. [02:41], [03:02] - **Allocation is artificial construct**: Memory allocation and deallocation are artificial constructs layered on top of the fundamental computational model of a head and infinite tape. On old machines like 6502, the entire memory is available without allocation; we invented it to ease memory work but introduced these problems. [03:13], [03:48]
Topics Covered
- Full Video
Full Transcript
would you use to debu memory leaks something like as and W gr yes both of them depending on which one works for me I just like use both of them but detecting memory leaks with v grind is kind of difficult because the definition
of memory leak is kind of fuzzy what is a memory leak chat what is a memory leak if you define memory leak as something that was not freed before exiting the program that's kind of a useless
definition because that's the [ __ ] that happens all the time and doesn't cause any problems because people quite often just prate the fixed amount of memory that they use for the entire to the program and then they exit without deoca
this memory so technically because of that definition it is a leaked memory but in reality it is not a leaked memory in in fact defining leaked memory is very difficult in reality it is very
difficult to Define what is a leaked memory you can use the definition of garbage collectors which is basically unreachable memory that is a little bit better definition than just memory that
you didn't free until the end but it also has their own problems for example D programming language uh has a garbage collector but it has a limitation because it's a system programming language and it allows you to work with
pointers directly you can potentially mess with those pointers so what it does it scans the memory and looks for integers that looks like a valid pointers to the memory and because of
that it considers the object under such pointer as reachable if you start messing with the pointers to the point that garbage collector cannot recognize that pointer it will basically clean up that memory because it became
unreachable but in reality it wasn't unreachable one of such things could be zor linked list do you guys know what is zor linked list zor linked list it's absolutely useless thing but that's besides the point the thing about zor
link list is that it takes a zor operation between the pointers so the number that stores two pointers is actually an invalid pointer and that trips off the garbage collector of D and
basically the D developers recommend to not do any of such tricks because it tricks garbage collector into thinking that those objects don't exist anymore and it will clean up your linked list so
again reachability is a better definition but it's not useful in system languages where you have a direct access to the pointers because you can mess with the pointers furthermore quite
often in JavaScript for instance not a system language right JavaScript you may forget to clean up some of the callbacks that hold pointers to some things and
because of that the usage memory is kind of growing because the objects that should have been unreachable still reachable you have a garbage collector you have a good definition of memory leak you still leaking memory the memory
conception is growing because the things that should have not been reachable are reachable and you still back to thinking about memory management there is no good definition of a memory leag that's the thing memory leag is kind of a
subjective thing if you start thinking about what is memory leak deeply you will realize that what the [ __ ] is a memory Le it's more like an intent but an intent is is a subjective thing you know what I'm talking about it's kind of
difficult right so when you only start in programming it feels very easy oh you just like you forgot to free no no no no no this is is actually kind of more complicated than that are you saying
that memory leak is a social constract it kind of is in a sense I probably because memory allocation is a artificial construct it is kind of an artificial construct because on the
level of computational model basic fundamental computational model what you have you have a head you have an infinite tape and you have the whole tape at your exposal where is the memory allocation deallocation in that
computation model this is a construct that we put on top of the fun mental computation model and in reality if you take a look at the x86 okay so with x86 machines is kind of difficult because
you have a virtual memory and memory mapping and stuff like that but look at it like old machines 6502 or something where you just have a memory in the 6502 program you start a program that is located in a memory you have the
entirety of the memory at your exposal you have no allocation deallocation this is something that you made up to make working with the memory a little bit easier you see what I'm talking about it's just like kind of you created this
artificial construct and now you dealing with the problems that you introduce by introducing this artificial construct and it kind of spiles down from there and I'm not proposing any solution or
anything like that I'm just stating the reality we have to deal with I'm not saying that we have to not use memory location or anything like that it's just like the reality is that we have to work with this very fuzzy Concepts I I don't
know if I make any sense but it's it's kind of weird Mar only matter if the code that alates the memory is C repeatedly true memory leaks only matter if you're poor and don't have any
[ __ ] money to buy Ram stop being poor that's how you [ __ ] fix memory leaks
Loading video analysis...