Resource management
Because of memory and CPU limitations mobile applications must use resources efficiently. This topic describes the resource management techniques that you can apply to optimize your resource allocation when using the Universal Tracker SDK.
Resource management for Universal Tracker SDK classes
Most classes and interfaces of the Universal Tracker SDK are pure managed objects. However, the session holds references to native resources, for example, a reference to the HttpClient instance, which uses a native socket.
To properly dispose of unmanaged resources, the session implements the IDisposable interface, which has the following requirements:
-
If you use the session as a local variable, you must declare and instantiate it in a
usingstatement, which calls theDisposemethod in the correct way and causes the session object to go out of scope as soon as the method is called:RequestResponseusing (var session = SitecoreUTSessionBuilder.SessionWithHost("https://mysite.com") .DefaultInteraction(defaultInteraction) .BuildSession() ) { // Send some requests } -
If you store the session as an instance variable of your class, you must implement the
IDisposableinterface and dispose of the session object explicitly:RequestResponsevoid ReleaseResources() { if (null != this.session) { this.session.Dispose(); this.session = null; } } public virtual void Dispose() { this.ReleaseResources(); GC.SuppressFinalize(this); } ~MyClassThatUsesUTSession() { }
To ensure that your application disposes of the HttpClient, wrap the session in a using block. Otherwise, the application may crash with a memory warning, or the Too many open files exception.