Azure / azure-cosmos-dotnet-v3

.NET SDK for Azure Cosmos DB for the core SQL API
MIT License
723 stars 477 forks source link

Set createValueFunc to null after the value being evaluated #4470

Open guiyong opened 1 month ago

guiyong commented 1 month ago

Pull Request Template

Description

Set createValueFunc to null once the Func delegate is not needed. Without breaking the reference, the lambda function will keep capturing of any outer variables, in #4345, DocumentServiceRequest was captured, which prevent GC from recycling the request object, cause memory leak.

Below code, the request object will be captured by singleValueInitFunc delegate.

addresses = await this.serverPartitionAddressCache.GetAsync( 
                         key: partitionKeyRangeIdentity, 
                         singleValueInitFunc: (_) => this.GetAddressesForRangeIdAsync( 
                             request, 
                             cachedAddresses: null, 
                             partitionKeyRangeIdentity.CollectionRid, 
                             partitionKeyRangeIdentity.PartitionKeyRangeId, 
                             forceRefresh: false), 
                         forceRefresh: (_) => false); 

Type of change

Please delete options that are not relevant.

Closing issues

To automatically close an issue: closes #4345