当前位置: 首页 > 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.zskr.cn/news/55594.html

相关文章:

  • 把 1688 商品详情「搬进 MySQL」:Java 爬虫全链路实战(2025 版) - 实践
  • 【NAOI】题解
  • 深入解析:医疗多模态共情推理与学习一体化网络Python实现(2025扩充版)
  • 2025年11月沣硕40+中微量元素水溶肥,防裂果中微量元素水溶肥,促花稳果中微量元素水溶肥厂家推荐:规模化种植适配品牌
  • 自动类型推导、智能指针、Lambda表达式和函数包装器 - 详解
  • es的sql语句 有哪些限制
  • RocketMQ 概念介绍 - 邓维
  • ffmpeg for linux
  • Docker桥接网络能实现跨主机吗
  • fastdb c++如何优化存储结构
  • discuz与mysql数据迁移怎样操作
  • C语言内存管理怎样优化空间
  • dns 服务器 linux
  • c语言 linux
  • DataTable SQL怎样处理大数据量
  • DataTable SQL如何进行数据更新
  • db2安装linux
  • c#怎么获取服务器ip
  • C++命名空间怎样适应大型项目
  • ArangoDB 键值存储如何实现
  • arm与linux
  • 北海:偶尔不刮风
  • P10176 「OICon-02」Native Faith 题解
  • alisql数据库怎样提高安全性
  • JSAPI Three 是什么?—— 百度地图二三维一体化渲染引擎入门指南
  • 2025临时/水冲储污式/太阳能/真空吸附/气压式/发泡式/打包式/景区/工地/音乐节/展会/马拉松/公园/移动厕所品牌实力榜:三大优质企业领跑多场景适配解锁便捷环保新体验
  • 深度学习之批量归一化的原理
  • Spark微博舆情分析系统 情感分析 爬虫 Hadoop和Hive 贴吧资料 双平台 讲解视频 大内容 Hadoop ✅
  • Langchain Splitter源码阅读笔记(一)CharacterTextSplitter
  • 11/20