目录

一、测试过程及问题

二、问题排查及分析过程

三、问题分析及解决过程

四、问题解决初步结果


一、测试过程及问题

从昨天15点左右开始测试,1个主节点,10个计算节点,1000个数据点,每个数据点3(1个实时窗口,2个延迟窗口)个数据窗口,每个数据点随时生成窗口周期和计算实例,每个数据点随时生成实时数据或历史数据。

测试结果,由于程序无法再获得电脑的内存而停止工作,更专业的说是System. OutOfMemoryException。

主节点,今天3点左右开始出现异常,如下:

[20-11-13 03:00:21]>>窗口0952-补发数据_CSharpFlink.Core.Window.Operator.Min-线程(0033):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ToString()
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358 [20-11-13 03:00:35]>>窗口0927-补发数据_CSharpFlink.Core.Window.Operator.Min-线程(0098):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.RegularExpressions.Match..ctor(Regex regex, Int32 capcount, String text, Int32 begpos, Int32 len, Int32 startpos)
at System.Text.RegularExpressions.RegexRunner.InitMatch()
at System.Text.RegularExpressions.RegexRunner.Scan(Regex regex, String text, Int32 textbeg, Int32 textend, Int32 textstart, Int32 prevlen, Boolean quick, TimeSpan timeout)
at System.Text.RegularExpressions.Regex.Run(Boolean quick, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat)
at System.Text.RegularExpressions.Match.NextMatch()
at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358 [20-11-13 03:00:42]>>窗口0941-补发数据_CSharpFlink.Core.Window.Operator.Avg-线程(0085):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.GC.AllocateNewArray(IntPtr typeHandle, Int32 length, Boolean zeroingOptional)
at System.GC.AllocateUninitializedArray[T](Int32 length)
at System.Buffers.TlsOverPerCoreLockedStacksArrayPool`1.Rent(Int32 minimumLength)
at System.Text.ValueStringBuilder.Grow(Int32 additionalCapacityBeyondPos)
at System.Text.ValueStringBuilder.Append(ReadOnlySpan`1 value)
at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358
ValueStringBuilder.Append(ReadOnlySpan`1 value)
at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358 [20-11-13 03:00:46]>>窗口0970-补发数据_CSharpFlink.Core.Window.Operator.Sum-线程(0074):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.String.Concat(String str0, String str1)
at CSharpFlink.Core.Common.FileUtil.WriteAppend(String filePath, String[] contents) in \CSharpFlink\src\CSharpFlink.Core\Common\FileUtil.cs:line 36
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 370

从节点,部分存活,部分异常退出,异常信息如下:

[20-11-13 02:00:38]>>任务解析异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.String.Concat(String str0, String str1)
at CSharpFlink.Core.Common.FileUtil.WriteAppend(String filePath, String[] contents) in \CSharpFlink\src\CSharpFlink.Core\Common\FileUtil.cs:line 36
at CSharpFlink.Core.Task.SlaveTaskManager.AddTask(String taskMsg) in \CSharpFlink\src\CSharpFlink.Core\Task\SlaveTaskManager.cs:line 138

358行的代码:

CalculateContext calcContext=(CalculateContext)context;

370行的代码:

_masterCacheList.TryAdd(downTrans.Key, compressMsg);

138行的代码:

_slaveCacheList.TryAdd(downTrans.Key, downTrans);

masterCacheList和slaveCacheList变量是ConcurrentDictionary类。

二、问题排查及分析过程

共性问题:记录的每处OutOfMemoryException异常信息都会涉及到对【String】的操作。

第一步,使用dotnet-dump工具对String进行操作

参考链接:https://docs.microsoft.com/zh-cn/dotnet/core/diagnostics/debug-memory-leak

System.String有1784359个对象,为什么这么多对象呢?因为要生成计算节点的任务,这个任务要临时保存到文件目录中,把计算任务的文件发送到计算节点后,再进行删除和清空程序缓存。

写任务文件其中涉及到FileUtil.WriteAppend()方法,这个和上面异常的日志信息是对应的,WriteAppend的代码,如下:

public static void WriteAppend(string filePath, string[] contents)
{
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
fs.Seek(fs.Length, SeekOrigin.Current);
    string content = String.Join(Environment.NewLine, contents) + Environment.NewLine;
    byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
    fs.Write(data, 0, data.Length);
    fs.Close();
}
}
注:这是很早写的代码。

其中Join函数,可能涉及到了Concat函数,和异常信息也是对应的。

那就奇怪了,难道using和Close没有起来关闭和释放资源的目的吗?让我们来看看FileStream的基类Stream的Dispose和 Close都做了什么?看源代码,如下图:

从代码上看唯一做了SuppressFinalize函数操作,那么SuppressFinalize是什么意思呢?参见链接:

https://docs.microsoft.com/zh-cn/dotnet/api/system.gc.suppressfinalize?view=netcore-3.1

上面链接的大概意思是:请求公共语言运行时不要调用指定对象的终结器。也就是说继承了IDisposable接口,就不再调用类的析构函数了,那析构函数做了什么呢?如下图:

我们分析至此,Dispose和Close相当于什么都没有做。那只能依赖GC来清理资源了。那在高并发下操作FileStream,Dispose和Close不起作用的情况下,难道GC没有及时回收资源?看来有可能是这个问题。

三、问题分析及解决过程

但是怎么解决这个问题呢?记得FileStream类有一个Flush函数,具体操作函数代码,如下图:

Flush函数主要调用了FlushOSBuffer函数,代码如下图:

没有找到FlushFileBuffers函数,调用的函数,如下图:

这是非托管的代码,函数参考链接:https://docs.microsoft.com/zh-cn/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers。大致意思是立即把数据写到磁盘文件中,但是没有找到该函数的源代码。

不管源代码的事了,修改一下WriteAppend函数,加上Flush测试一下,代码如下:

using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
fs.Seek(fs.Length, SeekOrigin.Current);
string content = String.Join(Environment.NewLine, contents) + Environment.NewLine;
byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
fs.Write(data, 0, data.Length);
fs.Flush(); //新增加代码。
fs.Close();
}

四、问题解决初步结果

上午10点部署,测试到下午15点,总共5个小时左右的时间。内存使用情况,主节点基本维持在:380 MB(1000数据点,每个数据点有3个数据窗口,如果1个窗口,应该在130 MB左右),子节点基本维持在:150 MB。有一段时间,内存会逐步增涨,但是某个时间点内存会释放到基本情况,曲线呈现正弦波趋势。内存使用情况,如下图:


物联网&大数据技术 QQ群:54256083

物联网&大数据合作 QQ群:727664080

网站:http://www.ineuos.net

联系QQ:504547114

合作微信:wxzz0151

官方博客:https://www.cnblogs.com/lsjwq

iNeuOS工业互联网操作系统 公众号

CSharpFlink分布式实时计算,OutOfMemoryException异常,你意想不到的原因。的更多相关文章

  1. JStorm 是一个分布式实时计算引擎

    alibaba/jstorm JStorm 是一个分布式实时计算引擎. JStorm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStor ...

  2. [开源]CSharpFlink(NET 5.0开发)分布式实时计算框架,PC机10万数据点秒级计算测试说明

    github地址:https://github.com/wxzz/CSharpFlinkgitee地址:https://gitee.com/wxzz/CSharpFlink  1         计算 ...

  3. 开源分布式实时计算引擎 Iveely Computing 之 WordCount 详解(3)

    WordCount是很多分布式计算中,最常用的例子,例如Hadoop.Storm,Iveely Computing也不例外.明白了WordCount在Iveely Computing上的运行原理,就很 ...

  4. 开源分布式实时计算引擎 Iveely Computing 之 本地调试Topology(4)

    当我们写完一个比较复杂的Topology之后,倘若直接提交到服务器上运行,难免会有很多问题,如何进行本地的调试Topology,是我们非常关心的问题.我们依然以WordCount作为代码示例. 首先, ...

  5. 开源分布式实时计算引擎 Iveely Computing 之 安装部署(2)

          在Github中下载代码和二进制程序中,您都会看到一个bin\iveely computing目录,里面即是Iveely Computing的运行库.              以前总是有 ...

  6. 一脸懵逼学习Storm---(一个开源的分布式实时计算系统)

    Storm的官方网址:http://storm.apache.org/index.html 1:什么是Storm? Storm是一个开源的分布式实时计算系统,可以简单.可靠的处理大量的数据流.被称作“ ...

  7. (第8篇)实时可靠的开源分布式实时计算系统——Storm

    摘要: 在Hadoop生态圈中,针对大数据进行批量计算时,通常需要一个或者多个MapReduce作业来完成,但这种批量计算方式是满足不了对实时性要求高的场景.那Storm是怎么做到的呢? 博主福利 给 ...

  8. TOP100summit:【分享实录】Twitter 新一代实时计算平台Heron

    本篇文章内容来自2016年TOP100summit Twitter technical lead for Heron Maosong Fu 的案例分享. 编辑:Cynthia Maosong Fu:T ...

  9. Storm分布式实时流计算框架相关技术总结

    Storm分布式实时流计算框架相关技术总结 Storm作为一个开源的分布式实时流计算框架,其内部实现使用了一些常用的技术,这里是对这些技术及其在Storm中作用的概括介绍.以此为基础,后续再深入了解S ...

随机推荐

  1. randomatic

    下载 randomaticrandomatic 使用简单的字符序列生成指定长度的随机字符串.原generate-password. 请考虑下面这个项目的作者,Jon Schlinkert主演的,考虑项 ...

  2. 异步编程新方式async/await

    一.前言 实际上对async/await并不是很陌生,早在阮大大的ES6教程里面就接触到了,但是一直处于理解并不熟练使用的状态,于是决定重新学习并且总结一下,写了这篇博文.如果文中有错误的地方还请各位 ...

  3. CLion 控制台输出内容乱码问题的解决方法

    问题再现 #include "stdio.h" #include "stdlib.h" int main() { printf("嘤嘤嘤") ...

  4. 【Luogu】P3369 【模板】普通平衡树(树状数组)

    P3369 [模板]普通平衡树(树状数组) 一.树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构. ...

  5. swoole为什么不建议使用static和global

    $http = new swoole_http_server("0.0.0.0", 9501); $http->on("request", functio ...

  6. nginx安全:配置allow/deny控制ip访问(ngx_http_access_module)

    一,nginx中allow/deny指令的用途 1, Nginx的deny和allow指令是由ngx_http_access_module模块提供, Nginx安装默认内置了该模块 2, nginx访 ...

  7. JS的Document属性和方法(转)

    document.title //设置文档标题等价于HTML的title标签document.bgColor //设置页面背景色document.fgColor //设置前景色(文本颜色)docume ...

  8. Linux系统及第三方应用官方文档

    通过在线文档获取帮助 http://www.github.com https://www.kernel.org/doc/html/latest/ http://httpd.apache.org htt ...

  9. apktool的下载,安装,反编译和重新打包

    一.环境要求 安装java 1.8 以上 命令行运行 java -version 返回版本大于1.8 如果没有,请安装java 1.8 二.下载与安装 下载apktool_x.x.x.jar到本地 官 ...

  10. 逆向so文件调试工具ida基础知识点

    1.界面介绍 https://www.freebuf.com/column/157939.html 2.IDA常用快捷键 切换文本视图与图表视图 空格键 返回上一个操作地址 ESC 搜索地址和符号 G ...