【发布时间】:2011-12-12 14:49:31
【问题描述】:
如何在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);
【问题讨论】:
-
您真的确定知道页面大小可以让您在如何分配内存方面做出更好的选择吗?如果是这样,您确定 C# 是开发您的解决方案的最佳技术吗?您介意发布一个示例,说明知道页面大小有助于您分配内存吗?
-
请看我的示例代码。正如你在这里看到的 byte[] buffer = new byte[4096]; 4096 是硬编码的。我需要在这里使用正确的值,例如在 MS Windows 7 x86 和 x64 的情况下
标签: c# .net memory memory-management