Asp.net core 从InMemory数据库中删除数据时,内存使用率不会降低
我在ASP.Net-Core中创建了一个使用InMemory数据库的示例Web API项目,它显示了一种奇怪的行为,即当从数据库中删除它时,内存使用率并没有改变(减少) 我通过观察TaskManager(taskmgr.exe)中的“.Net核心主机”进程进行了确认,但找不到原因 我通过swagger进行了测试,下面是我的示例代码。 请告诉我我弄错了什么 [开发环境]Asp.net core 从InMemory数据库中删除数据时,内存使用率不会降低,asp.net-core,in-memory-database,Asp.net Core,In Memory Database,我在ASP.Net-Core中创建了一个使用InMemory数据库的示例Web API项目,它显示了一种奇怪的行为,即当从数据库中删除它时,内存使用率并没有改变(减少) 我通过观察TaskManager(taskmgr.exe)中的“.Net核心主机”进程进行了确认,但找不到原因 我通过swagger进行了测试,下面是我的示例代码。 请告诉我我弄错了什么 [开发环境] Asp.Net Core(2.2.0) EntityFrameworkCore.InMemory(2.2.4)
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; }
}
公共类MyDbContext:DbContext
{
///
///数据库上下文
///
///
公共MyDbContext(DbContextOptions):基本(选项)
{
}
///
///某些结果的数据集
///
公共数据库集结果{get;set;}
}
///
///包含搜索结果
///
[表(“结果”)]
公开课成绩
{
///
///身份证
///
公共int Id{get;set;}
///
///资料
///
公共字符串数据{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>();
}
公共类程序
{
公共静态void Main(字符串[]args)
{
var host=CreateWebHostBuilder(args.Build();
使用(var scope=host.Services.CreateScope())
{
var services=scope.ServiceProvider;
var context=services.GetRequiredService();
}
host.Run();
}
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup();
}
[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();
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext(opt=>
opt.UseInMemoryDatabase(“TestMemoryDb”).UseQueryTrackingBehavior(QueryTrackingBehavior.NotTracking));
services.AddSwaggerGen(c=>
{
c、 SwaggerDoc(“v0.0.1”),新信息
{
Title=“InMemoryDBTest”,
Version=“v0.0.1”,
Description=“InMemoryDBTest”,
});
});
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅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
vishwas trivedi确认GC在强制调用时确实会收集未使用的内存。 我将结束这个问题。 谢谢vishwas trivedi
首先,.NET中的字符串以UTF-16编码,即每个字符两个字节。感谢您的指点。关于100MB分配大小的问题是我的误解。我将编辑描述。首先,.NET中的字符串是用UTF-16编码的,或者每个字符两个字节。谢谢您的指点。关于100MB分配大小的问题是我的误解。我将编辑描述。