The project used Coldairarrow/EFCore.Sharding: Database Sharding For EFCore (github.com) This component , At first, it was used because the sub table was well made .
Because the project has been developing and testing , So there is a service memory has been soaring , Just restart , I got up after a long time . I have to say that Microsoft documents are really awesome
dotnet-counters Diagnostic tools - .NET CLI | Microsoft Docs
I first read this article , Without it , I won't go deep into Microsoft's diagnostic tools .
The picture above shows System.Diagnostics.StackFrame The memory consumption is the highest , All the output information is analyzed later , It is found that the database outputs a lot of information ,... Locate that the sub table cannot be released, resulting in .
By downloading the sample code :
using EFCore.Sharding;
using EFCore.Sharding.Tests;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks; namespace Demo.DateSharding
{
class Program
{
static async Task Main(string[] args)
{
DateTime startTime = DateTime.Now.AddMinutes(-5);
ServiceCollection services = new ServiceCollection();
// Configuration initialization
services.AddLogging(x =>
{
x.AddConsole();
});
services.AddEFCoreSharding(config =>
{
config.SetEntityAssemblies(typeof(Base_UnitTest).Assembly); // Add data sources
config.AddDataSource(Config.CONSTRING1, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.SqlServer); // By minute
config.SetDateSharding<Base_UnitTest>(nameof(Base_UnitTest.CreateTime), ExpandByDateMode.PerMinute, startTime);
}); var serviceProvider = services.BuildServiceProvider();
new EFCoreShardingBootstrapper(serviceProvider).StartAsync(default).Wait(); for (int i = 0; i < 2000; i++)
{
using var scop = serviceProvider.CreateScope();
var db = scop.ServiceProvider.GetService<IShardingDbAccessor>();
var logger = scop.ServiceProvider.GetService<ILogger<Program>>();
await db.InsertAsync(new Base_UnitTest
{
Id = Guid.NewGuid().ToString(),
Age = 1,
UserName = Guid.NewGuid().ToString(),
CreateTime = DateTime.Now
});
DateTime time = DateTime.Now.AddMinutes(-2);
var count = await db.GetIShardingQueryable<Base_UnitTest>()
.Where(x => x.CreateTime >= time)
.CountAsync();
logger.LogWarning(" Current data volume :{Count}", count);
}
Console.ReadKey(); }
}
}
using var scop = serviceProvider.CreateScope() If you create more than once , This memory continues to rise , Don't release .
If you move this line of code out , Memory will be stable . Although inherited idisposiable , It was used using But it still won't release , It's a headache . But if the following code is transformed into the above one, there will be no memory guarantee .
using EFCore.Sharding;
using EFCore.Sharding.Tests;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks; namespace Demo.HelloWorld
{
class Program
{
static async Task Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
services.AddLogging(config =>
{
config.AddConsole();
});
services.AddEFCoreSharding(config =>
{
config.SetEntityAssemblies(typeof(Base_UnitTest).Assembly); config.UseDatabase(Config.SQLITE1, DatabaseType.SQLite);
});
var serviceProvider = services.BuildServiceProvider();
new EFCoreShardingBootstrapper(serviceProvider).StartAsync(default).Wait(); using var scop = serviceProvider.CreateScope();
// Get the injection IDbAccessor You can perform all database operations
var db = scop.ServiceProvider.GetService<IDbAccessor>();
var logger = scop.ServiceProvider.GetService<ILogger<Program>>();
while (true)
{
await db.InsertAsync(new Base_UnitTest
{
Age = 100,
CreateTime = DateTime.Now,
Id = Guid.NewGuid().ToString(),
UserId = Guid.NewGuid().ToString(),
UserName = Guid.NewGuid().ToString()
});
var count = await db.GetIQueryable<Base_UnitTest>().CountAsync(); logger.LogWarning(" Current quantity :{Count}", count); await Task.Delay(1000);
}
}
}
}
It turned out IDbAccessor It was created through the factory , Don't pass service Inject . however IShardingDbAccessor It's through services.AddScoped<IShardingDbAccessor, ShardingDbAccessor>() Injected into the service .
The solution to the project is to put
services.AddScoped<IShardingDbAccessor, ShardingDbAccessor>() Change to simple interest injection .
Record netcore More related articles about the pit of a memory explosion
- MySQL information_schema Table queries cause memory to soar
case: The next one sql sentence , Lead to mysql Instance memory skyrocketed : select * from tables where table_name not in(select table_name from p ...
- windbg Analyze the memory explosion caused by a big query
There was a feedback question on the project , It's in the production environment , During the normal use of users , There was a sudden burst of server memory , The customer is a little flustered , Want to find out why . Be reasonable , If the memory is slowly rising and never released , There should be a memory leak , This kind of investigation is more difficult , ...
- Remember a .NET A top three hospital HIS System Analysis of memory explosion
One : background 1. Tell a story A few days ago, a friend added wx He said that his program suffered a memory explosion , How to analyze ? Talk to this friend , This dump Also from one HIS System , As my friend said, I'm really tied up with the hospital , So it is better to , Save some resources for yourself , ...
- ARC A solution to deal with the memory explosion
There is a situation : ; i < ; i++) { NSString *s = @"ABC"; s = [s lowercaseString]; s = [s stringByApp ...
- [ Equipped with lightning rod ]DropShadowEffect This led to a huge increase in memory
original text :[ Equipped with lightning rod ]DropShadowEffect This led to a huge increase in memory [ Equipped with lightning rod ]DropShadowEffect This led to a huge increase in memory Zhou Yinhui From learning WPF Start , You know " Bitmap effect " Not what ...
- Unity Built-In Shader Resulting in a surge in runtime memory
In a certain PC A lot of shaders are used in the project , And they all use their own Standard Shader, While the editor is running , Everything is good , The running memory is only 1G about , But it's going on AssetBundle After packaging , EXE shipment ...
- NGK Global Moscow roadshow : Pay attention to memory explosion and Defi The new project -Baccarat
The roadshow was conducted by Moscow lecturers Andrew First of all, I want to make a speech . First Andrew reviewed NGK The overall development in recent years , Express ,NGK The technical team has been working hard and will pay more attention to the improvement of technical solutions , Providing better technical solutions for the industry is NGK Vision ,NG ...
- Remember a .NET some WMS Warehouse billing system Analysis of memory explosion
One : background 1. Tell a story In mid July, a friend added wx For help , His program runs in production, and the memory soars , There seems to be no turning back trend , Ask how to solve , The screenshot is as follows : Talk to this friend , It feels like I've become a small boss in a small county , Regular students ...
- Python ThreadPoolExecutor Thread pool causes memory to soar
background There is 200W When your task needs to be captured , At present, the thread pool is used to grab , Eventually, the memory soared . reason Threadpoolexcutor By default, unbounded queues are used , If the speed of consumption task is lower than that of production task , Then the production task will be eliminated ...
- Make a record of the pits we have encountered in recent days (.netcore Agency problem )
1. Through the network of pictures url Convert picture to base64 Format The method is as follows : public static async Task<string> GetImageAsBase64Url(string u ...
Random recommendation
- CRM( Customer relationship management )
CRM It was originally made by Gartner Group Proposed . CRM Definition :" Customer relationship management (CRM), It means increasing profits . Designed for revenue and customer satisfaction , Enterprise wide business strategy ." We can see that ,Gartne ...
- [LintCode] Paint House II Painting the house two
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- linux command :more
1. Command Introduction : more Used to output file contents page by page , The space bar goes to the next page ,b Key to return to the previous page . 2. Command format : more [ Options ] file 3. Command parameter +n From the first n Line start display -n Definitional screen ...
- hash Decryption assistant -python edition
Let's watch the live broadcast of the black cloud conference today , The last question uses DEKHash Decrypt , So go on github Searched for a gadget , Name is GeneralHashFunctions.py, The source forgot to copy , Just copy the useful code , The next time we meet the source is sticky ...
- About C++ The coding of the program
from : http://blog.chinaunix.net/uid-26790551-id-3190813.html Our traditional programs are basically just Windows Or only in Linux Run under ,Windows Procedure makes ...
- Underscore.js Source learning notes ( On )
edition Underscore.js 1.9.1 altogether 1693 That's ok . I deleted the notes , Is too long. … The whole is one (function() {...}()); Such things , We should know that this is a IIFE( Execute now ...
- Codeforces Round #503 (by SIS, Div. 2)
Connect :http://codeforces.com/contest/1020 C.Elections Question type : If you say water question, just water question ... I didn't do it ...get To a new idea , Don't empty . It seems that there is something else made of three points ? KN ...
- [ turn ]kaldi Online identification in ----Online Recognizers
from : http://blog.csdn.net/wbgxx333/article/details/24932533 This article is about kaldi In the learning alliance @ Translated casually , Here is @ The translation result of Bolden , Thank you here @ Bold ...
- Leetcode 692 - Note
1. Subject requirements Given a non-empty list of words, return the k most frequent elements. Your answer should be ...
- uuid Only
Is the only one. . I have the same hardware on several machines ( Purchased in the same batch ). The software is exactly the same ( Use the same GHOST System installation ) I tried on my computer : On different computers ,wmic csproduct get uuid Acquired UUID The code is different . another ...