A little tidbit for programmers

Off topicProgramming → A little tidbit for programmers

Note: Java should have a built-in Garbage Collector, so maybe you don’t need to worry about this.

In many client-side programming languages, such as Java and C, you may find yourself using ‘malloc()’ or ‘new()’ to allocate memory. (C and Java, respectively). This is useful mainly for managing pointers and assigning additional memory to variables. However, many people make the mistake of not freeing the memory afterwards. No error is usually given for forgetting to free memory, so it is usually overlooked.

Whenever you write a function, if that function allocates additional memory, the computer will keep the allocated memory until the return value is called. After a value is returned, all allocated memory will be deleted (in reality, it is simply flagged as overwritable, but essentially deleted). However, not all functions use a return value - many functions are not used as ‘calculators’. As a result, all the memory allocated in the function stays used even after the program is done running. Over time, this ‘waste’ memory will build up. Ever wondered why new computers get slow over the years? It’s because along with the sheer amount of programs or apps downloaded comes sloppy code, where spent memory is not disposed of.

Now, while having a build-up of waste memory while running a program can cause quite a bit of lag, it is usually not permanent. However, if the memory is not freed (using ‘free()’ or a GC) when the program ends, the remaining memory can be permanently causing lag. Most minecraft servers reboot periodically, but only because of plugins with sloppy code. Over time, however, no matter how much the server resets it will become slow. However, if all code is perfect and memory is freed properly, servers shouldn’t need to reboot at all (unless something else needs rebooting).

Hope this helped spread awareness about poor memory-management!

Also, C isn’t the only programming language where you need to worry about this. I simply used it as an example.