0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[VB.NET]マシンの物理メモリ使用量を取得する

Posted at

久々にVB.NETを触ったので備忘録

環境

.NET Framework 4.5
(.NET Framework 2.0以上なら動く?らしい)

物理メモリ使用量を取得する

物理メモリ使用量は直接取得できないので
「合計物理メモリ量」と「使用可能な物理メモリ量」から計算する

Main.vb
Module Main

    Sub Main()

        ' 合計物理メモリ
        Dim totalPhysicalMemoryByte As Decimal = CDec(My.Computer.Info.TotalPhysicalMemory)
        Dim totalPhysicalMemoryKB As Decimal = totalPhysicalMemoryByte / 1024
        Dim totalPhysicalMemoryMB As Decimal = totalPhysicalMemoryByte / 1024 / 1024
        Dim totalPhysicalMemoryGB As Decimal = totalPhysicalMemoryByte / 1024 / 1024 / 1024

        ' 利用可能な物理メモリ
        Dim availablePhysicalMemoryByte As Decimal = CDec(My.Computer.Info.AvailablePhysicalMemory)
        Dim availablePhysicalMemoryKB As Decimal = availablePhysicalMemoryByte / 1024
        Dim availablePhysicalMemoryMB As Decimal = availablePhysicalMemoryByte / 1024 / 1024
        Dim availablePhysicalMemoryGB As Decimal = availablePhysicalMemoryByte / 1024 / 1024 / 1024

        ' メモリ使用量を計算
        Dim usedPhysicalMemoryByte As Decimal = totalPhysicalMemoryByte - availablePhysicalMemoryByte
        Dim usedPhysicalMemoryKB As Decimal = usedPhysicalMemoryByte / 1024
        Dim usedPhysicalMemoryMB As Decimal = usedPhysicalMemoryByte / 1024 / 1024
        Dim usedPhysicalMemoryGB As Decimal = usedPhysicalMemoryByte / 1024 / 1024 / 1024


        Console.WriteLine("[メモリ情報]合計物理メモリ(Byte):{0} Byte 利用可能な物理メモリ:{1} Byte メモリ使用量:{2} Byte" _
                          , totalPhysicalMemoryByte.ToString("0"), availablePhysicalMemoryByte.ToString("0"), usedPhysicalMemoryByte.ToString("0"))

        Console.WriteLine("[メモリ情報]合計物理メモリ(KB):{0} KB 利用可能な物理メモリ:{1} KB メモリ使用量:{2} KB" _
                          , totalPhysicalMemoryKB.ToString("0"), availablePhysicalMemoryKB.ToString("0"), usedPhysicalMemoryKB.ToString("0"))

        Console.WriteLine("[メモリ情報]合計物理メモリ(MB):{0} MB 利用可能な物理メモリ:{1} MB メモリ使用量:{2} MB" _
                          , totalPhysicalMemoryMB.ToString("0"), availablePhysicalMemoryMB.ToString("0"), usedPhysicalMemoryMB.ToString("0"))

        Console.WriteLine("[メモリ情報]合計物理メモリ(GB):{0} GB 利用可能な物理メモリ:{1} GB メモリ使用量:{2} GB" _
                          , totalPhysicalMemoryGB.ToString("0"), availablePhysicalMemoryGB.ToString("0"), usedPhysicalMemoryGB.ToString("0"))
    End Sub

End Module

こんなの絶対忘れる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?