How To Reduce Spring Memory

I would like to ask you HOW (or IF) you can reduce the size of the Spring memory area.

I created a simple helloworld application to demonstrate the problem. There are only two classes and the context.xml file:

  • Main - class with the main method
  • Test - a class used to simulate some "work" (printig Hello in an infinite loop)

context.xml contains only this:

 <context:component-scan base-package="mypackage" /> 

The test class contains only the init method, called after construction:

 @Component public class Test{ @PostConstruct public void init() { Thread t = new Thread(new Runnable() { @Override public void run() { try { while (true) { System.out.println("Hello " + Thread.currentThread().getName()); Thread.sleep(500); } } catch (InterruptedException ex) { ex.printStackTrace(); } } }); t.start(); } } 

I prepared two scripts, and in both of them the Main method contains only one line.

In the first scenario, the main method does this: (new Test()).init(); The application works without Spring and consumes only aprox. 8 MB of RAM.

In the second scenario, the main method contains the following: new ClassPathXmlApplicationContext(new String[]{"spring/context.xml"}); Thus, the application is initialized by the Spring container and consumes aprox. 45 MB RAM!

Is there a way to reduce (at best, completely get rid of) this extra memory? So far I have not been able to find a suitable solution.

I do not mind if additional memory consumption is added during startup - this is fine, but after that I need our application to reduce it.

(The story behind this question is a little more complicated, but for me this is now the main problem.)

thanks

+7
java spring ram
source share
1 answer

First, a few things that you already know, but are important for understanding the situation: most of the memory that your program uses is a bunch of JVMs. The heap has an initial size when your program starts execution. Sometimes the JVM requests the OS for more memory and increases the size of the heap. The JVM will also perform garbage collection, thereby freeing up space on the heap.

When the size of the heap used decreases, the memory is not necessarily freed by the JVM. The oracle JVM is reluctant to do this. For example, if your program uses 500 MB of RAM at startup, then after garbage collection for most of its execution, only 100 MB is used, you cannot assume that an additional 400 MB will be returned to your OS.

In addition, tools like Windows Task Manager (and I assume unix ps ) displays the size of all memory allocated by the JVM, regardless of whether that memory is actually used or not . Tools like jvisualvm will let you see exactly how memory is used in your java program, and in particular how much heap you actually use and how much is allocated.


With this in mind, I tested your program in the following scenarios. Please note that this depends on many factors, including which JVM you are using, its version, and possibly your OS, too.

  • Standard java (SE) vs Spring.
  • Garbage Collection (GC) vs none (NOGC) (I called the garbage collector from jvisualvm).
  • The minimum heap size defined for the JVM (using -Xmx8M). This tells the JVM that 8 MB will be allocated at startup. The default on my system is 256 MB.

In each case, I report the allocated heap size and heap size. Here are my results:

  • SE, NOGC, 256M: 270 MB allocated, 30 MB used.
  • Spring, NOGC, 256 MB: 270 MB, uses 30 MB

These results are identical, so from your question, I assume that you already had a different environment than me.

  • SE, GC, 256M: 270 MB allocated, 9 MB used

Using GC reduces heap usage, but we still have the same allocated memory

  • SE, NOGC, 8M: 9 MB allocated, <5 MB used
  • Spring, NOGC, 8M allocated: 20 MB, <5 MB used

This is the most important result: more memory is allocated, because Spring will probably need more at some point during startup.

Conclusions:

  • If you are trying to reduce heap usage, using Spring should not be a big problem. Overhead is not gigantic.
  • If you are trying to reduce the allocated memory, the cost of using Spring in this experiment is steeper. But you can still configure your JVM so that it frees memory more often than the default value. I don't know much about this, but jvm options like -XX:MaxHeapFreeRatio=70 can be a launch (here http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html# PerformanceTuning )
+27
source share

All Articles