r/computerscience • u/Hot-Bus6908 • Dec 30 '25
Discussion Would it theoretically be possible to make a memory leak happen on purpose? I know memory leaks only happen under pretty specific conditions but I've always been oddly fascinated by the useless side of modern technology.
112
Upvotes
99
u/thememorableusername Dec 30 '25 edited Dec 30 '25
Leaking memory is not like a super hard edge-case, it is very easy to do and people do it all the time.
Calling this function will cause a random amount of memory (up to 1KiB) to be allocated but inaccessible. Unless there is a special memory allocator and/or compiler pass which detects the unused allocation.
265
u/Peanutbutter_Warrior Dec 30 '25
Memory leak is a very general term, it's not hard to have one. Allocate a block of memory and forget the pointer to it and you have a memory leak
30
u/high_throughput Dec 30 '25
It's a bit like asking "would it theoretically be possible to make an airplane crash on purpose? I know airplane crashes only happen under pretty specific conditions."
Yes, it's very easy. In fact, it's the natural state of a plane to want to crash into the ground, and it will do so unless you put great effort into preventing it. The only reason it doesn't happen constantly is all the routines and tooling in place specifically to avoid it.
Similarly, it's the natural state of memory to leak. It will do so unless great care is taken to make sure it gets freed. It's a core consideration in the design of any language.
This is a memory leak in C++:
string* foo = new string("Hello world");(you would plug it by making sure there's a
delete foo;when you're done using it, and a much beloved and universally adopted feature introduced in C++11 was smart pointers to help with that)15
u/SenatorBunnykins Dec 30 '25
Yes, trivially. Just write a program that keeps allocating memory and never freeing it.
Memory leaks usually happen because someone's done so accidentally.
20
u/throwwaway_4sho Dec 30 '25
Yess, do tons of malloc in c and then forgot to free it. Next thing you know RAM is full and system bsod. Happened a lot if you do parallel computing.
8
u/nuclear_splines PhD, Data Science Dec 30 '25
Just allocate memory and then don't de-allocate it.
4
u/P-Jean Dec 30 '25
Yes, and then release an updated patch which makes your game “faster” for $.99.
4
u/[deleted] Dec 30 '25
Sure, it's very possible.
This program allocates 128 bytes of memory 1000 times, 1/5th of the time it randomly doesn't free the memory. This non-freed memory is still is use by the program, but the program loses track of it. IE it's "leaked"
1
u/helldit Dec 30 '25
If you like the topic, read about virtual memory. It's a super clever technique where the operating system and the processor trick programs into thinking that they have access to the entire system memory when in reality they only have access to what they are using plus a small buffer.
1
u/Few_Language6298 Dec 31 '25
Right now consciousness in machines is speculative, AI can simulate smart behavior but we don't have a scientific way to prove or engineer real subjective awareness yet.
3
u/QueSusto Dec 30 '25
It is quite easy and deterministic in any language without garbage collection.
2
u/gluedtothefloor Dec 30 '25
Yeah, if youre programming in a language where you need to manage your own memory and you dont manually free it.
2
u/Silly_Guidance_8871 Dec 30 '25
Two ways:
2
u/Mess-Leading Dec 30 '25
Useless side of modern technology? Manual memory allocation makes things possible that would otherwise not be simple and it just so happens that manually allocated things require manual deallocation which makes sense but is easy to forget!
2
u/mauriciocap Dec 30 '25
Check "Cheney on the MTA" / the Chicken scheme compiler. Never returning functions using the C stack as a generational GC arena. Stack Overflow=run GC.
2
u/set_of_no_sets Dec 30 '25
You can also make memory leaks happen in more "memory safe" languages. Ex. first google result for "mem leak java" https://stackoverflow.com/a/6471947
2
u/Daemontatox Dec 31 '25
"Under pretty specific conditions "
Lol you haven't seen my code on a Friday night, a sneeze will segfault it
2
u/alterego200 Jan 01 '26
new int; // C++ memory leak
If you sprinkle memory leaks over various sizes throughout your code, you can trap where your memory leaks are coming from.
2
u/txgsync Jan 01 '26
You can do it yourself in like 5 lines of C.
1
u/Soft-Marionberry-853 Dec 30 '25 edited Dec 30 '25
depending on your pov, the do happen on purpose. The code is doing exactly what you told it to do. Its just that what you told it to do was probably wrong.
1
u/zenidam Dec 30 '25
Yeah. It was fun before protected memory, too. For a while as a kid, if I saw an idle Apple ][, I'd sit down and write a little infinite loop that would just pick two random integers and POKE the value of one into the location of the other and keep going until something crashed. Every now and then you'd get weird and spectacular crashes that way.
1
u/CadenVanV Dec 30 '25
While a lot of higher level languages, like Java, have garbage collection stuff built in to prevent memory leaks, most low level languages like C do not, meaning it’s trivially easy to cause a massive memory leak.
1
u/voidsifr Dec 30 '25
Microsoft has said numerous times that about 70% of sll their security vulnerabilities are due to mismanagement of memory. Allocating memory and then forgetting to free it or losing track of it is a very common mistake.
Entire classes of languages and tools exist to try to solve that problem. For example, python, Java, Javascript etc don't even require you to manage memory. You just allocate memory and there is a garbage collector that tracks if that memory is being used and will clean it up. That's why you have to download "java" or "python" if you want to run Java or python programs. You are downloading their runtime, which has rhe garbage collector (among other things). For Javascript, the garbage collector is built into the browser.
You have Rust which enforces rules at compile time such that it won't even compile your code unless you follow those rules, which prevent memory leaks. You can still have issues though because you can explicitly ignore those rules.
Then you have tools like valgrind or Dr. Memory to try to detect leaks in languages like C.
There are cases where you typically don't care though. Allocating and Freeing memory has performance impacts. So for something like a simple cli tool, its common to just leak it and not care because the operating system will reclaim all process memory. Another case is one time long lived memory allocations. If thst memory is needed for the entirety of the application, there is no point in freeing it because the operating system will reclaim it when your program ends. So you could technically, intentionally leak that memory.
Where you will find this is video games. Deallocting has a cost. And video games need every bit of performance they can get. So when you load up a game, it gets a huge chunk of memory. And then as you play, it won't free any of it. You will either enter another area of the game (like a room) and the memory will get reset, or like battlefield or cod, the match will end and the memory will reset, or you will run out of memory and you will get kicked out of the match and sent to the game lobby (It's like a controlled crash). Lookup bump allocators and arenas and watchdog for more info an techniques for that. But they technically leaking memory on purpose.
1
u/seanprefect Dec 30 '25
to add to what other's have said it' rare in modern programing to have language that's better than another langue. Languages can be better for certain things than others but ultimately they're like tools in a tool box. Is a screwdriver better than a hacksaw? if you're screwing screws of course if you're sawing wood of course not , are you hammering nails ? then neither is good.
VM languages can actually be faster as web servers and can compile once and run on a lot of things but they take a way some features you might need for real time or software that wants to maximize the use of particular hardware.
C (and to a lesser extent C++) you run the risk of memory leaks , have a lot of trouble with collections , you'd never really want to use it as a web backend but to make a photo renderer or twitch video game ? you'd need those features
1
u/lupercalpainting Dec 30 '25
Every cache without an eviction policy is a memory leak.
1
u/usr_pls Dec 31 '25
Yes, it's a good exercise to try out yourself so you will know what to look for
1
u/Fizzelen Dec 31 '25
Create a linked list with a reference to the previous item and add items until you run out of memory.
1
u/strange_username58 Dec 31 '25
It super easy
1
u/Cybasura Dec 31 '25
Er, normally people do it on accident when they start using malloc or any memory allocation functions in general
So yes, that includes on purpose, because just do what you usually do
1
u/starlulz Dec 31 '25
malloc in a boundless for loop lol
HelloMemoryLeak.c
1
u/WitsBlitz Dec 31 '25
I'm curious what your understanding of what a memory leak is, given that you see them as fairly niche or unusual. Sincere question, not trying to be mean or anything.
1
u/Candid-Border6562 Dec 31 '25
Yes.
Curiously, the more proficient you are, the harder it is.
1
u/JohannKriek Dec 31 '25
in C#.NET, create an instance of a class A. Have it subscribe to an event in another class B.
Do not release this event handler for B in the Dispose() of class A.
Consequently, the object of class A will not be freed, even if it is not being used and can otherwise be claimed by the garbage collector. You thus have a memory leak.
1
u/ShoulderPast2433 Jan 01 '26
In c/c++ very easy.
In Java not so much (garbage collector)
1
u/HaphazardlyOrganized Jan 01 '26
If you want to replicate the conditions of known exploits, you can always emulate an older machine and run the unsafe code.
1
u/helpprogram2 Jan 01 '26
Learn programming is prob a better sub for this question
1
u/serendipitousPi Jan 02 '26
I’m surprised no-one has mentioned the absolutely peak way of leaking memory. In the rust standard library there is a method for it.
I’m doing this on a phone so I don’t know if the formatting worked.
1
u/generally_unsuitable Jan 02 '26
while (1){pointer = malloc(1);}
1
u/gm310509 Jan 04 '26
If you allocate memory and never free it and your environment does not have automatic garbage collection then yes.
Here is a simple example in C:
Every malloc in this loop is a "lost" allocation of memory and would, IMHO, constitute a memory leak. Some will say it isn't because it is deliberate and easily spotted, but that was not your question.
You can do something similar in other languages - e.g. Java - so long as you maintain a pointer to the object. For example, continuously appending objects to a linked list without ever removing any of them.
1
u/Chuu Jan 04 '26
A memory leak just means a program asks the system from memory but never returns it while the program is executing. You could trivially do this on purpose if you really want.
1
u/makgross Jan 05 '26
Of course. If you want to test response to resource exhaustion, you have to exhaust resources.
I also have procedures to generate bus errors, stack errors, division by zero errors (damn microcontroller resets with those), and anything else that might occur in flight, to verify those are logged and handled properly.
1
u/roopjm81 Dec 30 '25
For(int i; I <2147483647; i++) { Char *blah = malloc(sizeof(char)) ; }
Woops you just allocated and lost 4mb
0
u/AgathormX Dec 30 '25
Obviously.
You allocate with malloc.
If you don't call free() afterwards, it won't free the memory.
C doesn't have a garbage collector.
In higher level languages like Java, Python or JavaScript, you don't need to worry about manually freeing memory, because the garbage collector handles it for you.
C puts that control solely in your hands, with the benefit being that it allows for better resource management.
Mind you, there are circumstances where variables with get deallocated anyway, which is normally the case when stack variables go out of scope
0
u/YoungMaleficent9068 Dec 30 '25
You mean as an attacker to get a dos attack? People do that all the time. It's quite some work and usually not much payoff but in general yes.
3
u/[deleted] Dec 30 '25
[removed] — view removed comment