How the compile time auto-wiring in micronaut framework improve memory usage

268 Views Asked by At

I am coming from a Spring background. Recently we started using micronaut for our project. I understand that it uses compile time DI. How does it improve the overall memory footprint of the container ?

I took a look at the Application.class generated in the target folder. When I follow the trail, it seems like the one shown below -

run => Micronaut.run(Application.class, args); => applicationContext.findBean(EmbeddedApplication.class);

At this step it will scan for the beans and add them to the array Class[] and call the .start() on each embeddedApplication.

The beans are generated at compile-time -

enter image description here

When all the wiring is complete, shouldn't spring and micronaut consume the same amount of memory within the jvm ?

1

There are 1 best solutions below

0
saw303 On

Long story short. From the Micronaut documentation.

Micronaut uses the ASM bytecode library to generate classes, and because Micronaut knows ahead of time the injection points, there is no need to scan all of the methods, fields, constructors, etc. at runtime like other frameworks such as Spring do. Also, since reflection is not used when constructing the bean, the JVM can inline and optimize the code far better, resulting in better runtime performance and reduced memory consumption. This is particularly important for non-singleton scopes where application performance depends on bean creation performance.

As mentioned in the documentation the reduced memory consumption is achieved by not using any reflection and runtime proxies. This is a massive improvement, especially when it comes to larger applications, since there are now proxy and reflection object needed that blow up your heap.