dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.99k stars 4.67k forks source link

Proposed API : Make it convenient and feasible to unload the domain. #53493

Open NMSAzulX opened 3 years ago

NMSAzulX commented 3 years ago

Background and Motivation

I have encountered some thorny situations that cannot be uninstalled:

  1. Dynamically generating Controller. in Asp.netCore.
  2. A class library that uses AppDomain events.
  3. Use Autofac as the WEB framework of DI.

Can the official provide a method that allows an object to be marked if it is referenced? When GC occurs, the object is forcibly recycled. (remove reference). Or forcibly empty the memory where it is located.

Since the uninstall is manual, developers should know when to use this API.

Proposed API

defaultDomain.Foo = myDomain.Bar;
myDomain.Bar.Collect( CollectionOptions.NextGC );

//GCing.......
Assert.IsDefault(defaultDomain.Fool); // true

Usage Examples

Alternative Designs

Risks

Although this is a bit inappropriate, I would like to talk about the future Internet architecture will be dominated by dynamic logic and configuration files.

dotnet-issue-labeler[bot] commented 3 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

svick commented 3 years ago

I don't understand what you're asking for. If you're talking about unloading app domains, then .Net Core/.Net 5+ has only one, so it doesn't make sense to unload that. If you want to unload an assembly, you can use AssemblyLoadContext for that. Have you looked into that?

Suchiman commented 3 years ago

@svick i think what they're asking is an API to force free ("GC") an object that is otherwise being kept alive by being subscribed to event handlers (on AppDomain specifically).

svick commented 3 years ago

If you're asking for a way to forcibly GC an object, can you explain why exactly do you need that and why other alternative approaches are not sufficient?

svick commented 3 years ago

@Suchiman In that case, I would ask: Why can't you unsubscribe that handler? If it's because code that you need and can't modify does something that you don't want, then I would suggest the usual .Net approach for that kind of situation: use reflection.

huoyaoyuan commented 3 years ago

Strongly listening static event is a common pattern of managed memory leak. In this case they should be adjusted to use weak event pattern.

NMSAzulX commented 3 years ago

@svick Yes, I aready use ALC long long ago.

NMSAzulX commented 3 years ago

@svick @Suchiman https://github.com/dotnet/runtime/issues/13323#issuecomment-535937611

Suchiman commented 3 years ago

@NMSAzulX the code to hack this with reflection would look something like

var pExit = typeof(AppContext).GetField(nameof(AppContext.ProcessExit), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
var del = ((Delegate)pExit?.GetValue(null))?.GetInvocationList().FirstOrDefault(x => x.Method.Name == "EnsureClearingPools");
if (del != null)
{
    AppContext.ProcessExit -= (EventHandler)del;
}

and then the same again for DomainUnload

NMSAzulX commented 3 years ago

@Suchiman Thank you! And https://github.com/dotnet/aspnetcore/issues/21906 to asp.net core.

ghost commented 3 years ago

Tagging subscribers to this area: @dotnet/gc See info in area-owners.md if you want to be subscribed.

Issue Details
## Background and Motivation I have encountered some thorny situations that cannot be uninstalled: 1. Dynamically generating Controller. in Asp.netCore. 2. A class library that uses AppDomain events. 3. Use Autofac as the WEB framework of DI. Can the official provide a method that allows an object to be marked if it is referenced? When GC occurs, the object is forcibly recycled. (remove reference). Or forcibly empty the memory where it is located. > Since the uninstall is manual, developers should know when to use this API. ## Proposed API ```C# defaultDomain.Foo = myDomain.Bar; myDomain.Bar.Collect( CollectionOptions.NextGC ); //GCing....... Assert.IsDefault(defaultDomain.Fool); // true ``` ## Usage Examples ## Alternative Designs ## Risks > Although this is a bit inappropriate, I would like to talk about the future Internet architecture will be dominated by dynamic logic and configuration files.
Author: NMSAzulX
Assignees: -
Labels: `api-suggestion`, `area-GC-coreclr`, `untriaged`
Milestone: -
Maoni0 commented 3 years ago

changing the label to VM - it would be up to the VM to remove references to objects if it wants objects to be considered reclaimable by the GC (without causing any functional problems).

NMSAzulX commented 2 years ago

Is this issue already being processed?

NMSAzulX commented 2 years ago

@Maoni0 How's the progress? :)

danmoseley commented 2 years ago

I think you mean @mangod9