Try   HackMD

將IDE升級至VC2022後的好處,即可使用VC2022提供的診斷工具檢測記憶體。

前置知識

Stack(堆疊)
儲存區域變數及函式,速度快不需額外管理。

Heap(堆積)
動態記憶體分配的空間,大小受裝置物理所限制,開發者需謹慎管理避免Memory leak。

Heap的記憶體檢測就是本篇的主角

診斷工具

在應用程式運行時自動會跳出診斷工具,切換到記憶體使用量,擷取快照。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

配置數 Objects(Diff)
當前物件配置數量及與上次快照差異。

堆積大小 HeapSize(Diff)
當前Heap大小及與上次快照差異。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

只能看到原生層堆疊圖,但聊勝於無,搭配應用程試運行歷程可以更明確分析目前記憶體是否正常。

實例分析

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

掛機狀態的記憶體用量

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

開始自動玩後記憶體用量

對比可發現自動玩前記憶體相對穩定,開始掛機自動玩後記憶體開始大幅增長,更進一步分析Object及Diff,會發現有大量物件增長,但可惜此工具無法明確獲取lua物件類型,只能透過堆疊圖知道是由原生層建構式產生觀察物件類別,進一步縮小檢測範圍。

參考:
https://learn.microsoft.com/zh-tw/visualstudio/profiling/memory-usage?view=vs-2022