return to sourceobby
banner

The Bell Curve

A couple days ago, I saw a Reddit post from an individual on the r/CProgramming subreddit. The post was asking the question of, since the OS cleans up memory after program termination, do we necessarily need to free our memory, since it will be freed regardless of what we do? Well, the question, honestly, is that it depends. But, it depends on where you are on the bell curve. If you in the "hump", then you would say that yes, all heap allocated memory you should free whenever you no longer need it to prevent memory leaking. If you are on the left, you might say something similar, or you might even say that it depends (what it might depend on I have no idea what people there are thinking), but if you are on the right, you will say that it depends. So lets break it down.

So first of all, yes, when your process terminates, the OS will automatically (or any OS made like within the last two decades or so that is competently made for normal people) clean up any heap memory for you. So yes, in theory, leaked memory is not that big of an issue, as it is impossible with this to permanently lose memory. For beginner programmers, or those who are unfamiliar with system programming, they might ask, then why do we chimp out so bad about memory leaks? Why do we develop super advanced and cool software like Valgrind or Doctor Memory to detect them? The problem lies within the statement. The memory is cleaned up when your process exits. What if you are running a particularly persistent process? Like your web browser for instance. I would imagine that most people, without a second thought when they boot up their computer (unless they are single threaded, LOL, on some task, like checking a document or something) will open up their web browser of choice. Furthermore, they probably will not close the web browser completely until they need to shut down their computer or something, or maybe if they are about to do something memory intensive and want to free up as much memory as possible. So, in such a situation, do you still think it is ok to not properly clean up memory because when you close your browser the memory will be cleaned up anyways? What about the memory leaks while you are using your browser? What if you are running a server, and you have an application that runs in the background and you need it to run for as long as the server is online. Is it still ok to leak memory in this case?

Usually for most people, when they realize this, they instantly get it, and they join the upper quartiles. So now lets analyze the obvious other side of the coin. Consider you have a super short lived program. Why would it matter that you leak memory when your program is going to die before it becomes a problem? So if you have a super short program, it is more than ok to not clean up. Why? Because it takes some time and some extra keystrokes to type that "free()". Also if you are being careless, you could accidentally double free, something you likely won't notice until you run the program, or you are in the habit of rereading your code constantly. It also reduces the amount of code in the final binary. So although these benefits are certainly pretty small and not exactly anything to call home about, the fact of the matter is, why not scoop them up when it costs you essentially nothing? Also, what if its data that you use up until shutdown? Then yeah, why bother freeing it if the OS will do it anyways?

Now, lets address a tiny elephant in the room. Why (or for who?) are you writing the code? Are you writing for personal or for like some professional work? Then yeah, why not? In both of these cases, you can probably justify why you are leaking memory, and as long as it works, then it works. However, what if you are writing in an academic situation? How are you going to try and convince your professor who just taught you that you should always be cleaning up heap allocated memory, that you actually do not always need to? In school, they are just teaching you best practices and such, and trying to get you into good habits, and lets be honest, properly cleaning up your memory is a pretty good habit that any good programmer should embrace. However, sometimes if we are super high IQ, and know that sometimes, some problems just shouldn't be fixed, and it is more than ok for us to skirt by the rules.

Ok, so that's enough of my spiel. Now lets talk about some of the responses I got. So I actually did reply to the post like a good netizen to give our C newbie a helping hand (we will come back to this, if you are asking a question like this, you are obviously not an advanced user). I posed the same question asked earlier in the second paragraph, "What if your program needs to run for a long time?". And I got a couple responses.

it doesn't really matter if the resource is being allocated just once for example... The answer is depends...

What? Ok sure, if you allocate like a single byte and run your program till the end of kali yuga, ok, fine, it doesn't really matter, realistically speaking. But what if you allocate a sizeable chunk, like maybe a couple hundred megabytes? A gigabyte? Sure your computer might not explode, but its not good to just leak memory, especially when you really don't know what you are doing. Like if you have to ask this question, you are a beginner-intermediate user of C. And it really is a good question that will mature your problem solving and programming skills. There is no shame in asking it. But by asking, you are exposing the fact that you really don't know what the whole picture is. And it is best when you are getting started, to build a solid foundation of good habits. Only once you know what you are doing and you know when and where you can cut corners should you be taking any kind of shortcut. But ok, sure, it does depend.

I mean it sounds like the implication is that the memory is used for that whole tine. Otherwise ofc free it

This is beyond stupid. I am sorry, but if you are using memory for something, no matter how much you are using, you shouldn't free it until you are done using it.

Lets say I malloced some memory at the start of the main function, then I have a function that loops continously and uses pointer to that malloced memory, it it a big deal?

Similar to the last comment. This is a stupid premise and completely missing the point.


I don't mean to make these comments to be mean or rude or something. But the answer of "it depends" is kind of dangerous. It is not a good idea to just plop down "it depends" in this scenario. Is it the truth? Yes. Do we teach high school chemistry kids the complete truth about the atom? No. Why? Because it will confuse them, and they need a simplified answer now to build a strong foundation so that they won't be confused in the future. The answer of "it depends" is too complicated here to justify using it. Sure, it does. But it is best practice to free memory that you do not need while your program is running. It is better for the young novice to understand this than for them to go off and think they can just cut corners as they please because the internet came up with a use case and said so.

Ok that is enough rambling. Now you might ask, Josh, did you really spend like an hour writing this article on your own website that you own and have complete dominion over so that you can ramble for however long and however you wish? Yes, I really did.

If you wish to read my comment and the replies I received, you may do so here

Josh