this post was submitted on 31 Aug 2025
314 points (96.4% liked)

Programmer Humor

26095 readers
1424 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

Finally I have a valid reason to learn about memory management. It was also hella weird when encountering it.

top 50 comments
sorted by: hot top controversial new old
[–] iAvicenna@lemmy.world 6 points 20 hours ago (1 children)
[–] cows_are_underrated@feddit.org 3 points 20 hours ago (1 children)

I did not knew this existed, so thanks for the tip.

[–] iAvicenna@lemmy.world 1 points 9 hours ago

have fun (ノಠ益ಠ)ノ彡┻━┻

[–] ulterno@programming.dev 15 points 1 day ago (1 children)

Back when I was a kid and was learning C, I used to wonder why people considered pointers hard.
My usage of pointers was like:

void func (int * arg1)
{
    // do sth with arg1
}
int main ()
{
    int x;
    func (&x);
    return 0;
}

I didn't know stuff like malloc and never felt the need in any of the program logic for the little thingies I made.
Pointers are not hard. Memory management makes it hard.

[–] chunes@lemmy.world 2 points 17 hours ago

C makes them unnecessarily confusing in my opinion. In Forth they're as simple as can be compared to C.

[–] diemartin@sh.itjust.works 4 points 23 hours ago

It'll be fun when you get to funny errors because you used freed memory.

When I was learning about linked lists and decided to use them in a project, I "removed" items by making the previous item's next point to this item's next, except I misplaced a call to free before using the fields, and it somehow still worked most of the time on debug builds, but on optimized builds it would cause a segmentation fault 100% of the time.

[–] boredsquirrel@slrpnk.net 41 points 1 day ago (2 children)

Unused memory is wasted memory

[–] mmmac@lemmy.zip 18 points 1 day ago (1 children)

Cloud providers LOVE you with this one quick trick!

[–] henfredemars@infosec.pub 2 points 21 hours ago

Also goes for mobile. You use more memory and apps get killed.

[–] sus@programming.dev 5 points 1 day ago

and with a good enough leak, the amount of unused memory will become negative!

[–] favoredponcho@lemmy.zip 9 points 1 day ago

Valgrind to the rescue

[–] henfredemars@infosec.pub 26 points 1 day ago* (last edited 1 day ago) (15 children)

Not freeing your memory at all is a memory management strategy. I think some LaTeX compilers use it as well as surprisingly many Java applications.

[–] entwine@programming.dev 4 points 17 hours ago (1 children)

This non-sarcastically. The operating system is better at cleaning up memory than you, and it's completely pointless to free all your allocations if you're about to exit the program. For certain workloads, it can lead to cleaner, less buggy code to not free anything.

It's important to know the difference between a "memory leak" and unfreed memory. A leak refers to memory that cannot be freed because you lost track of the address to it. Leaks are only really a problem if the amount of leaked memory is unbounded or huge. Every scenario is different.

Of course, that's not an excuse to be sloppy with memory management. You should only ever fail to free memory intentionally.

[–] henfredemars@infosec.pub 3 points 17 hours ago* (last edited 17 hours ago)

Absolutely. I once wrote a server for a factory machine that spawned child processes to work each job item. Intentionally we did not free any memory in the child process because it serves only one request and then exits anyway. It’s much more efficient to have the OS just clean up everything and provides strong guarantees that nothing can be left behind accidentally for a system where up time was money. Any code to manage memory was pointless line noise and extra developer effort.

In fact I think in the linker we specifically replaced free with a function that does nothing.

[–] GissaMittJobb@lemmy.ml 2 points 20 hours ago* (last edited 20 hours ago)

Yeah, you can use the Epsilon garbage collector in Java for a no-op garbage collection strategy.

For short-lived programs that do not risk hitting any memory constraints, it makes a lot of sense - zero time wasted on cleanup during execution, then you just do it all at the end when killing the program, which you can do in constant time since you don't need to reason about what should remain or not.

[–] csm10495@sh.itjust.works 5 points 1 day ago (1 children)

Upvoted. This is something I learned rather recently. Sometimes it's more performant to slowly leak than it would be to free properly. Then take x amount of time to restart every n amount of time.

[–] henfredemars@infosec.pub 1 points 21 hours ago

A middle ground is a memory pool or an object pool where you reuse the memory rather than free it. Instead, you free it all in one operation when that phase of your application is complete. I’ve seen this done for particle systems to reduce overhead.

[–] wewbull@feddit.uk 8 points 1 day ago

.net

Anything I run in C# or similar seems to allocate 512GB of virtual address space and then just populates what it actually uses.

load more comments (11 replies)
[–] resipsaloquitur@lemmy.world 9 points 1 day ago (1 children)

RAII.

Can’t leak what never leaves the stack frame.

[–] CookieOfFortune@lemmy.world 10 points 1 day ago (1 children)
[–] resipsaloquitur@lemmy.world 7 points 1 day ago (1 children)

Classes are just pretentious structs.

[–] CookieOfFortune@lemmy.world 3 points 1 day ago (1 children)

How do you get destructor behavior in C?

[–] henfredemars@infosec.pub 2 points 21 hours ago* (last edited 21 hours ago) (1 children)

You call the destructor. It’s simply not automatically done for you with the concept of going out of scope.

Back when C++ was simply a text pre-processor for C, you could see these normal function calls. You can still see them in the un-optimized disassembly. There’s nothing magical about a destructor other than it being inserted automatically.

[–] entwine@programming.dev 2 points 17 hours ago

being inserted automatically.

Aka the entire point of RAII

[–] bjoern_tantau@swg-empire.de 11 points 1 day ago

You haven't lived until you've produced a memory leak in JavaScript.

load more comments
view more: next ›