-
-
Notifications
You must be signed in to change notification settings - Fork 33
Manual Memory Management
Timur Gafarov edited this page Jul 2, 2015
·
12 revisions
From version 0.5.0, dlib supports manual memory management (MMM) based on standard C malloc/free. You can use the module dlib.core.memory
to allocate classes, structs and arrays in unmanaged heap using New
and Delete
template functions:
import dlib.core.memory;
class MyClass
{
int x;
this(int x)
{
this.x = x;
}
}
MyClass obj = New!MyClass(99);
assert(obj.x == 99);
Delete(obj);
int[] arr = New!(int[])(100);
assert(arr.length == 100);
Delete(arr);
This functionality resembles C++ way of doing things, and, as you are requred to manually delete your objects, may lead to memory leaks, if used carelessly. To help you debug your applications, dlib now also provides tools for memory profiling. To use it, compile everything with --version=memoryDebug
option.
The function printMemoryLog
outputs an allocation log with type and size information. Objects that remain in memory (undeleted) are marked as such.
printMemoryLog();
Example output:
----------------------------------------------------
Memory allocation log
----------------------------------------------------
Foo - 12 byte(s) at 544AF0
REMAINS: int[] - 400 byte(s) at 5D07B0
REMAINS: Foo - 12 byte(s) at 544E70
----------------------------------------------------
Total amount of allocated memory: 412
----------------------------------------------------