Why does it take up so much memory

From: NB (nej_at_tak.dk)
Date: 09/18/04


Date: Sat, 18 Sep 2004 18:30:11 +0200

When I run this simple application it takes up a huge amount of memory
(in the Task Manager it says more than 100 megabytes). Why is that?
As I see it, 30.000 objects are created containing each 25 characters,
therefor I'd expect for the application to take up around 30.000 x 25 =
750.000 bytes of memory but that's obviously not the case.
Could anyone please help me and explain what I'm doing wrong?

// Test.dpr
program Test;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  uTest in 'uTest.pas',
  uTestList in 'uTestList.pas';

var
  FTestList: TTestList;

begin
  FTestList := TTestList.Create;

  try
    FTestList.LoadFromFile('file.txt');
  finally
    FTestList.Free;
  end;
end.

// uTest.pas
unit uTest;

interface

type
  TTest = class
  private
    FValue1: string;
    FValue2: string;
    FValue3: string;
    FValue4: string;
    FValue5: string;
  public
    constructor Create(AValue1, AValue2, AValue3, AValue4, AValue5:
string);
  end;

implementation

{ TTest }

constructor TTest.Create(AValue1, AValue2, AValue3, AValue4,
  AValue5: string);
begin
  FValue1 := AValue1;
  FValue2 := AValue1;
  FValue3 := AValue1;
  FValue4 := AValue1;
  FValue5 := AValue1;
end;

end.

// uTestList.pas
unit uTestList;

interface

uses
  Classes,
  SysUtils,
  uTest;

type
  TTestList = class
  private
    FCount: Integer;
    FTest: array of TTest;
    function Get(AIndex: Integer): TTest;
    procedure Put(AIndex: Integer; const ATest: TTest);
  public
    property Test[AIndex: Integer]: TTest read Get write Put; default;

    procedure Add(ATest: TTest);
    procedure LoadFromFile(AFileName: string);
  end;

implementation

{ TTestList }

procedure TTestList.Add(ATest: TTest);
begin
  Inc(FCount);
  SetLength(FTest, FCount);
  FTest[FCount - 1] := ATest;
end;

function TTestList.Get(AIndex: Integer): TTest;
begin
  Result := FTest[AIndex];
end;

procedure TTestList.LoadFromFile(AFileName: string);
var
  StringList: TStringList;
  Row: Integer;

  Line: string;
  Value1: string;
  Value2: string;
  Value3: string;
  Value4: string;
  Value5: string;
begin
  StringList := TStringList.Create;

  try
    StringList.LoadFromFile(AFileName);

    // Approx. 30000 lines.
    for Row := 0 to StringList.Count - 1 do
    begin
      Value1 := Copy(Line, 1, 7);
      Value2 := Copy(Line, 8, 3);
      Value3 := Copy(Line, 14, 4);
      Value4 := Copy(Line, 19, 6);
      Value5 := Copy(Line, 49, 5);

      Add(
        TTest.Create(
          Value1,
          Value2,
          Value3,
          Value4,
          Value5,
        )
      );

      Write(#13, IntToStr(FCount));
    end;
  finally
    StringList.Free;
  end;
end;

procedure TTestList.Put(AIndex: Integer; const ATest: TTest);
begin
  FTest[AIndex] := ATest;
end;

end.



Relevant Pages

  • Newbie question: Writing your own class
    ... This may be stupid question, but I am struggling, so any help would ... Definition: String; ... TTest; (actually, this is declared and called from another unit, ...
    (comp.lang.pascal.delphi.misc)
  • Re: Newbie question: Writing your own class
    ... procedure Set_Definition(NewDef: String); ... myClassInstance:TTest; ... the proper way that you should be doing this if Properties is ...
    (comp.lang.pascal.delphi.misc)
  • Re: Newbie question: Writing your own class
    ... procedure Set_Definition(NewDef: String); ... myTest: TTest; (actually, this is declared and called from another unit, ... You need to create an instance of TTest, just as you would create an instance of TStringList or TForm1. ...
    (comp.lang.pascal.delphi.misc)
  • Re: MASD syntax to load the current address in C.A at runtime
    ... so my memory on A=PC is not as good. ... CODE object, the CRC on the CODE object will change. ... easier to store individual pieces as the time string was built. ...
    (comp.sys.hp48)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)