@@ -77,6 +77,7 @@ public class Mqtt5Canary {
7777 static int operationFutureWaitTime = 30 ;
7878
7979 private static final int MAX_PAYLOAD_SIZE = 65535 ; // Use UINT64_MAX for the payload size
80+ private static final long MEMORY_CHECK_INTERVAL_SECONDS = 600 ; // 10 minutes
8081
8182 static void printUsage () {
8283 System .out .println (
@@ -207,6 +208,17 @@ static void PrintLog(String message) {
207208 }
208209 }
209210
211+ static void PrintMemoryUsageReport (long elapsedSeconds , long operationsExecuted ) {
212+ long nativeMemoryBytes = CRT .nativeMemory ();
213+ StringBuilder report = new StringBuilder ();
214+ report .append ("\n === Memory Usage Report ===\n " );
215+ report .append (" Elapsed time: " ).append (elapsedSeconds ).append (" seconds\n " );
216+ report .append (" Native memory (bytes): " ).append (nativeMemoryBytes ).append ("\n " );
217+ report .append (" Operations executed: " ).append (operationsExecuted ).append ("\n " );
218+ report .append ("===========================\n " );
219+ PrintLog (report .toString ());
220+ }
221+
210222 static void exitWithError (int errorCode ) {
211223 if (configFilePrinter != null ) {
212224 configFilePrinter .close ();
@@ -715,17 +727,27 @@ public static void main(String[] args) {
715727 // ====================
716728 PrintLog ("Starting canary test loop..." );
717729
730+ // Print initial memory usage report
731+ PrintMemoryUsageReport (0 , 0 );
732+
718733 boolean done = false ;
719734 java .time .LocalDateTime nowDateTime = java .time .LocalDateTime .now ();
720735 long secondsDifference = 0 ;
721736 long operationsExecuted = 0 ;
737+ long nextMemoryCheckSeconds = MEMORY_CHECK_INTERVAL_SECONDS ;
722738 while (!done ) {
723739 try {
724740 nowDateTime = java .time .LocalDateTime .now ();
725741 secondsDifference = startDateTime .until (java .time .LocalDateTime .now (), ChronoUnit .SECONDS );
726742 if (secondsDifference >= configSeconds ) {
727743 done = true ;
728744 }
745+
746+ // Check if it's time to print memory usage report
747+ if (secondsDifference >= nextMemoryCheckSeconds ) {
748+ PrintMemoryUsageReport (secondsDifference , operationsExecuted * configClients );
749+ nextMemoryCheckSeconds += MEMORY_CHECK_INTERVAL_SECONDS ;
750+ }
729751 } catch (ArithmeticException ex ) {
730752 // Time overflow - exit with an error!
731753 exitWithError (1 );
@@ -744,6 +766,9 @@ public static void main(String[] args) {
744766
745767 PrintLog ("Test loop operations complete: Total=" + (operationsExecuted * configClients ) + " Cycles=" + operationsExecuted );
746768
769+ // Print final memory usage report
770+ PrintMemoryUsageReport (secondsDifference , operationsExecuted * configClients );
771+
747772 // Stop all the clients and close them to clean their memory
748773 for (int i = 0 ; i < clients .size (); i ++) {
749774 OperationStop (i );
0 commit comments