使用内存映射文件的缺点 [英] Disadvantages of using memory mapped files

查看:375
本文介绍了使用内存映射文件的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web服务写入每分钟交易数十万,我们将它们保存在HD。

My web service writes several thousands of transactions per minute and we save them on the hd.

我是测试不同的方式来保存这些文件,我用标准的IO和MemoryMapped文件的一些测试。在我的结果,写入文件(20K的文本文件)与MemoryMapped文件大约是4倍于标准IO速度更快,我无法找到任何缺点。

I was testing different ways to save these files and I made some tests with standard IO and with MemoryMapped files. In my results, writing files (20 k text files) with MemoryMapped files is about 4x faster than standard IO and I was not able to find any disadvantages.

由于我没有那么多的经验,有了这项技术,你以为我会利用他们面临的任何问题,或者您没有看到任何缺点?

As I have not so much experience with this technology, do you think I may face any problem using them or you don't see any disadvantage?

谢谢!

修改1 ,这里的源:

namespace FileWritingTests.Writers {
    public class MappedFileWriter : ITestWriter {
        public void Write(string content, string path,string id) {
            Byte[] data = System.Text.Encoding.UTF8.GetBytes(content);

            using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))

            using (MemoryMappedFile memoryMapped = MemoryMappedFile.CreateFromFile(fileStream, id, data.Count(),
                MemoryMappedFileAccess.ReadWrite, new MemoryMappedFileSecurity(), HandleInheritability.Inheritable, true)) {
                var viewStream = memoryMapped.CreateViewStream();
                viewStream.Write(data, 0, data.Length);                       
            }
        }
    }
}

和这是测试仪:

  public TimeSpan Run(int iterations, Writers.ITestWriter tester, String subfolder) {
            Console.WriteLine(" -- Starting test {0} with {1} file writings",subfolder, iterations.ToString());

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Reset();
            stopWatch.Start();
            for (int i = 1; i <= iterations; i++) {
                tester.Write(transaction, this.path + "\\" + subfolder + "\\" + "file_" + i.ToString() + ".txt", i.ToString());
            }
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            Console.WriteLine(" -- finish test {0} with {1} file writings. Time Elapsed: {2}", subfolder, iterations.ToString(), ts.TotalMilliseconds);
            return ts;
        }

测试仪被称为几次并有多种类型的测试仪的所谓的进行比较。

the tester is called several times and there are several types of tester called for comparison.

推荐答案

磁动势的主要缺点是,它们占用的RAM,使得文件系统缓存效果较差。不是一个问题,这样的小文件。

The main disadvantage of MMFs is that they consume RAM, making the file system cache less effective. Not an issue with such small files.

另外一个缺点,但肯定是故意的,就是你不能再衡量写入文件的费用。这就是现在,这是由内核再进行,而不是你的程序工作。它仍然是正在做,当然,有没有这样的东西作为一个免费的午餐。这是并发程序的执行过程中休息,自由线程可以这么说。继续在任务管理器中的系统进程的CPU利用率的眼睛。再次,不太可能有这样的小文件有问题。

Another disadvantage, although surely intentional, is that you can no longer measure the cost of writing the file. That's now a job that's done by the kernel, not your program anymore. It is still being done of course, there's no such thing as a free lunch. It is concurrent with the rest of your program's execution, free threading so to speak. Keep an eye on the CPU utilization of the "System" process in Task Manager. Again, unlikely to be a problem with such small files.

有一个微型的优化,通过创建该文件的成本吹走。其中徘徊20和50之间的磁盘驱动器上毫秒。不要忘记,包括在您的测量。写入文件数据进行操作5千兆字节/秒的存储器总线速度,向上根据其种类的RAM机具有。你切出仅仅是低层次的WriteFile的()调用,他们现在由内核完成。你可以用一个FileStream尝试测试,使用一个构造函数中的 BUFFERSIZE 的值。默认值是4096字节,其增加到32K所以只有一个WriteFile的()调用。做这种方式的主要优点是,你不必猜测MMF的大小锋线。它变得非常难看的时候,你猜得太低。

It is a micro-optimization, blown away by the cost of creating the file. Which hovers between 20 and 50 msec on a disk drive. Don't forget to include that in your measurement. Writing the file data operates at memory bus speeds, upwards of 5 gigabyte/sec depending on the kind of RAM the machine has. What you cut out are just the low-level WriteFile() calls, they are now done by the kernel. You could try testing with a FileStream, use a constructor that takes the bufferSize value. Default is 4096 bytes, increase it to 32K so there's only one WriteFile() call. Chief advantage of doing it this way is that you don't have to guess the size of the MMF up front. It gets very ugly when you guessed too low.

这篇关于使用内存映射文件的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆