当前位置: 首页 > news >正文

【App Service】.NET 应用在App Service上内存无法占用100%的问题原因

问题描述

如果使用Azure App Service部署.NET 应用,会发现在内容并没有达到100%的时候,也会出现OOM错误。这是一个什么情况呢?

image

大内存测试代码

        static void Main(string[] args){Console.WriteLine("Hello, World!");int objunmbers = 20000;byte[][] largeArray = new byte[objunmbers][];Console.WriteLine("Start to create big memory object ... from 2GB");//Console.WriteLine("Start to create big memory object ... from 20MB");long size = 2L * 1024 * 1024 * 1024; // 2GB//long size = 2L * 1024 * 1024 * 10; // 20MBint times = 1;while (size > 0){if (CreateBigMemoryObject(largeArray,size, times)){Console.WriteLine($"[{times}] Successfully created memory object of size: {size} bytes ({size / 1024 / 1024} MB)");times = times + 1;}else{Console.WriteLine($"Failed to create memory object of size: {size} bytes ({size / 1024 / 1024} MB). Trying smaller size...");size = size - 100 * 1024 * 1024; // Decrease by 100MB//size = size - 1 * 1024 * 1024; // Decrease by 1MB
                }//Thread.Sleep(2000);
            }Console.WriteLine("Finished memory allocation attempts.");Console.WriteLine("sleep 5 mins");Thread.Sleep(5 * 60 * 1000);Console.WriteLine("End");}static bool CreateBigMemoryObject(byte[][] largeArray,long size,int index){try{largeArray[index] = new byte[size];return true;}catch (OutOfMemoryException ex){Console.WriteLine($"OutOfMemoryException: {ex.Message}");}catch (Exception ex){Console.WriteLine($"Exception: {ex.Message}");}return false;}

问题解答

在反复试验后,证明这是.NET 应用在 Azure App Service(Windows) 上才会遇见的问题。

根本原因是:.NET Core 在运行时会对 GC 堆的最大可用内存设定一个Hard Limit,而App Service中设定的值为 75%, 所以32Gb的内容最大可用24Gb。

image

( Manage resource usage for all GC flavors : https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#manage-resource-usage-for-all-gc-flavors )

当然,知道这个限制之后,就可以通过配置去修改它,比如通过配置 runtimeconfig.json 文件,设置 System.GC.HeapHardLimitPercent : 96  或者  System.GC.HeapHardLimit : 32,000,000,000

image

 

 

参考资料

Manage resource usage for all GC flavors : https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#manage-resource-usage-for-all-gc-flavors 

 

http://www.jsqmd.com/news/45935/

相关文章:

  • 深入解析:css 的 clip-path 属性,绘制气泡
  • 快速构建一个基础、现代化的 WinForm 管理系统!
  • 国内外研究现状全面解析:掌握学术前沿的必备指南
  • 费马小定理在素数检测中的应用
  • 把 1688 商品详情「搬进 MySQL」:Java 爬虫全链路实战(2025 版) - 实践
  • 深入解析:从传统架构到云原生,如何应对数据增长挑战?
  • 50036_基于微信小程序的智能点餐推荐系统
  • 【NAOI】题解
  • Windows系统基础安全浅谈
  • 深入解析:医疗多模态共情推理与学习一体化网络Python实现(2025扩充版)
  • curl/libcurl SMTP CRLF注入漏洞深度分析
  • 2025年11月氨基酸水溶肥,花芽分化氨基酸水溶肥,低温酶解氨基酸水溶肥厂家最新推荐,权威测评与种植选择指南!
  • 2025年11月沣硕40+中微量元素水溶肥,防裂果中微量元素水溶肥,促花稳果中微量元素水溶肥厂家推荐:规模化种植适配品牌
  • 4.6.4版本闪亮登场~赶快了解一下新内容吧
  • 2025年11月花芽分化氨基酸水溶肥,膨果上色氨基酸水溶肥,高含量氨基酸水溶肥厂家推荐,实测促产效果与品牌解析!
  • XMind for Mac v24.01.dmg 安装教程(Mac思维导图软件下载安装步骤)
  • 自动类型推导、智能指针、Lambda表达式和函数包装器 - 详解
  • RocketMQ 概念介绍 - 邓维
  • fio linux
  • find linux 文件
  • Docker主机网络优化咋做
  • C语言小程序在日常生活中的应用实例
  • Docker桥接网络能实现跨主机吗
  • fastdb c++如何优化存储结构
  • Docker客户端支持哪些存储驱动
  • c语言实现linux命令
  • discuz使用mysql有哪些注意事项
  • discuz与mysql数据迁移怎样操作
  • c语言在linux
  • dns设置linux