Skip to content


The Memory class in the ByteCobra.Logging namespace provides a straightforward method for measuring and logging the current memory usage of your application. It reports memory usage in megabytes for easy interpretation.


  • Simple Memory Measurement: Quickly measure the current memory usage of your application.
  • Automatic Logging: Option to log the memory usage directly, simplifying monitoring during development or production diagnostics.


Measure and Log Memory Usage

To measure and automatically log the current memory usage:


This will output something like: Current memory usage: 85.32 MB to the logging system.

Measure Without Logging

If you prefer to measure the memory usage without logging it, you can set the log parameter to false:

double memoryUsage = Memory.Measure(log: false);


It is also possible to reclaim unused memory with the Collect method. It triggers a manual garbage collection process, aiming to immediately reclaim unused memory. It performs a full garbage collection cycle, waits for any pending finalizers, and then performs another garbage collection to clean up any objects finalized in the first pass.



Note: Use this method judiciously, as manual garbage collection can affect application performance. It's recommended to rely on the automatic garbage collection process unless specific scenarios require explicit intervention.