In the general case, if you do not free memory when it is no longer needed, you can run out of system memory. This is what is known as a memory leak.
This is most obvious in programs that run for a long period of time ... when you start and allocate an increasing amount of memory (for example, call malloc), more and more memory resources are consumed by the program. If they do not return to the system, the operating system may start working from memory to provide it to applications when "malloc" is called.
Some operating systems and programming languages โโhave functions that can either mitigate or prevent this.
Java, for example, does not allow you to directly call malloc for free. Instead, when you create objects, memory is allocated for them in the java runtime. When you are done with variables (that is, when they go out of scope), they are marked as garbage collection. Typically, a task or garbage collection function is run periodically and will be freed from all memory associated with variables that are no longer in use (ie. "Cleans garbage").
Some operating systems also support run-time limits on processes and / or threads that prevent them from consuming "all" system resources. If any of these restrictions is affected, the operating system may stop the program.
Hope this helps, - J.
source share