Memory
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.
Features
- 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.
Usage
Measure and Log Memory Usage
To measure and automatically log the current memory usage:
Memory.Measure();
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);
Collect
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.
Usage
Memory.Collect();
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.