簡體   English   中英

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

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

請看下面的代碼:

'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)

和我系統上的 output :

430125056
2466795520
839479296
2463166464
1273315328
2461618176
2065424384

正如預期的那樣,每個初始化的數組在應用程序中占用大約 400MB 的 memory。 但是可用的物理 memory 在用值填充數組之一后僅減少了 400MB(任務管理器也僅顯示在 for 循環之后使用了 400MB...)。

我一直認為一個初始化的 integer 數組占據了整個所需的 memory 因為它填充了 0。有什么意義?

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

包含計算機可用物理 memory 字節數的 ULong。

我認為關鍵是“物理”memory。 我的理解是,說到你可用的 memory 的實際硬件水平。

PrivateMemorySize64 是 memory “分配”給當前進程。 但是,想想頁面交換; 一個進程的 memory 根本不必是物理的。

編輯:我認為喬的評論比我的回答更能回答這個問題 - 哦!

.Net 環境使用垃圾回收,使設計人員無需操作 memory。 .Net 為您完成了這一切,實際上做得很好,節省了大量的 memory、錯誤和開發時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM