从 InMemory 数据库中删除数据时内存使用量不会减少

Memory usage does not decrease when removing data from InMemory Database

我在使用 InMemory 数据库的 ASP.Net-Core 中创建了示例 Web API 项目,它显示出奇怪的行为,即从数据库中删除它时内存使用没有改变(减少)。

我在TaskManager(taskmgr.exe)中查看“.Net Core Host”进程确认,但找不到原因。

我用swagger测试过,下面是我的示例代码。 请告诉我我哪里错了或误会了。

[开发环境]

    Asp.Net Core(2.2.0)
    EntityFrameworkCore.InMemory(2.2.4)
    Swashbuckle.AspNetCore(4.0.1)

[ValuesController.cs]

    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private MyDbContext _context;

        public ValuesController(MyDbContext context)
        {
            _context = context;
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id)
        {
            // Create about 200 MB Data of string
            var result = new Result()
            {
                Id = id,
                Data = new string('*', 100 * 1024 * 1024)
            };

            // Push 200 MB Data to InMemoryDB and save changes
            _context.Result.Add(result);
            _context.SaveChanges();
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            // find specific id result
            var result = _context.Result.Find(id);

            // if found, remove it and save changes
            if (result != null)
            {
                // ** Memory usage not change when removing **
                _context.Result.Remove(result);
                _context.SaveChanges();
            }
        }
    }

[MyDbContext.cs]

    public class MyDbContext : DbContext
    {
        /// <summary>
        /// DB context
        /// </summary>
        /// <param name="options"></param>
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {
        }

        /// <summary>
        /// Data set of Some Result
        /// </summary>
        public DbSet<Result> Result { get; set; }
    }

    /// <summary>
    /// Contains search result
    /// </summary>
    [Table("result")]
    public class Result
    {
        /// <summary>
        /// ID
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Data
        /// </summary>
        public string Data { get; set; }
    }

[Program.cs]

    public class Program
    {
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var context = services.GetRequiredService<MyDbContext>();
            }

            host.Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

[Startup.cs]

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddDbContext<MyDbContext>(opt =>
               opt.UseInMemoryDatabase("TestMemoryDb").UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v0.0.1", new Info
                {
                    Title = "InMemoryDBTest",
                    Version = "v0.0.1",
                    Description = "InMemoryDBTest",
                });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v0.0.1/swagger.json", "InMemoryDBTest V0.0.1");
                c.RoutePrefix = string.Empty;
            });
            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }

vishwas-trivedi 确认 GC 在强制调用时确实收集了未使用的内存。 我会关闭这个问题。 谢谢vishwas-trivedi

https://github.com/aspnet/EntityFrameworkCore/issues/16398