评估大内存的可用内存的最佳方法 [英] Best way to gauge free memory for a big buffer

查看:104
本文介绍了评估大内存的可用内存的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎完成了一个擦除可用空间"的应用,在使用文件系统的硬盘驱动器上.基本上,它会创建随机数的大文件(FAT为2GB,NTFS为4GB),直到磁盘已满.我想做的是使用最大的 可能的写缓冲区,可以更快地写入文件.我可以想到两种方式:

I have almost finished an app that "wipes free space" on a hard drive using the filesystem. Basically it creates large files of random numbers (2GB for FAT and 4GB for NTFS) until the disk is full. What I would like to do is to use the largest possible write buffer which makes writing the files faster. I can think of two ways:

1-设置一个循环,尝试分配4GB,捕获异常并降低分配,直到没有异常发生为止.

1 - set up a loop that tries to allocate 4GB, catch the exception, and lower the allocation until no exception occurs.

2-使用My.Computer.Info内存变量获取可用内存并分配< some>其中.见下文:

2 - use the My.Computer.Info memory variables to get the free memory and allocate <some> of that. See Below:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        TextBox1.AppendText(String.Format("TotalPhysicalMemory: {0} MBytes", System.Math.Round(My.Computer.Info.TotalPhysicalMemory / (1024 * 1024)), 2).ToString & vbNewLine)
        TextBox1.AppendText(String.Format("AvailablePhysicalMemory: {0} MBytes", System.Math.Round(My.Computer.Info.AvailablePhysicalMemory / (1024 * 1024)), 2).ToString & vbNewLine)
        TextBox1.AppendText(String.Format("TotalVirtualMemory: {0} MBytes", System.Math.Round(My.Computer.Info.TotalVirtualMemory / (1024 * 1024)), 2).ToString & vbNewLine)
        TextBox1.AppendText(String.Format("AvailableVirtualMemory: {0} MBytes", System.Math.Round(My.Computer.Info.AvailableVirtualMemory / (1024 * 1024)), 2).ToString & vbNewLine)
    End Sub

在我的系统上,我看到以下内容:

On my system here I see this:

使用以下代码:

        Dim StartBufferSize As ULong = 1024L * 1024L * 1024L * 8L ' Start with 8 GB
        Dim Allocated As Boolean = True
        Dim BUFFER() As Byte
        Do
            Try
                Allocated = True
                ReDim BUFFER(StartBufferSize)
            Catch ex As Exception
                Allocated = False
                StartBufferSize \= 2
            End Try
        Loop Until Allocated = True
        ReDim BUFFER(0)
        TextBox1.AppendText("Allocated " & System.Math.Round(StartBufferSize / (1024 * 1024), 2).ToString & vbNewLine)
    End Sub

我知道

已分配1024(1 GB)

Allocated 1024 (1 GB)

现在我正在使用256 MB作为固定缓冲区,这在这里还可以,但是对于其他人,我想知道是否应该使用我可以分配的所有资源.巨大的驱动器(3或4 TB)需要几个小时才能装满.关于多少可用内存应该有什么建议吗 被一个进程使用?

Right now I am using 256 MB as a fixed buffer which is OK here but for others I am wondering if I should use ALL of what I can allocate. A huge drive (3 or 4 TB) takes hours to fill. Are there any suggestions as far as how much of available memory should be used by one process ?

推荐答案

您是否测试过是否有一些最佳缓冲区大小而不是最大缓冲区大小?

Have you tested to see if there is some optimum buffer size as opposed to aiming for the maximum buffer size?

-
安德鲁


这篇关于评估大内存的可用内存的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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