Garbage Collection in Java: You might be doing it wrong.

Seriously? Most OOP languages come with constructors and destructors and java isn't any different. If you control the full contextual life cycle of an object then by all means destroy it when you are done. Good OOP practices also involve good memory management. When you are done with a string object, then delete it. If you are passing it on to another object and it is the final consumer, then it should delete it. If it something that must persist through the life of the thread or application, then let it persist until either terminates.

You needn't be sarcastic about good practices, because when you do that, it proves you are either emotionally attached to lazy behaviour, or poorly informed.

When done properly, good object context management can save the programmer from a whole mess of bugs. Have you ever wondered why so many mods fill up memory when all they do is allocate a few strings? One reason is that while the do the allocation, they do not bother to deallocate unneeded strings but rather wait for garbage collection to get around to a job they should have done themselves. As shown by the article, that may or may not happen if the frame renderer, the AI, or some other thread is so busy that the garbage collector doesn't get a time slice.

When the garbage collector doesn't get a time slice to free up resources, or it enters a race condition when marking potential free objects, it may NEVER get the opportunity to do so if a higher priority thread keeps the JVM 100% busy. Watch the heap grow under ordinary conditions on a server and you will see my point. This is one of the reasons for the need for periodic server and client restarts that would be unnecessary if proper heap management were implemented.

Lazy is a lazy does. All of my code properly removes objects, in JAVA, C++, C#, ADA, etc. I have yet to have either a memory leak or a heap related restart. Several of my programs have run continuously since going golden without the need to reboot at all. Some have run for twenty years or more.

So I know what I'm talking about when I say, clean up you own messes and don't beg the garbage collector to do it during its limited time-slice. It has better things to do than clean up after you!

/r/feedthebeast Thread Link - blog.takipi.com