HostingEnvironment instantiates PhysicalFileProvider which is later used by components like Razor Pages to watch for file changes. This app reproduces the issue where we do not dispose the HostingEnvironment instance on TestServer shutdown causing memory to increase rapidly. HostingEnviroment instance does not currently implement IDisposable.
User has a web application and some functional tests for it. He makes a change to razor pages file, saves it and runs the tests again.
NOTE: The tests in this scenario create & dispose the TestServer for every test. i.e 100 tests here create 100 instances of it. The memory issue does NOT repro if the TestServer is created only once and shared by all tests, which is more common. You can checkout the branch 'WithTextFixture' to test that scenario too.
- Open the FileSystemWatcherRepro.sln
- Build the solution and run the tests in WebApp.Test. This time tests should run quickly.
- Make a change(like edit the Title html tag) in a razor page under WebApp\Pages\Foo.cshtml and save the file.
- Run the tests again and now you should see(ex: TaskManager) the memory increase rapidly.
Each new instance of PhysicalFileProvider creates a new instnace of FileSystemWatcher (on calling Watch on the fileprovider). This appears to create pinned handles by the FileSystemWatcher. Since we run like, 100 tests, this creates lot of pinned handles and since pinned handles cause heap fragmentation, the size of memory keeps increasing.
Uncomment the code in Dispose method of the test. This would dispose the file providers hanging off of HostingEnvironment and now the tests will run fine.