Java Application Memory Usage

I am writing a small Java application (my first!) That does only a few things at the moment. He currently runs the Main class, which runs the gui class (the class I wrote that extends the JFrame, which only contains JTextArea), the class that loads the local file through BufferedInputStream, which is approximately 40kb, and the class that loads the record from Java properties file.

Everything works wonderfully, however, I looked at the Windows task manager and I noticed something that seemed strange to me. When I run the application, the RAM usage is about 40 MB, and it downloads a local file and pulls several values ​​from it to display it in JTextArea, which seems normal to me for JVM, Java base classes, etc. However, however, when the application has finished downloading the file, it just sits idle, because at the moment I have nothing else. While he is sitting idle, while the window is active, the memory usage of the application starts to rise by 10-20kb every second. It seems strange to me. If I click on another program to make it an inactive window, the memory is still growing, but at a much lower speed (about 10 kb every 3-5 seconds).

I have not tested how far it goes, but it seems to me a very strange behavior. Is this normal Java behavior? I think it is possible that my code may be a memory leak, but I'm not sure how to do this. I really have to close the BufferedInputStream that I'm using, and I don't see what else could cause this.

I apologize if my explanation does not make sense, but I would appreciate any insights and / or pointers that anyone might have.

UPDATE:

On a suggestion, I basically turned off my application to the Main class, which simply calls the gui class. The gui class extends the JFrame and sets the window size, close operation, and visible properties. With these changes, the memory is still growing at 10-20 kb, but slower. This, combined with other tips that I received, makes me believe that it is just Java. I will continue to play with him and let you know if I find out anything interesting.

+7
java memory memory-leaks
source share
5 answers

Try monitoring heap usage with jconsole instead of windows task manager:

  • Launch the application with the -Dcom.sun.management.jmxremote parameter, for example.

java -Dcom.sun.management.jmxremote -jar myapp.jar

  • Launch jconsole from the command line and connect to the local pid of the java process that you started in the last step.
  • Go into memory and see heap memory (default display)

If you look at it for a while, you will probably get a “sawtooth” pattern when memory is taken over time, but after that there is a sharp drop when the garbage collector starts. You can try to “suggest” garbage collection by clicking on this button.

When you do this, does memory usage decrease to the same minimum level or does the total minimum increase within a few minutes? If the minimum usage increases, then you have a memory leak. If he always returns to the same minimum level, then you are fine.

+9
source share

Congratulations on your first app! Now about two things to think about. First, the Windows Task Manager is not a great resource to understand how fast your vm is growing. Instead, you should monitor garbage collection statistics in the console (use the -verbose:gc command-line -verbose:gc ). Secondly, if you are concerned about potential leaks and vm growth, there are many great profiles that are easy to use and can help you diagnose memory problems. view these two messages for some profiler options.

+2
source share

Congratulations on your first Java app!

Java applications run in a virtual machine. The virtual machine is assigned a fixed amount of OS memory, typically 512 MB. As long as the application uses less than 512 MB, the garbage collector will not start looking for "dead" blocks of memory. The JVM memory limit can be changed on most operating systems. Try translating the memory limit to 32 MB, for example.

+2
source share

Is this normal Java behavior?

Not.

I think it is possible that my code may be a memory leak

This is definitely the reason. Send the source code, otherwise further diagnosis is not possible.

I noticed that you are using Swing, make sure you run the JFrame in the event dispatch thread using the invokeLater(Runnable) method.

If you use any collections, make sure that you execute them clear .

Since you are executing an IO I / O file, make sure you close all classes involved in I / O after you are done with them.

If you use event listeners, be sure to explicitly remove the event listeners when they are no longer needed.


One thing you could try is experimenting. Take the application and delete the I / O file, see what happens. Is memory usage still in use? Now change your application to normal and delete the text area - is the memory still rising as before? Etc etc. This will help you determine what the source is, and you can focus your efforts there. Most likely, you will find out what you do after that.

Another useful diagnostic tool is to use System.gc() at specific points in time, usually after heavy blocks of code. This will allow the JVM to garbage collect at this point in time, and not at other times determined by memory consumption. This will help you take into account any periodic fluctuations in the memory usage of your application.

Otherwise, you can always use a memory profiler. If you use the NetBeans IDE, there is one built into it. There are several plugins for Eclipse that can perform profiling.

+1
source share

this is normal. some background calculations may leave dead objects around, which the JVM is in no hurry to clean. they will eventually collect garbage when max mem arrives at it.

leave your program overnight and your car will not explode.

+1
source share

All Articles