Guilty Pleasure of Dirty Memory

Published on 2022-07-28 22:40:00+02:00

"Remember to free the allocated memory!" That's what we usually teach everyone when they start their journey with manual memory management. Of course, it's the rightful way. The one and only, isn't it?

Well, yes but no.


Let's start with the basics - why do we deallocate the memory? It's because we, or rather the program that we implement, does not own it. Instead the memory is requested from the Operating System or otherwise some kind of allocator. It provides part of the memory to the program. It's natural to give it back after we are done since that's a part of the contract (although it's an unwritten one usually).

The thing is - we can ignore this contract. OS will reclaim any memory allocated for the process after the process is done. It will consume its corpse and leave nothing behind. There are rules and exceptions, so let's write a formal sentence describing this approach.

When implementing a short-lived userspace program, you don't need to pay attention to freeing memory allocations. There is literally no point to it. You don't risk any memory leaks this way. What is a "short-lived program", and are there things I need to pay attention to?

I'm gonna answer the first one in a vague way. For me, this means something like a thing I would execute from shell and expect "immediate" response. For you it might be something completely different and I honestly don't care. The whole point is to be free once in a while. If thinking about whether you should do it or not takes too long, then it's probably better to do the usual thing. If you have a hunch that you can do it and there is no real cost, then what are you waiting for? For sure don't do it in daemons (unless you are old PHP).

As for things to pay attention to - first off, anything that's not owned by the process or that live longer than it. Of course, these are not "allocated memory", but use this as a reminder to not get too careless. This is an opportunity to have fun right at the boundaries of the "correct" implementation and not beyond them.

Another potential problem is growing rate. Even a short-lived program can try to allocate a lot of memory. This can easily accumulate in loops or similar situations, so keep an eye on that. As a good rule of thumb it's easier to just deallocate the memory that was allocated in a loop.

Now, now, before you comment that it's a stupid idea. Yeah, it is. That's exactly why you should try it at least once. This is an exercise about manual memory management: "can you guess which things are actually worth deallocating?" This is a fun activity if you like programming and enjoy trying things in a completely unusual way. This is a liberating approach that let's you focus on solving some problem instead of being correct. This is an excuse when you simply forgot to free memory and someone pointed it out.

It's all of it, and also a stupid idea. Everything depends on the point of view and what you have in hands. Try it!