3.38Managing and Releasing Resources
Created on Aug 17, 2026~1 min read
Refer to sections 3.36 and 3.37 to learn how to release resources at the end of a request to avoid memory leaks.
// Release resources to avoid memory leakshttpRequestBuilder.ReleaseResources();The following is the underlying implementation of the ReleaseResources method, which is responsible for managing and releasing all resources associated with the HTTP request:
public void ReleaseResources(){ // Null check if (HttpClientPooling is not null) { HttpClientPooling.Release?.Invoke(HttpClientPooling.Instance); HttpClientPooling = null; } // Release the collection of disposable objects ReleaseDisposables();}internal void ReleaseDisposables(){ // Null check if (Disposables.IsNullOrEmpty()) { return; } // Iterate and release each item foreach (var disposable in Disposables) { disposable.Dispose(); } // Clear the collection Disposables.Clear();}