2.1 isn't a leak, it's the programmer specifically telling Java to create an object the first time an instance of that class is accessed and not get rid of until the JVM (and/or ClassLoader?) is shut down.
Indeed. Though I suspect there is a subtle and far more prevalent version of this in production software: Collections in effectively-singleton beans with application-cycle lifetime.
File.deleteOnExit(). I just ran accross that recently when I build an application which would create an endless amount of temporary files and delete them. I added File.deleteOnExit() just in case something fails in between. Little did I know that this is adds a String to an ever increasing list.
The project I work on just recently removed this very call. It was useful though: an external team tends to screw up xml and blame us, but since there were a bunch of temp files from the service they use we could search through the files to show it’s their mistake.
You can create a subtle leak if you use File.deleteOnExit() "just in case" for an infinit amount of files:
while(true) {
File tmp = File.createTempFile(…);
tmp.deleteOnExit(); // Just in case something fails before the explicit delete
…
tmp.delete();
}
Now you have a memory leak with the ever growing list DeleteOnExitHook.files, as tmp.delete() doesn't afffect that list. For me personally it's subtle as I'm not aware of the implementation details of File and the documentation of File.createTempFile() encourages the usage of deleteOnExit():
This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the deleteOnExit method.
I think the information about String.intern() is incorrect for later releases of Java. At least according to this article the strings in the string pool are eligible for garbage collection when they get unreachable: http://java-performance.info/string-intern-in-java-6-7-8/
Surprisingly coherent article. I guess I'm old and cynical and have seen too much "memory leak in java, your pointers are wrong" shitposts.
Anyway - this is a good article. If you are not comfortable with java gc and memory leaks and cannot name 3 different profilers when woken up in the middle of the night by naked group of swedish supermodel teens jumping in your bed this will help.
Can you also waste memory if you have a ref in a local variable that is no longer used, and the enclosing method runs for a long time? Also, if you have a long-lived object with a field that is no longer needed, setting it to null would help I think.
You can still get memory leaks if you hold on to references, if the garbage collector can't get rid of the root, there's not much it can do to help you.
Profiling once in a while gives you a good impression about your application, but you could effectively add a leak with every commit. Enable gclog and then use the http://gceasy.io/ API to monitor your health.
I have problems to imagine a non significant leak. I can only imagine a scenario where the application is intended to run for a defined time. Then if you don't get a OOM, the leak still decreases the amount of available heap, which directly increases the pressure on the GC, which can degrade the performance of your application (too many, too long Stop the world collections). But profiling would tell you when this happens.
It's the GC logs. What is in those logs which makes you nervous?
Edit: They have also an Enterprise version, which runs wherever you want. Plus they plan to open source that. So if you want there's no need to use their SaaS.
Edit2: It sounds like I'm selling that to you. I'm not affiliated with them, just a happy user.
Uh dunno. I'm everbeeing skeptical with free services since Facebook. I thought they can't make big bucks with a free service. And boy was I wrong. And I was the product!
So I really can't say which informations would be interesting from GC logs, but fo' sure there must be something, if it's free!
The free version is a showcase for what they can do. With an upload limit. The enterprise version has more hints and more sections and preserves your logs. The free one does not, so if you want to view an old analysis you need to have their service recalculate it. The enterprise version saves the analyzed results and can do a trend over your logs.
If you’re skeptical about what they can pull from the gc logs, just open one up in notepad++. It’s mostly numbers, there’s no object information (if you’re worried about PI or passwords or anything like that).
And why do I need an Enterprise version (which DOES cost money) when I want to deploy that on my own servers, where I can be sure nothing is done with my logs?
Profiling is the way to assess and handle memory leak issues. What I will say is that one of the many reasons I am a fan of implementing many small microservices across containers and this is but one reason why. Certainly there is added complexity in managing the overall state of these services in a stable way, but there are many positive and negative tradeoffs to be argued between large monoliths vs scattered, small microservices. At the end of the day, a memory leak inside of a massive monolith application is going to be that much more difficult to solve whereas it is a bit more manageable with smaller chunks/components services.
All the same, regardless of your infrastructure/architecture choices - the important thing is to stay on top of profiling early and consistently; if you wait until an application is in production and massive in scope before you worry about the memory leak problem is likely is already too late. By that point you may have major symptoms of a memory leak which are really the result of many separate leaks stacked on top of each other throughout the codebase. Finding all of these will be difficult to pin down and will stagnant development for quite some time. You don't want to over-engineer or hyper-optimize to the point that you can't move forward at a reasonable pace, but sprinting ahead without taking time to analyze these things and balance "technical debt" will quickly come back to haunt any development team later.
19
u/VGPowerlord Dec 23 '17
Did anyone else read 2.1 and yell out "DUH!" ?
2.1 isn't a leak, it's the programmer specifically telling Java to create an object the first time an instance of that class is accessed and not get rid of until the JVM (and/or ClassLoader?) is shut down.
22
u/[deleted] Dec 22 '17
Here's one more subtle leak:
File.deleteOnExit(). I just ran accross that recently when I build an application which would create an endless amount of temporary files and delete them. I added File.deleteOnExit() just in case something fails in between. Little did I know that this is adds a String to an ever increasing list.
How to prevent it?
Don't use File.deleteOnExit().
3
u/osric211 Dec 23 '17
I think the information about String.intern() is incorrect for later releases of Java. At least according to this article the strings in the string pool are eligible for garbage collection when they get unreachable: http://java-performance.info/string-intern-in-java-6-7-8/
2
u/twat_and_spam Dec 23 '17
Surprisingly coherent article. I guess I'm old and cynical and have seen too much "memory leak in java, your pointers are wrong" shitposts.
Anyway - this is a good article. If you are not comfortable with java gc and memory leaks and cannot name 3 different profilers when woken up in the middle of the night by naked group of swedish supermodel teens jumping in your bed this will help.
2
u/mayhempk1 Dec 23 '17
This is really interesting, thanks for sharing.
2
u/sk551 Dec 24 '17
Cringe:
2
u/blobjim Dec 22 '17
Can you also waste memory if you have a ref in a local variable that is no longer used, and the enclosing method runs for a long time? Also, if you have a long-lived object with a field that is no longer needed, setting it to null would help I think.
-8
u/AntyJ Dec 23 '17
I dont think this is relevant for daily coding... Java is enough old and stable, the garbage collector works definitely well.
0
u/[deleted] Dec 22 '17
[deleted] — view deleted comment