c# - Redis 因内存不足而中止

标签 c# redis stackexchange.redis

我正在尝试将一个大文件(电影)分 block 移动到 Redis 缓存中。 我在 Windows 机器上使用 stackexchange.redis。 redis 配置为执行 allkey-lru 并每秒使用 append。 Maxmemory 配置为 100mb。

ConnectionMultiplexer redis2 = ConnectionMultiplexer.Connect("localhost:6380, syncTimeout=100000");
IDatabase db2 = redis2.GetDatabase();

const int chunkSize = 4096;
string fileName = "D:\\movie.mp4";
using (var file = File.OpenRead(fileName))
{
    int bytesRead;
    int inCounter = 1;
    var buffer = new byte[chunkSize];
    while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
    {
        db2.StringSet(file.Name + inCounter, buffer);
        inCounter++;
    }
}

当 block 大小为 chunkSize = 4096 时一切正常。 但是,当我将 block 大小更改为 65536 时,服务器崩溃并显示以下日志:

[2816] 20 Jul 23:06:42.300 * Starting automatic rewriting of AOF on 6766592700% growth
[2816] 20 Jul 23:06:42.331 * Background append only file rewriting started by pid 3672
[3672] 20 Jul 23:06:42.331 # Write error writing append only file on disk: Invalid argument
[3672] 20 Jul 23:06:42.331 # rewriteAppendOnlyFile failed in qfork: Invalid argument
[2816] 20 Jul 23:06:42.440 # fork operation complete
[2816] 20 Jul 23:06:42.440 # Background AOF rewrite terminated with error
[2816] 20 Jul 23:06:42.549 * Starting automatic rewriting of AOF on 7232582200% growth
[2816] 20 Jul 23:06:42.581 * Background append only file rewriting started by pid 1440
[2816] 20 Jul 23:06:42.581 # Out Of Memory allocating 10485768 bytes!
[2816] 20 Jul 23:06:42.581 #

=== REDIS BUG REPORT START: Cut & paste starting from here ===
[2816] 20 Jul 23:06:42.581 # ------------------------------------------------
[2816] 20 Jul 23:06:42.581 # !!! Software Failure. Press left mouse button to continue
[2816] 20 Jul 23:06:42.581 # Guru Meditation: "Redis aborting for OUT OF MEMORY" #..\src\redis.c:3467
[2816] 20 Jul 23:06:42.581 # ------------------------------------------------
[1440] 20 Jul 23:06:42.581 # Write error writing append only file on disk: Invalid argument
[1440] 20 Jul 23:06:42.581 # rewriteAppendOnlyFile failed in qfork: Invalid argument

有什么想法吗?

最佳答案

原来是一个非常有趣和令人惊讶的问题!

真正的原因是他们正在使用的分配器中的内存碎片 (dlmalloc)。 希望 MSFT 能够做得更好,但我预计这需要一些时间。

与此同时,解决方法。

解决此问题的正确方法(目前)

同时配置maxmemorymaxheap 参数。使 maxheapmaxmemory 大得多。

因此,如果您想要 maxmemory=100MB,则使 maxheap 大 5 倍甚至 10 倍,例如 maxheap=500MB 甚至 maxheap =1000MB。 我认为没有一个好的经验法则 maxheap 需要多大,这就是为什么它是一个如此棘手的问题。

这样做的效果:Redis 仍然会尝试将内存使用量保持在 100MB 以下,但实际使用的物理内存可能 比那个大,可能达到 maxheap 值。具体多少取决于碎片化程度。希望 在现实生活中,这会保持在合理的水平。


我已将问题记录在团队中。 https://github.com/MSOpenTech/redis/issues/274


编辑:我根据新知识完全修改了这个答案。在编辑历史中查看以前的版本。

关于c# - Redis 因内存不足而中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525903/

相关文章:

c# - 直接对redis对象应用分页和排序

c# - 了解用户是否更改了 DataGrid 中的数据的最佳方法是什么?

c# - 获取同一解决方案中另一个可执行文件的应用程序路径

c# - 在 C# Visual Studio 中工作时 mysql 存储过程的问题

postgresql - 在数据库上实现大规模调度程序

redis - StackExchange.Redis - 禁止使用 EVALSHA?

c# - 如何从嵌入式 Uri 资源(png 图像)加载 System.Drawing.Bitmap 对象?

Redis 无法在 centos 7 上启动

database - Web 服务器前端或数据库服务器后端的 Redis

azure - Azure Function 中的 StackExchange.Redis 问题(ResetNonConnected)