2

I'm using Java persistence API to develop a standalone software. Recently I saw that the memory usage keep rising when I'm creating objects from entity classes, as well as JPAController classes. It seems that the objects stays at the memory since the memory allocation to the project won't decrease (Eg: 400mb ---> Create Object ---> 450mb ---> Stays at 450mb). Will this affect badly on performance? Should I call System.gc() method to remove these objects?

Neil Stockton
  • 10,531
  • 3
  • 28
  • 27
Thanuj
  • 129
  • 2
  • 11
  • How do you measure memory allocation? – biziclop Oct 14 '15 at 10:39
  • 1
    No, there's practically never a reason to call `System.gc();` and it won't solve any problems you're going to encounter. – Kayaman Oct 14 '15 at 10:41
  • @biziclop I use windows Task Manager, I can see the memory allocation under Java(TM) Platform Binary – Thanuj Oct 14 '15 at 10:42
  • Short response: no. Long response: no, you shouldn't – Fran Montero Oct 14 '15 at 10:45
  • 1
    @Thanuj Okay, in that case the answer is that the Java VM will allocate virtual memory from the OS when needed but will rarely (if ever) give it back. This mechanism is completely unrelated to what happens **within** the Java heap, which is where garbage collection occurs, and unless you see long delays or your application fails with an out of memory error, there is no reason for you to worry about it at all. – biziclop Oct 14 '15 at 10:46
  • 1
    And if you want to monitor heap allocation (which is useful when you do have problems with running out of memory or very long pauses), you can use [`jvisualvm`](http://docs.oracle.com/javase/6/docs/technotes/guides/visualvm/index.html) – biziclop Oct 14 '15 at 10:49
  • Of interest to read: http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf – Gimby Oct 14 '15 at 11:22

3 Answers3

5

Generally System.gc() is not guarenteed to perform a garbage collection. Ultimately it is up to the JVM to decide. See the javadoc.

Have you observed what happens when you are approaching your memory limits of the JVM, does garbage collection happen then ? If not and you receive an OutOfMemoryError, you either are retaining something longer than you need to, or actually need extra heap allocated to your VM.

In anycase System.gc() I believe shouldn't be used to solve such problems.

Donald_W
  • 1,667
  • 18
  • 34
Damien O'Reilly
  • 832
  • 5
  • 12
  • @Damien Yeah, I realized that using System.gc() actually slowing down some processes. Thanks for the answer. :) – Thanuj Oct 14 '15 at 10:55
4

In my opinion, the approach to the problem should be different. Actually the call to System.gc() is not a guarantee that it will free any memory at all; please see When does System.gc() do anything

If you can measure a problem in your memory allocation, either via jconsole, or making a post mortem analysis on the jvm dump, or whatever, then this is another problem. By gathering this information you will know what remains where in your memory regions, and then take actions in order to contain it.

Community
  • 1
  • 1
Jorge_B
  • 9,144
  • 2
  • 14
  • 22
1

The only way that this would negatively affect performance throughout the life of your program is if you want to keep these entities around forever but the size of your old generation in your heap is less than the 450MB you specified. Assuming that you are want to keep around between 1 and 2 times the 450MB you have you have specified forever, with the default ratios of the JVM, setting a parameter such as -Xmx2g will probably be fine. There are many more parameters to fine tune your performance much more than that, but that's probably all the complexity you're looking for for now. If you want to check out some more details on heap tuning and really get into performance, check out this doc on Garbage Collection Tuning by Oracle. Alternatively, something to eat lunch to is a great Youtube video on GC tuning by a guy named Gil Tene.

But calling System.gc() probably won't do anything useful.

entpnerd
  • 8,063
  • 5
  • 36
  • 60