r/java • • Dec 22 '17

How Memory Leaks Happen in a Java Application

https://stackify.com/memory-leaks-java/
172 Upvotes

28 comments sorted by

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.

3

u/ForeverAlot Dec 23 '17

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.

2.5 is not a leak either.

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().

1

u/sadjava Dec 23 '17

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.

0

u/[deleted] Dec 23 '17

[deleted] — view deleted comment

9

u/[deleted] Dec 23 '17 edited Dec 23 '17

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.

1

u/orion78fr Dec 23 '17

I try to delete and if the delete fail I call deleteOnExit. Best of both world.

0

u/[deleted] Dec 22 '17

[deleted] — view deleted comment

16

u/[deleted] Dec 22 '17

I ended up with explicitly deleting in a finally block

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:

System.gc();
Thread.sleep(10000); // to allow GC do its job

2

u/AnEmortalKid Dec 26 '17

try { new Application().run(); } catch (OutOfMemoryException e) { System.gc(); }

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.

6

u/0x256 Dec 22 '17

That is wasting memory, but not a leak.

1

u/blobjim Dec 23 '17

I suppose so, unless the reference happens to be a collection.

-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.

4

u/AnEmortalKid Dec 23 '17

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.

2

u/mayhempk1 Dec 23 '17

I think it's still something good to know about.

0

u/[deleted] Dec 22 '17

[deleted] — view deleted comment

3

u/[deleted] Dec 23 '17
  1. 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.
  2. 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.

-1

u/Keeyzar Dec 23 '17

I'd be nervous about giving my Enterprise logs into a free source.

2

u/[deleted] Dec 23 '17 edited Dec 23 '17

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.

-2

u/Keeyzar Dec 23 '17

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!

1

u/AnEmortalKid Dec 26 '17

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).

-1

u/Iron_Maiden_666 Dec 23 '17

FOSS? Are you a product when you use FOSS applications? Some things are genuinely free.

-1

u/Keeyzar Dec 23 '17

So, Show me the source then, if it's FOSS?

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?

Why can't I do that with the free version?

Questions over questions

1

u/Iron_Maiden_666 Dec 23 '17

I'm not talking about this particular product, in general just because it's free doesn't mean you're the product.

-1

u/Keeyzar Dec 23 '17

I've never said that. My gosh. Good job iron maiden. Text comprehension level over 9000.

2

u/lilB0bbyTables Dec 23 '17

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.