Posts C# - 간단한 힙 메모리 디버거 (콘솔)
Post
Cancel

C# - 간단한 힙 메모리 디버거 (콘솔)

기능


  • 원하는 지점의 힙 메모리 크기 기록
  • 기록된 두 지점의 힙 메모리 크기 차이 출력


주의사항


  • Print() 이후에는 Record()하지 않아야 한다.

    Future Works : Print() 내에서 스트링을 스택에 할당하여 해결


사용 예시


1
2
3
4
5
6
7
8
9
10
11
12
13
HeapDebugger.Record(0);

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

HeapDebugger.Record(1);

new string("ABCDE");

HeapDebugger.Record(2);

HeapDebugger.PrintDiff(0, 1); // 128
HeapDebugger.PrintDiff(1, 2); // 32
HeapDebugger.PrintDiff(0, 2); // 160


소스코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;

public static class HeapDebugger
{
    const int RECORDS_LENGTH = 1024;
    private static readonly long[] records = new long[RECORDS_LENGTH];
    private static bool recordAfterPrint = false;

    public static void Record(int index)
    {
        if (recordAfterPrint)
        {
            throw new InvalidOperationException("Print 이후의 Record는 정확하지 않습니다.");
        }

        try
        {
            records[index] = GC.GetTotalMemory(true); // .GetTotalAllocatedBytes() 에서 변경 : 실시간 확인 위해
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine($"HeapDebugger.Record() : 0 ~ {RECORDS_LENGTH - 1} 인덱스에만 기록할 수 있습니다. (입력값 : {index})");
        }
    }

    public static long GetDiff(int from, int to)
    {
        try
        {
            if (records[from] == 0)
                throw new ArgumentException($"[{from}] 인덱스에는 기록하지 않았습니다.");
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine($"HeapDebugger.GetDiff(from) : 0 ~ {RECORDS_LENGTH - 1} 인덱스에만 기록할 수 있습니다. (입력값 : {from})");
        }
        try
        {
            if (records[to] == 0)
                throw new ArgumentException($"[{to}] 인덱스에는 기록하지 않았습니다.");
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine($"HeapDebugger.GetDiff(to) : 0 ~ {RECORDS_LENGTH - 1} 인덱스에만 기록할 수 있습니다. (입력값 : {to})");
        }
            
        return records[to] - records[from];
    }

    public static void PrintDiff(int from, int to, string msg = null)
    {
        recordAfterPrint = true;

        long diff = GetDiff(from, to);

        string message = msg == null ?
            $"Heap Memory Difference[Index : {from} -> {to}] : {diff}" :
            $"{msg} : {diff}";
        Console.WriteLine(message);
    }
}
This post is licensed under CC BY 4.0 by the author.