3

I'm writing a Java/Swing application with ~30 class my probleme is when i run my programe it load more than 150 M of the memory, is that normal ? since the application have 4 threads, parse some XML files, load some icon file, and drow some Jfreechat charts. if not how can i do to minimize the amount of memory used by the application, is affecting some variables to null help? is loading the XML files once to use them in all the application life cycle help or i have to load them evry time i need them? is there some other tips that help me?

PS: im devlopping with a 8G memory computer in case that can affect the memory used by my program.

EDIT: it appeared that the program don't occupy all the 150MB because i get this value from the top command on linux, by running this code in my application as vilmantas advises me:

   long free = Runtime.getRuntime().freeMemory();
   long total = Runtime.getRuntime().totalMemory();
   long max = Runtime.getRuntime().maxMemory();
   long used = total - free;

I found that he occupy much less than that (~40MB) so i decide to run it with "-Xmx40M" argument and i reduce more than 40% of memory usage in the Top command. The problem who are occupying the rest of memory since JVM (as i know) have his own process ? and how to make this operation automatic**?** because when choosing a not appropriate value you can get a memory exception as i have by running with "-Xmx30M" argument:

   Exception in thread "Thread-2" java.lang.OutOfMemoryError: Java heap space
Community
  • 1
  • 1
imanis_tn
  • 1,150
  • 1
  • 12
  • 33

4 Answers4

4

It is. This is Java, usually your VM/GC will do the job for you. Worry about memory usage when and if it becomes a problem.
If you want, there are several tools that can help you analyze what is going on. How to monitor Java memory usage?

Community
  • 1
  • 1
unexpectedvalue
  • 6,079
  • 3
  • 38
  • 62
  • 1
    +1 You can use VisualVM or a commercial profiler to see how much memory is being used by what. This is the best way to discover what you might change to reduce usage. – Peter Lawrey Apr 18 '12 at 11:14
3

Setting variables to null can help preventing memory leaks, if the referring variable's life cycle is greater than the referred instance. So that variables that should hold-on through the whole application life cycle are better not hold references to temporary objects that are used for a short time.

Loading the XMLs only once can help if you're good with loading its information only once. Meaning, if the XML is changed otherwise than through your application and you need to get the update - you'll have to reload the XML (and if the deprecated XML info is no longer needed - get rid of it).

yair
  • 8,945
  • 4
  • 31
  • 50
  • Setting variabls to null dos call the GC to do his job? Or just clean the reference and waiting the end of block? – imanis_tn Apr 18 '12 at 11:09
  • +1 Its rarely needed but it can mean you don't retain objects you don't need. It doesn't make the GC any more likely to run. – Peter Lawrey Apr 18 '12 at 11:13
  • And my Xml fiels are static so no need to reload but the problem is that im not sure that i will use them all the application live cycle that depend the user input and actions, and the big problem is that they size more than3M (2 files) – imanis_tn Apr 18 '12 at 11:13
  • 1
    3M in a 8G machine isn't that much. 3MB is worth about 2 cents of memory and its reusable. – Peter Lawrey Apr 18 '12 at 11:16
  • 1
    While setting to null is a good advice, please don't follow it blindly and set variables to null only if there is a reason to. Read this and don't try to outsmart your GC: http://stackoverflow.com/questions/627784/what-are-some-java-memory-management-best-practices – unexpectedvalue Apr 18 '12 at 11:19
  • @ssteinberg this is agreed. My answer refers explicitly to long-living objects redundantly holding on to short-living objects. – yair Apr 18 '12 at 11:51
2

You could use java memory heap analyzer like http://www.eclipse.org/mat/ to identify the parts of your application that use up most of the memory. You can then either optimize your data structures, or decide release parts of the data by setting all references to it to null.

Unintended references to data that is not needed anymore are also refered as "memory leaks". Settings those references to null will cause the garbage collector to remove it from java memory heap.

Along that line, you might find WeakReferences helpful.

sulai
  • 5,204
  • 2
  • 29
  • 44
1

Where do you observe those 150M? Is that how much your JVM process occupies (e.g. visible in the top command on linux/unix) or is it really the memory used (and necessary) by your application?

Try writing the following 4 values when your application runs:

    long free = Runtime.getRuntime().freeMemory();
    long total = Runtime.getRuntime().totalMemory();
    long max = Runtime.getRuntime().maxMemory();
    long used = total - free;

If the value for "used" is much lower than 150M, you may add java start parameter e.g. "-Xmx30M" to limit the heap size of your application to 30MB. Note that the JVM process will still occupy a little bit more than 30MB in such case.

The memory usage by JVM is somewhat tricky.

Vilmantas Baranauskas
  • 6,596
  • 3
  • 38
  • 50
  • Yes i observer this value from the top command, i run your code and i get values from 40MB to 80MB or more, i guess so that the rest is used by the JVM. I tried to run the application with the argument -Xmx30M and it give me a memory exception "Exception in thread "Thread-2" java.lang.OutOfMemoryError: Java heap space" and with the argument -Xmx40M it just work fine and the process occupy now ~80MB. the problem here that i have always to check the memory used to give the appropriate argument, there is now automatic solution? – imanis_tn Apr 18 '12 at 14:21