欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net core服務(wù)限制堆內(nèi)存大小的操作方法

 更新時(shí)間:2022年09月22日 09:22:36   作者:L-960  
asp.net core是微軟旗下支持跨平臺(tái)的開發(fā)框架,與springboot思想類似,支持ioc等,可以快速的開發(fā)web api等項(xiàng)目,這篇文章主要介紹了asp.net core服務(wù)限制堆內(nèi)存大小,需要的朋友可以參考下

前言

在我們眾多的微服務(wù)項(xiàng)目中,都有限制其堆內(nèi)存大小的需求,以免占用宿主機(jī)內(nèi)存過高。

在java中我們可以通過jvm參數(shù)來很好的控制堆內(nèi)存以及其他參數(shù)。

但是在asp.net core的web服務(wù)中,我們?cè)撊绾稳ハ拗贫褍?nèi)存大小呢?

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

1、asp.net core是什么

微軟旗下支持跨平臺(tái)的開發(fā)框架,與springboot思想類似,支持ioc等,可以快速的開發(fā)web api等項(xiàng)目
官方文檔:https://learn.microsoft.com/zh-cn/aspnet/core/introduction-to-aspnet-core?view=aspnetcore-6.0

2、限制其堆內(nèi)存最大大小

建議熟讀官方文檔:https://learn.microsoft.com/zh-cn/dotnet/core/runtime-config/

2.1 設(shè)置.NET 運(yùn)行時(shí)的配置

官網(wǎng)文檔:https://learn.microsoft.com/zh-cn/dotnet/core/runtime-config/#runtimeconfigjson

.NET 提供了以下機(jī)制用于配置 .NET 運(yùn)行時(shí)的行為:

  • runtimeconfig.json 文件
  • MSBuild 屬性
  • 環(huán)境變量

通過使用環(huán)境變量來配置某個(gè)選項(xiàng)會(huì)將設(shè)置應(yīng)用于所有的 .NET 應(yīng)用。 在 runtimeconfig.json 或項(xiàng)目文件中配置某個(gè)選項(xiàng)則只會(huì)將設(shè)置應(yīng)用于該應(yīng)用程序。

選擇 runtimeconfig.json文件作為.net運(yùn)行時(shí)的配置文件。

2.2 在項(xiàng)目中創(chuàng)建runtimeconfig.json配置文件

構(gòu)建項(xiàng)目時(shí),將在打包的輸出目錄中生成 [appname].runtimeconfig.json 文件。

如果項(xiàng)目文件所在的文件夾中存在 runtimeconfig.template.json 文件,它包含的任何配置選項(xiàng)都將插入到 [appname].runtimeconfig.json 文件中。

如果自行構(gòu)建應(yīng)用,請(qǐng)將所有配置選項(xiàng)放在 runtimeconfig.template.json 文件中。 如果只是運(yùn)行應(yīng)用,請(qǐng)將其直接插入 [appname].runtimeconfig.template.json 文件中。

在這里插入圖片描述

2.2 限制堆的大小

  • 指定 GC 堆和 GC 簿記的最大提交大?。ㄒ宰止?jié)為單位)。
  • 此設(shè)置僅適用于 64 位計(jì)算機(jī)。
  • 如果已配置每對(duì)象堆限制,則忽略此設(shè)置。
  • 默認(rèn)值(僅在某些情況下適用)是 20 MB 或容器內(nèi)存限制的 75%(以較大者為準(zhǔn))。 此默認(rèn)值在以下情況下適用:
  • 進(jìn)程正在具有指定內(nèi)存限制的容器中運(yùn)行。
  • HeapHardLimitPercent 未設(shè)置。

示例:限制堆內(nèi)存最大為1G

{
    "configProperties": {
      "System.GC.HeapHardLimit": 1073741824
    }
}

3、測試配置是否生效

測試控制器:

[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
    [HttpGet]
    public void testMemory()
    {
        List<byte[]> bytesList = new List<byte[]>();
        while (true)
        {
            Console.ReadKey();
            // 100m
            for (int i = 0; i < 100; i++)
            {
                // 1mb
                byte[] bytes = new byte[1024 * 1024];
                bytesList.Add(bytes);
            }
            Console.WriteLine("當(dāng)前堆內(nèi)存大小 -- " + GC.GetTotalMemory(false) / 1024 / 1024.0 + " MB");
        }

    }
}

結(jié)果,可見配置生效,達(dá)到1g時(shí)報(bào)錯(cuò) System.OutOfMemoryException,然后系統(tǒng)強(qiáng)行g(shù)c,服務(wù)down,配置docker-compose的自動(dòng)重啟即可完成gc后自動(dòng)重啟

當(dāng)前堆內(nèi)存大小 -- 102.0029296875 MB
當(dāng)前堆內(nèi)存大小 -- 202.013671875 MB
當(dāng)前堆內(nèi)存大小 -- 302.0166015625 MB
當(dāng)前堆內(nèi)存大小 -- 402.0126953125 MB
當(dāng)前堆內(nèi)存大小 -- 502.0166015625 MB
當(dāng)前堆內(nèi)存大小 -- 602.02734375 MB
當(dāng)前堆內(nèi)存大小 -- 702.044921875 MB
當(dāng)前堆內(nèi)存大小 -- 802.046875 MB
當(dāng)前堆內(nèi)存大小 -- 902.0498046875 MB
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action office_conver_server.Controllers.TestController.testMemory (office-conver-server) in 5924.7612ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'office_conver_server.Controllers.TestController.testMemory (office-conver-server)'
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
         at office_conver_server.Controllers.TestController.testMemory() in D:\BaiduSyncdisk\項(xiàng)目目錄\ItemProjects\dotnet\office-conver-server\Controllers\TestController.cs:line 49
         at Microsoft.Extensions.Internal.ObjectMethodExecutor.<>c__DisplayClass33_0.<WrapVoidMethod>b__0(Object target, Object[] parameters)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.VoidResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Obje
ct[] arguments)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object sta
te, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
         at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

4、在docker容器中限制堆內(nèi)存大小

可以采用上述配置,但是缺點(diǎn)就是不靈活,需要頻繁更新代碼,更新容器。。。

添加容器環(huán)境變量DOTNET_GCHeapHardLimit: "value"
注意value是十六進(jìn)制

version: "3"
services:
  officeConverServer:
    image: l-4.1-office-conver-server:test
    ports:
      - 8079:80
    volumes:
      - ./uploadFile:/uploadFile
      #- ./office-conver-server.runtimeconfig.json:/app/office-conver-server.runtimeconfig.json
      - ./appsettings.json:/app/appsettings.json
    environment:
      # 堆內(nèi)存最大限制【十六進(jìn)制】
      DOTNET_GCHeapHardLimit: "40000000"
      TZ: Asia/Shanghai
   # deploy: 
    #  resources: 
     #   limits: 
      #    memory: 1G
    restart: always
    security_opt:
      - seccomp:unconfined

到此這篇關(guān)于asp.net core服務(wù)限制堆內(nèi)存大小的文章就介紹到這了,更多相關(guān)asp.net core堆內(nèi)存大小內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論