runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Multiple restart of runtime host within the same process

Open AmeetShinde opened this issue 8 years ago • 8 comments

We have requirement to restart the runtime host within the same process. We are using following sequence of calls with ICLRRuntimeHost4 interface as listed in https://docs.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting

  1. Load coreclr library and get m_runtimehost (of type ICLRRuntimeHost4) by calling GetCLRRuntimeHost
  2. m_runtimehost->Start()
  3. m_runtimehost->CreateAppDomainwithManager() => using SINGLE_APP_DOMAIN
  4. m_runtimehost->CreateDelegate()
  5. Call delegates
  6. m_runtimehost->UnloadAppDomain2()
  7. m_runtimehost->Stop()
  8. Unload the coreclr library

When we try to repeat the above sequence without restart of the process, we get access violation exception at step 2 above with following call stack

    coreclr.dll!SystemDomain::LoadBaseSystemClasses() Line 2715	C++
coreclr.dll!SystemDomain::Init() Line 2460	C++
coreclr.dll!EEStartupHelper(tagCOINITEE) Line 1058	C++
coreclr.dll!EEStartup(tagCOINITEE) Line 1237	C++
coreclr.dll!EnsureEEStarted(tagCOINITEE) Line 375	C++
coreclr.dll!CorHost2::Start() Line 189	C++
  1. Is this known issue? Is there any workaround to restart the runtime host multiple times without restarting the process? We see similar issue with Linux hosts throwing exception in coreclr_initialize method.
  2. We are also seeing managed assemblies not being unloaded with UnloadAppDomain2 call mentioned in the step 6 above. Also we expect memory to be released when we call m_runtimehost->Stop() mentioned in the step 7 above.

AmeetShinde avatar May 28 '18 04:05 AmeetShinde

Restarting runtime within the same process is not supported. Issues dotnet/runtime#8350 and dotnet/runtime#9296 have discussion about this.

jkotas avatar May 28 '18 15:05 jkotas

Are there any plans to support this in near future? This is one of the critical requirement for us. Also why unloading app domain does not release managed assemblies?

The related question wrt runtime host API is controlling managed memory usage - mscoree API provides interfaces to limit the managed memory or invoke GC on demand, is this functionality available on all platforms (are there similar interfaces available with platform neutral API coreclr or if there is any other way to achieve the same effect).

AmeetShinde avatar May 29 '18 03:05 AmeetShinde

We do not have plans to support full runtime unloading. We have plans to support assembly unloading (#552).

jkotas avatar May 29 '18 04:05 jkotas

interfaces to limit the managed memory

The managed memory limits are set based on the environment settings today (amount of physical memory, Windows job object memory limit, or Docker container memory limit). Look for GCToOSInterface::GetPhysicalMemoryLimit to see the details. If you think it would be useful to have it exposed via hosting API as well, please open an issue on it.

invoke GC on demand

You can do that via the managed GC.Collect API.

jkotas avatar May 29 '18 04:05 jkotas

Thanks. Just to confirm, are you saying ICLRGCManager->SetGCStartupLimits and ICLRGCManager->Collect is not functional currently.

Also do yo know 1. if there are any known issues in .Net core with respect to managed memory usage (across GC generations) increasing (or leaking/fragmentation) during the course of long running process and 2. if there is any way to reclaim that memory.

AmeetShinde avatar May 29 '18 08:05 AmeetShinde

The code these ICLRGCManager APIs happens to be still compiled in, but it is not nested. I would not recommend using it.

ICLRGCManager::SetGCStartupLimits just gives hint to the GC about the initial segment size. It does not limit the manage memory that can be used by the process.

Issues in .Net core with respect to managed memory usage

This is too broad question. Yes, some programs run into managed memory usage issues. The root cause and solution is different for each case. Most of the time, these issues are bugs in the programs, not issues in .NET Core. http://github.com/microsoft/perfview is the best too we have to analyze issues like these.

jkotas avatar May 29 '18 14:05 jkotas

Thanks for providing detailed answer. One more thing regarding managed memory issue - what we have noticed so far is that when our application runs for longer duration and loads large sized files (the application uses OfficeOpenXML SDK to retrieve text/attachments/images from office open xml documents), the memory start accumulating in various GC generations and doesn't come down. My specific question is how to control or release this memory over period of application run.

AmeetShinde avatar May 31 '18 06:05 AmeetShinde

Issues https://github.com/dotnet/coreclr/issues/11338 and related is about this problem.

jkotas avatar May 31 '18 14:05 jkotas