Skip to content

how to keep assemblyloadcontext delay gc collected after call unload method #113115

Answered by AustinWise
srxqds asked this question in General
Discussion options

You must be logged in to vote

I see two more problems with the UnloadSentinel class as written:

  • the WeakReference class it is using gets finalized and cleared before the UnloadSentinel class. So UnloadSentinel is detecting the unload early. In general it is recommended to not use other managed classes in a finalizer, as the order of finalization is not guaranteed.
  • Once the above is fixed, the alc primary constructor variable keeps the ALC alive.

Here is a test program and a version of UnloadSentinel that solves these two problems.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;

CreateUnloadSentinel();

while (true)
{
    GC.Collect();
    GC.WaitForPendingFi…

Replies: 6 comments 11 replies

Comment options

You must be logged in to vote
11 replies
@AustinWise
Comment options

@srxqds
Comment options

@srxqds
Comment options

@AustinWise
Comment options

Answer selected by srxqds
@srxqds
Comment options

@AustinWise
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants