PDA

View Full Version : Memory size



Jimmy Valavanis
02-07-2007, 05:33 AM
System: Windows
Compiler/IDE: Delphi 5
Libraries/API: None

When you go in "Project" menu and click the "Information for.." item
a dialog box appears and give some statistics about Code size and data size.

How can I get in my program the Data size as a function result??

I mean I'm looking for a function, for example:

function GetProjectDataSize: integer; that returns the memory size of the static structures that are used.

Setharian
02-07-2007, 06:42 AM
nothing like that exists, however you could get that info from the PE header by looking for .data/DATA section and if you find it look at SizeOfRawData member...

Jimmy Valavanis
02-07-2007, 08:52 AM
I've made the next function:


const
IMAGE_NT_OPTIONAL_HDR32_MAGIC = $10b;
type
PPESectionHeaderArray = ^TPESectionHeaderArray;
TPESectionHeaderArray = array[0..$FFFF] of TImageSectionHeader;

function I_GetSizeOfRawData(fname: string = ''): LongWord;
var
f: file;
PEHeaderOffset, PESig: Cardinal;
EXESig: Word;
PEHeader: TImageFileHeader;
PESectionHeaders: PPESectionHeaderArray;
PEOptHeader: TImageOptionalHeader;
i: integer;
begin
if fname = '' then
fname := ParamStr(0);
filemode := 0;
{$I-}
assign(f, fname);
reset(f, 1);
BlockRead(f, EXESig, SizeOf(EXESig));
if EXESig <> $5A4D {'MZ'} then
begin
close(f);
result := 0;
exit;
end;
seek(f, $3C);
BlockRead(f, PEHeaderOffset, SizeOf(PEHeaderOffset));
if PEHeaderOffset = 0 then
begin
close(f);
result := 0;
exit;
end;
seek(f, PEHeaderOffset);
BlockRead(f, PESig, SizeOf(PESig));
if PESig <> $00004550 {'PE'#0#0} then
begin
close(f);
result := 0;
exit;
end;
BlockRead(f, PEHeader, SizeOf(PEHeader));
if PEHeader.SizeOfOptionalHeader <> SizeOf(TImageOptionalHeader) then
begin
close(f);
result := 0;
exit;
end;
BlockRead(f, PEOptHeader, SizeOf(PEOptHeader));
if PEOptHeader.Magic <> IMAGE_NT_OPTIONAL_HDR32_MAGIC then
begin
close(f);
result := 0;
exit;
end;
GetMem(PESectionHeaders, PEHeader.NumberOfSections * SizeOf(TImageSectionHeader));
BlockRead(f, PESectionHeaders^, PEHeader.NumberOfSections * SizeOf(TImageSectionHeader));
result := 0;
for i := 0 to PEHeader.NumberOfSections - 1 do
result := result + PESectionHeaders[i].SizeOfRawData;
close(f);
{$I+}
if IOResult <> 0 then
result := 0;
end;




I use the SizeOfRawData member, but the function returns the size
of code plus the size of const data of program (I suppose).

What I need is a function to return all the data size (i.e. not only tables
that are initialized).

E.g.

var tbl1 = array[1..1024*1024] of LongWord; // <- not constant!!!

Any ideas??