Free memory storage used by a variable System unit
 procedure FreeMem ( MemoryPointer : Pointer {; MemorySize : Integer} ) ;
The FreeMem procedure frees up memory storage used by a variable MemoryPointer.
You can optionally specify the MemorySize to be freed. However, you must specify the size allocated in the first place.
If the variable is nil then nothing happens.
If the variable invalidly points to memory (maybe it has already been freed), then an EInvalidPointer exception is thrown.
If the memory contains references to memory based variables, you must call Finalize before FreeMem.
FreeMem is the counter command to GetMem.
Ideally, you should use New and Dispose instead of GetMem and FreeMem. It avoids the need to call Finalize.
Related commands
Dispose Dispose of storage used by a pointer type variable
GetMem Get a specified number of storage bytes
New Create a new pointer type variable
ReallocMem Reallocate an existing block of storage
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : A simple exampl of using GetMem and FreeMem
  charPtr  : PChar;

  // Allocate storage for 4 characters
  GetMem(charPtr, 4 * SizeOf(Char));

  // Assign to these characters
  charPtr^ := 'A';
  charPtr^ := 'B';
  charPtr^ := 'C';
  charPtr^ := #0;  // String terminator

  // Now display these values
  Dec(charPtr, 3);
  ShowMessage('Characters stored = '+charPtr);

  // Now free the memory for these characters
Show full unit code
   Characters stored = ABC
Delphi Programming Neil Moffatt 2002 - 2016. All rights reserved.  |  Home Page