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

发布于 2024-12-11 03:09:45 字数 1196 浏览 0 评论 0原文

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

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

谢谢你!

更新:这是一个示例代码...我对 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);

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 on the line 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2024-12-18 03:09:45

如果您使用的是 C# 4.0,则有一个属性 Environment.SystemPageSize 在内部使用 GetSystemInfo。我以前从未见过它,因为它是新的。

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

在 C 中你可以写这样的东西。

#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;
}

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

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

首先,C#内存是可以移动的,C#使用压缩分代垃圾收集器。
没有任何关于数据将分配到哪里的信息。

其次,C#中的数组可以由不连续的内存区域组成!数组连续存储在虚拟内存中,但连续的虚拟内存不是连续的物理内存。

第三,C#中的数组数据结构比内容本身多占用一些字节(它存储数组大小和其他信息)。如果您分配页面大小的字节数,则使用数组几乎总是会切换页面!

我认为这种优化可以是非优化。

通常,C# 数组的性能非常好,无需使用页面大小来分割内存。

如果确实需要精确分配数据,则需要使用固定数组或元帅分配,但这会减慢垃圾收集器的速度。

我想说最好只使用数组而不过多考虑页面大小。

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.

离鸿 2024-12-18 03:09:45

使用 pinvoke 调用 GetSystemInfo() 并使用 dwPageSize 值。尽管您可能确实想要 dwAllocationGranularity,这是最小的块 VirtualAlloc() 将进行分配。

Use pinvoke to call GetSystemInfo() and use the dwPageSize value. Although you probably really want dwAllocationGranularity, which is the smallest block VirtualAlloc() will allocate instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文