简体   繁体   English

DotNet Memory 用法(VB.Net 2005/ .NET 2.0)

[英]DotNet Memory Usage (VB.Net 2005/ .NET 2.0)

please have look at the following code:请看下面的代码:

'Create array
Dim a(10000, 10000) As Integer
'Print memory of application and physical memory
Console.WriteLine(Process.GetCurrentProcess.PrivateMemorySize64)
Console.WriteLine(My.Computer.Info.AvailablePhysicalMemory)

'Do it twice again
Dim b(10000, 10000) As Integer
Console.WriteLine(Process.GetCurrentProcess.PrivateMemorySize64)
Console.WriteLine(My.Computer.Info.AvailablePhysicalMemory)

Dim c(10000, 10000) As Integer
Console.WriteLine(Process.GetCurrentProcess.PrivateMemorySize64)
Console.WriteLine(My.Computer.Info.AvailablePhysicalMemory)

For i As Integer = 0 To 10000
    For j As Integer = 0 To 10000
        a(i, j) = 0
    Next
Next

Console.WriteLine(My.Computer.Info.AvailablePhysicalMemory)

and the output on my system:和我系统上的 output :

430125056
2466795520
839479296
2463166464
1273315328
2461618176
2065424384

Every initialized array occupies about 400MB of memory in the application, as expected.正如预期的那样,每个初始化的数组在应用程序中占用大约 400MB 的 memory。 But the available physical memory is only reduced by 400MB after filling one of the array with values (the task manager also only shows 400MB used after the for loop...).但是可用的物理 memory 在用值填充数组之一后仅减少了 400MB(任务管理器也仅显示在 for 循环之后使用了 400MB...)。

I always thought an initialized integer array occupies the whole needed memory because it is filled with 0. What's the point?我一直认为一个初始化的 integer 数组占据了整个所需的 memory 因为它填充了 0。有什么意义?

AvailablePhysicalMemory (http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.devices.computerinfo.availablephysicalmemory.aspx)可用物理内存 (http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.devices.computerinfo.availablephysicalmemory.aspx)

A ULong containing the number of bytes of free physical memory for the computer.包含计算机可用物理 memory 字节数的 ULong。

I think the key there is 'physical' memory.我认为关键是“物理”memory。 My understanding is that speaking to the actual hardware level of memory you have available.我的理解是,说到你可用的 memory 的实际硬件水平。

The PrivateMemorySize64 is memory 'assigned' to the current process. PrivateMemorySize64 是 memory “分配”给当前进程。 But, think of page swaps;但是,想想页面交换; that memory for a process doesn't have to be physical at all.一个进程的 memory 根本不必是物理的。

EDIT: I think Joe's comment does a better job of answering this than my answer - doh!编辑:我认为乔的评论比我的回答更能回答这个问题 - 哦!

The.Net environment uses garbage collection, which frees the designer from the need to operate the memory. .Net 环境使用垃圾回收,使设计人员无需操作 memory。 .Net does this all for you, and actually does a great job, saving lots of memory, errors and development time. .Net 为您完成了这一切,实际上做得很好,节省了大量的 memory、错误和开发时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM