Generics Memory Sorunu

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
edo
Üye
Mesajlar: 40
Kayıt: 05 Haz 2005 11:12

Generics Memory Sorunu

Mesaj gönderen edo »

Merhaba,

Aşağıdaki gibi class tanımlarım var. X ve Z li kopyaları neden oluşturduğumu aşağıda yazıcam.

Kod: Tümünü seç

      TVariable_Attr = class
      private
         FName: String;
         FValue: String;
      public
         property Name: String read FName write FName;
         property Value: String read FValue write FValue;
      end;

      TVariable_Attr_List = TObjectList<TVariable_Attr>;

      TVariable = class
      private
         FData_ID: Integer;
         FVariable_Attr_List: TVariable_Attr_List;
      public
         property Data_ID: Integer read FData_ID write FData_ID;
         property Variable_Attr_List: TVariable_Attr_List
           read FVariable_Attr_List write FVariable_Attr_List;
         constructor Create;
         destructor Destroy; override;
      end;

      TVariable_List = TObjectList<TVariable>;

      TVariable_Attrx = class
      private
         FName: String;
         FValue: String;
      public
         property Name: String read FName write FName;
         property Value: String read FValue write FValue;
      end;

      TVariable_Attrx_List = TObjectList<TVariable_Attrx>;

      TVariablex = class
      private
         FData_ID: Integer;
         FVariable_Attr_List: TVariable_Attrx_List;
      public
         property Data_ID: Integer read FData_ID write FData_ID;
         property Variable_Attr_List: TVariable_Attrx_List read FVariable_Attr_List write FVariable_Attr_List;
         constructor Create;
         destructor Destroy; override;
      end;

      TVariable_Listx = TObjectList<TVariablex>;

      TVariable_Attrz = class
      private
         FName: String;
         FValue: String;
      public
         property Name: String read FName write FName;
         property Value: String read FValue write FValue;
      end;

      TVariable_Attrz_List = TObjectList<TVariable_Attrz>;

      TVariablez = class
      private
         FData_ID: Integer;
         FVariable_Attr_List: TVariable_Attrz_List;
      public
         property Data_ID: Integer read FData_ID write FData_ID;
         property Variable_Attr_List: TVariable_Attrz_List read FVariable_Attr_List write FVariable_Attr_List;
         constructor Create;
         destructor Destroy; override;
      end;

      TVariable_Listz = TObjectList<TVariablex>;
Ve aşağıdaki gibi Constructor ve Destructor'larım var:

Kod: Tümünü seç

{ TVariable_List }

constructor TVariable.Create;
begin
   inherited Create;

   FVariable_Attr_List := TVariable_Attr_List.Create;
end;

destructor TVariable.Destroy;
begin
   FVariable_Attr_List.Free;

   inherited Destroy;
end;

{ TVariablex }

constructor TVariablex.Create;
begin
   inherited Create;

   FVariable_Attr_List := TVariable_Attrx_List.Create;
end;

destructor TVariablex.Destroy;
begin
   FVariable_Attr_List.Free;

   inherited Destroy;
end;

{ TVariablez }

constructor TVariablez.Create;
begin
   inherited Create;

   FVariable_Attr_List := TVariable_Attrz_List.Create;
end;

destructor TVariablez.Destroy;
begin
   FVariable_Attr_List.Free;

   inherited Destroy;
end;
Ve aşağıdaki şekilde bu Class'ları oluşturuyor ve kullanıyorum:

Kod: Tümünü seç

var
   t: TVariable_List;
   x: TVariable_Listx;
   z: TVariable_Listz;
  i: Integer;
  j: Integer;
begin
   Screen.Cursor := crHourGlass;
   try
      t := TVariable_List.Create;

      for j := 0 to 10000000 do
      begin
         t.Add(TVariable.Create);
         {t.last.Data_ID := j;
         for i := 0 to 1000 do
         begin
            t.Last.Variable_Attr_List.Add(TVariable_Attr.Create);
            t.Last.Variable_Attr_List.Last.Name := IntToStr(i);
            t.Last.Variable_Attr_List.Last.Value := IntToStr(i);
         end;}
      end;

      t.Free;

      x := TVariable_Listx.Create;

      for j := 0 to 10000000 do
      begin
         x.Add(TVariablex.Create);
      end;

      x.Free;

      z := TVariable_Listx.Create;

      for j := 0 to 10000000 do
      begin
         z.Add(TVariablex.Create);
      end;

      z.Free;
   finally
      Screen.Cursor := crDefault;
   end;
end;
Uygulamam ilk açıldığında memory kullanımı 1.8 Mb, bu procedure çalıştmasını bitirdikten sonra (Comment'li kısım açık ya da kapalı fark etmiyor) memory kullanımı 3.6 Mb olarak kalıyor. Free etmediğim bir şeyler var bu yüzden mi bu durum oluşuyor diye x ve y'li class kopyalarını oluşturdum ve koduma ekledim, döngü değişkeninin değerini 10.000.000 gibi absürt bir değere çektim fark eden bir şey yok. Uygulama 1.8 Mb memory tüketirken bu metod çalışmasını bitirince 3.6 Mb olarak kalıyor. Ayrıca bu metodu bir kaç defa da çalıştırsam metod kullanımı 3.6 Mb'ın üstüne de çıkmıyor. Acaba Delphi'nin belleğe yükleyip de temizlemediği bir şey olabilir mi ya da sorunun nedeni ne olabilir?

Kullandığım ortam: Delphi XE2 (Update 1), Windows 10
edo
Üye
Mesajlar: 40
Kayıt: 05 Haz 2005 11:12

Re: Generics Memory Sorunu

Mesaj gönderen edo »

Şöyle enteresan bir şey var. Döngü değişkenini 0 to 1000 yapıp on kere çalıştırdığımda (butonun altına kodu koyup 10 kere tıklayarak) bellek tüketimi artmazken 0 to 10.000 yaptığımda bellek tüketimi 1,8 MB'dan 2,5 MB'a çıkıyor :)
Kullanıcı avatarı
kimimben
Üye
Mesajlar: 129
Kayıt: 28 Oca 2016 04:41
Konum: İstanbul

Re: Generics Memory Sorunu

Mesaj gönderen kimimben »

edo
Üye
Mesajlar: 40
Kayıt: 05 Haz 2005 11:12

Re: Generics Memory Sorunu

Mesaj gönderen edo »

Güzel ipucuymuş ama ne yazık ki işe yaramadı. Herhangi bir memory leak gözükmüyor. (Özellike leak oluşturursam tabii onlar gözüküyor orada sıkıntı yok yani)
ertank
Kıdemli Üye
Mesajlar: 1716
Kayıt: 12 Eyl 2015 12:45

Re: Generics Memory Sorunu

Mesaj gönderen ertank »

Benim anladığım FastMM bağlantılı bir durum. Aşağıdaki linkleri inceleyebilirsiniz.

http://stackoverflow.com/questions/4463 ... y-back-why
http://stackoverflow.com/questions/4475 ... sed-memory

MemoryLeak olmadıktan sonra bu tarz şeylere pek takılmaya gerek yok bence.
Cevapla