Sling Models field injectors are used to support injection of AEM Library-specific context objects. For example, @ScriptVariable SightlyWCMMode will inject the WCMMode object, @ScriptVariable Resource will inject the current resource object, and @ScriptVariable Style will inject the Style object.
With the Apache Sling Model’s injector specific annotations, we are able to inject AEM Services, Objects, Configuration, etc… directly into the context of the Sling Model easily without much hassle.
What is the catch? Whenever we are injecting objects into our Sling Models via field injection, an instantiated Sling Model adaptable is actually storing a reference of those object in memory; stored injected objects in the Sling Model instance will consume memory.
The Sling Model constructor injection is also supported (as of Sling Models 1.1.0), and its documented that it does not store the reference to the adaptable, lets test this out.
In this article, we will test the Sling Model memory consumption with two scenarios:
1. Test 1: Sling Model Field Injection
2. Test 2: Sling Model Constructor Injection