Blog
All Blog Posts | Next Post | Previous PostTMS MemInsight, Delphi runtime memory inspection: statistics and filters
Tuesday, February 22, 2022
Using TMS MemInsight is easy: drop a component on your form, set some properties and there you go. Altough the component and UI of TMS MemInsight are currently based on the VCL you can already use it in your FireMonkey (on Windows and without UI), Windows service or console applications or any other headless application, such as a TMX XData server. But in this blog, we dive deeper:
TMS MemInsight has a wide API and offers all of its options and features by code.
Our sample unit (compatible with Delphi 2010 and up) looks like this:
unit TMS.MI.StatisticsSample; interface procedure InitializeProfiling; procedure StartProfiling; procedure StopProfiling; procedure ResetStatistics; procedure SaveToFile(const AFileName: String); implementation uses Classes, SysUtils, TMS.MI.Access, TMS.MI.Memory, TMS.MI.UnitNameFilter, TMS.MI.Core; var GStatistics: TTMSMIMemoryStatistics; GFilter: TTMSMIUnitNameFilter; procedure InitializeProfiling; begin // initialize once (it's an example) if GStatistics = nil then begin // create statistics. attaches to the profiler by itself. GStatistics := TTMSMIMemoryStatistics.Create; // create a filter, configure and attach it GFilter := TTMSMIUnitNameFilter.Create; GFilter.Add('*StdCtrls'); GFilter.Active := True; MemoryProfiler.Attach(GFilter); // set some options MemoryProfiler.Callstacks := []; MemoryProfiler.DefaultFilter.TraceTypes := [mtObject]; end; end; procedure StartProfiling; begin MemoryProfiler.Active := True; end; procedure StopProfiling; begin MemoryProfiler.Active := False; end; procedure ResetStatistics; begin if GStatistics <> nil then GStatistics.ResetItems; end; procedure SaveToFile(const AFileName: String); var LStream: TStreamWriter; i: Integer; LItem: PTMSMIMemoryStatisticItem; LLine: String; begin if Assigned(GStatistics) then begin // disable profiling for this method MemoryProfiler.SuspendMethod; // stream will be freed when done TObjectGuard.Guard(LStream, TStreamWriter.Create(AFileName)); // dump the statistics for i := 0 to GStatistics.Count -1 do begin LItem := GStatistics.Items[i]; LLine := LItem.TypeName + ' Live: ' + IntToStr(LItem.LiveCount) + ' +: ' + IntToStr(LItem.Created) + ' -: ' + IntToStr(LItem.Destroyed) + #13#10; LStream.Write(LLine); end; end; end; initialization finalization FreeAndNil(GStatistics); FreeAndNil(GFilter); end.
Within your application call InitializeProfiling once. If possible at a very early stage but initializing at some later point during run-time is not a problem. Also activating/deactivating the profiler several times during run-time is absolutely fine and allows profiling right on the spot. Call StartProfiling from within your application whenever you need to and StopProfiling if you are done. The latter is not strictly necessary but allows accurate control when profiling takes place. Call SaveToFile whenever you need to. Adjust the filter (*StdCtrls) as well as the output to your needs.
I hope you enjoyed my first blog post. One of the next updates of TMS MemInsight will contain some utility classes to make these exports - statistics, memory items, callstacks etc. - even more easy and customizable.
If you have any questions don't hesitate to leave a comment or go to the support forum at https://support.tmssoftware.com/c/developer-tools/tms-meminsight/111
Offering more options, like accessing TMS MemInsight remotely are on our long list of ideas, sketches, enhancements and extensions. Stay tuned!
Stefan Meisner, architect TMS MemInsight
Bruno Fierens
This blog post has received 2 comments.
Bruno Fierens
All Blog Posts | Next Post | Previous Post
I see it states "you can already use it in your Firemonkey".
At first we were told that only VCL was supported.
In the releasenotes I didn''t see any mentions of FMX.
So the question is: Is FMX (Windows?) already supported?
Thx.
Jorissen John