开发者

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:

430125056
2466795520
839479296
2463166464
1273315328
2461618176
2065424384

Every initialized array occupies about 400MB of memory in the applicatio开发者_如何学Pythonn, as expected. 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...).

I always thought an initialized integer array occupies the whole needed memory because it is filled with 0. What's the point?


AvailablePhysicalMemory (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.

I think the key there is 'physical' memory. My understanding is that speaking to the actual hardware level of memory you have available.

The PrivateMemorySize64 is memory 'assigned' to the current process. But, think of page swaps; that memory for a process doesn't have to be physical at all.

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 does this all for you, and actually does a great job, saving lots of memory, errors and development time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜