Resource management

Version: 10.4

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 using statement, which calls the Dispose method in the correct way and causes the session object to go out of scope as soon as the method is called:

    RequestResponse
    using (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 IDisposable interface and dispose of the session object explicitly:

    RequestResponse
    void ReleaseResources()
    {
      if (null != this.session)
      {
        this.session.Dispose();
        this.session = null;
      }
    }
    
    public virtual void Dispose()
    {
      this.ReleaseResources();
      GC.SuppressFinalize(this);
    }
    
    ~MyClassThatUsesUTSession() 
    {
    }
Note

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.

Do you have some feedback for us?

If you have suggestions for improving this article,