Reputation: 447
Consider a process that is running for a very long time in my application, which has an
object. Will calling SuppressFinalize()
ensure that the GC doesn't release the resources
until the end of process?
Upvotes: 1
Views: 2943
Reputation: 168
In short: No, it does something different.
SuppressFinalize()
tells the GC not to call the finalization method of your object, when garbage collected, it does not prevent the object from being garbage collected.
For your scenario, you need to store all your objects somewhere and clean them after your long-running task is completed. Best Practice is to use a DI (dependency injection) framework.
.Net core has an integrated DI framework that will easily do what you want. You would need to register your class as scoped. Create a scope, create your object inside the scope, start your long-running task, dispose the scope after the task is completed.
SuppressFinilize: https://learn.microsoft.com/en-us/dotnet/api/system.gc.suppressfinalize?view=net-6.0
.net core DI: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0
Upvotes: 1