如何获得在C#中的内存微软Windows 7的当前页面大小? [英] How to obtain a current page size of the memory MS Windows 7 in C#?

查看:184
本文介绍了如何获得在C#中的内存微软Windows 7的当前页面大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取内存微软Windows 7的当前页面大小在C#?

在某些情况下,我们需要它的最好的方式分配内存。

感谢您!

更新:下面是一个示例code ......我这里有一些疑惑byte []的缓冲区=新的字节[4096]

  //赋值给这些对象在这里,以便他们能
        //在finally块引用
        流remoteStream = NULL;
        流localStream = NULL;
        WebResponse的响应= NULL;

        尝试
        {
            响应= request.EndGetResponse(结果);

            如果(响应!= NULL)
            {
                //一旦WebResponse对象被检索,获得与响应的数据相关联的流对象
                remoteStream = response.GetResponseStream();

                //创建本地文件
                串pathToSaveFile = Path.Combine(FileManager.GetFolderContent(),TaskResult.ContentItem.FileName);
                localStream = File.Create(pathToSaveFile);

                //分配1k的缓冲http://en.wikipedia.org/wiki/Page_(computer_memory)
                byte []的缓冲区=新的字节[4096];
                INT读取动作;

                //简单的do / while循环,从流中读取,直到没有字节被退回
                做
                {
                    //阅读来自该流数据(高达到1 k)
                    读取动作= remoteStream.Read(缓冲液,0,buffer.Length);
 

解决方案

如果您使用的是C#4.0还有就是在内部使用的GetSystemInfo财产Environment.SystemPageSize。之前,因为它是新的我从来没有见过它。

http://msdn.microsoft.com/en-us/library/system.environment.systempagesize.aspx

在C语言中,你可以写这样的事情。

 的#include< WINDOWS.H>
诠释的主要(无效){
    SYSTEM_INFO SI;
    的GetSystemInfo(安培; SI);

    的printf(页面大小为这个系统是%u字节\ñ,si.dwPageSize);

    返回0;
}
 

然后,您可以使用P / Invoke来调用的GetSystemInfo。 看看 http://www.pinvoke.net/default.aspx/kernel32.getsysteminfo

我还要补充一点,分配的页面大小的字节数组是不是最好的选择。

首先,C#内存可以移动,C#使用压实代垃圾收集器。 上没有,其中数据将被分配任何类型的信息。

二,在C#中的数组可以通过内存不连续的区域形成!数组连续存储在虚拟内存中,但连续虚拟内存是不连续的物理内存。

三,在C#中数组占用一些字节比内容本身(它存储数组大小和其它信息)的更多。如果分配字节的页面大小量,使用阵列几乎总是切换页面!

我倒是觉得这种优化可以是一个非优化。

通常是C#中的数组有很好的表现,而无需使用页面大小分割内存。

如果你确实需要的数据precise分配则需要使用固定阵列或元帅分配,但是这会减慢垃圾收集器。

我会说这是最好只使用数组没有考虑太多的页面大小。

How to obtain a current page size of the memory MS Windows 7 in C#?

In some cases we need it to allocate memory in best way.

Thank you!

UPDATES: Here is a sample code... I have some doubts here byte[] buffer = new byte[4096];

        // Assign values to these objects here so that they can
        // be referenced in the finally block
        Stream remoteStream = null;
        Stream localStream = null;
        WebResponse response = null;

        try
        {
            response = request.EndGetResponse(result);

            if (response != null)
            {
                // Once the WebResponse object has been retrieved, get the stream object associated with the response's data
                remoteStream = response.GetResponseStream();

                // Create the local file
                string pathToSaveFile = Path.Combine(FileManager.GetFolderContent(), TaskResult.ContentItem.FileName);
                localStream = File.Create(pathToSaveFile);

                // Allocate a 1k buffer http://en.wikipedia.org/wiki/Page_(computer_memory)
                byte[] buffer = new byte[4096];      
                int bytesRead;

                // Simple do/while loop to read from stream until no bytes are returned
                do
                {
                    // Read data (up to 1k) from the stream
                    bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

解决方案

If you are using C# 4.0 there is the property Environment.SystemPageSize that internally uses GetSystemInfo. I never seen it before since it is new.

http://msdn.microsoft.com/en-us/library/system.environment.systempagesize.aspx

In C you can write something like this.

#include <windows.h>
int main(void) {
    SYSTEM_INFO si;
    GetSystemInfo(&si);

    printf("The page size for this system is %u bytes.\n", si.dwPageSize);

    return 0;
}

Then, you can use P/INVOKE to call GetSystemInfo. Take a look at http://www.pinvoke.net/default.aspx/kernel32.getsysteminfo

I must also add that allocating an array of page-size bytes is not the best choice.

First of all, C# memory can be moved, C# uses a compacting generational garbage collector. There is not any kind of information on where data will be allocated.

Second, arrays in C# can be formed by non-contiguous area of memory! Arrays are stored contiguously in virtual memory but contiguous virtual memory is not contiguous physical memory.

Third, array data structure in C# occupies some bytes more than the content itself (it stores array size and other informations). If you allocate page size amount of bytes, using the array will switch page almost always!

I would think that this optimization can be an non-optimization.

Usually C# arrays performs very well without the need to split memory using page size.

If you really need precise allocation of data you need to use pinned arrays or Marshal allocation, but this will slow down the garbage collector.

I would say it is better to just use your arrays without thinking too much about the page size.

这篇关于如何获得在C#中的内存微软Windows 7的当前页面大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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