6

From the question Creating a memory leak with Java there are many ways to create memory leaks in Java, but I am just trying to create a simple memory leak and being able to observe its effect somehow. In fact, after trying to create a memory leak I would like to verify that ( in numbers or something) so that I assure that the memory leak has been successfully created. Can we investigate the occurrence of memory leak in our code by not using a third party application, just by inserting e.g. runtime.maxMemory() in our code?

Community
  • 1
  • 1
C graphics
  • 6,750
  • 18
  • 75
  • 124
  • To you consider [VisualVM](http://visualvm.java.net/) that is part of the JDK (since 6 if I'm not mistaken) as a third party tool/application? – Jonathan Drapeau Aug 29 '14 at 19:23

2 Answers2

4

This is a poor-man's substitute for a memory profiling tool:

public static long bytesOccupied() {
   final Runtime rt = Runtime.getRuntime();
   for (int i = 0; i < 2; i++) rt.gc();
   return rt.totalMemory()-rt.freeMemory();
}

I have used this on several occasions and can attest that it works at least on some setups. Try it and see if it gives sensible results for you.

Otherwise, it is really easy to start VisualVM, which is already installed on your computer with the JDK. Be sure to install the optional VisualGC plugin. Make your code do some allocations in a loop and watch the GC generations churn. Use a short sampling time (100 or 200 ms) to get a realtime feel.

Marko Topolnik
  • 179,046
  • 25
  • 276
  • 399
  • So the key to create memory leak was calling rt.gc() twice? Can you explain how your code works - Thanks (cuz unless I added this( char [] ch= new char[100000000]; String st= new String(ch);) prior to calling your code I could not generate memory leak. – C graphics Aug 29 '14 at 19:57
  • The code in my answer doesn't pretend to be creating a memory leak; it is a method to *measure and detect* a memory leak. You'll need that same code no matter what technique to create a memory leak you want to test out. – Marko Topolnik Aug 29 '14 at 20:15
  • So why the rt.gc() is placed there? – C graphics Aug 29 '14 at 20:24
  • 1
    To make your measurement accurate. Otherwise memory occupancy will show part live, part dead objects, and dead objects by definition do not contribute to a memory leak. – Marko Topolnik Aug 29 '14 at 20:26
0

Non-memory-profiling way of dealing with memory leak:

First Step

Open the project in IntelliJ. Configure error warnings. With any class of the project opened, use the upper-right pop-up window of code inspectons warnnings to access the "Configure Inspections" button.

Inside the new tab, at the group "Memory" and "Resource Management", change their severity level to "error".

Perform analysis:

 Menu Analize > Inspect Code.

This will open a "Problems" View Tab at the bottom of the windom.

Check those errors we changed severity before. Make a list of all of them. Analise if they make sence, or if they can be ignored. (this may take some time)

Make a list of all of them.

Delete from the list any error that is not a good candidate for fixing.

Second Step

Open the project in Eclipse. Configure error warnings.

Menu Window > Preferences > Java > Compiler > Error/Warnings

Inside the new tab, mark all options in "Potencial programming problems" as errors.

In order to perform a analysis, clean up the previous build and make another one. Eclipse will perform the analysis. (make sure automatic build is set "on" at Project menu)

Menu Source > Clean up...

This will open a "Problems" View Tab at the bottom of the windom. You may copy and paste this "report" from Eclipse to your list, but make visual adjustiments.

Check those errors we changed severity before. Include them in the list. Analise if they make sence, or if they can be ignored. (this may take some time)

Delete from the list any error that is not a good candidate for fixing.

Third Step

If you realize any extra erros lurking inside any class while you hunt for the more obvious ones, add them to the list. Search them like you search for "streams", as bellow.

Make a final search round. Use:

Ctrl+Shift+F (in IntelliJ)

Search Module after Module for "import ...bad class...", "stream" (must do), or any new subtle problem you may have discovered.

Using Module as your unit of search will get you better organized than the other sorting options and will make you move faster through the project.

Finally, fix all the problems in the list. They may be try/catch/finally, try-with-resources, or some other king of solution.