What are typical memory leaks or issues? This is in not a specific order and not all are actual memory leaks, for example, the first two. So, a wrong data type is still something that we have to look into because this is a very typical thing I frequently encountered. Often programs use memory types that are not efficient for the task that you want to solve. And then, when you handle like this big data, you're going to allocate a huge amount of memory, even though you would not need that, by just using a different data type.
So using the correct data type can also help you to prevent your program to crash in case you once encounter bigger data. And the second one is that we also try to run a lot of things in parallel. All of us probably love promises and async await, and we use promise all to improve the performance of the application, because normally, like with Node, things are all run on a single thread and we still want to do multiple things at a time, pretty much, to have multiple remote procedure calls. But when you just use too many of those at the same time, we also have to allocate memory call all of these steps. And when they are not yet done, they might also just cause an exception in your program because there's no more memory left. So this is also important to look into.
Now let's go to the first actual memory loop, event listeners. So something a lot of people know is in browsers, like some while ago, it was typical to just attach event listeners immediately to a specific DOM element in a list that you would care about an event to trigger from. And then we sometimes have to just add lots and lots of event listeners to all of these elements. Instead, you want to have a single event listener at a top node, that would then listen to all the events coming from all these nodes below. And then you don't have to attach so many listeners because event listeners also add up in memory. And sometimes we just add a new event listener for each event incoming. This would be a program at arrow, but it's frequently occurring.
And more difficult is even a closures. Because sometimes closures prevent the garbage collector from knowing when a variable is not used anymore. And when we are able to dispose that memory. So this is a tricky one. Then file descriptors can also cause issues. Because when you open up a file, there's a limited amount of files you can open at the same time. And when we just run our program, it normally works, even though we do not close in the file after opening it, that just can cause issues as well. And so we always have to make sure to close it on both success and non-success. So you should always have like a file link that no matter if you are successfully opening in that file or not. Because sometimes there might be something in between and you still open it. But there was an error somewhere else. And you still have to close that one. The most tricky part is that in JavaScript, and when we look in Chrome or any Chromium browser and Node.js is also run with V8, then we have to know some implementation details.
Comments