Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  1 474

•  TDictionary Custom Sort  4 162

•  Fast Watermark Sources  3 847

•  3D Designer  5 932

•  Sik Screen Capture  4 109

•  Patch Maker  4 656

•  Айболит (remote control)  4 563

•  ListBox Drag & Drop  3 728

•  Доска для игры Реверси  89 307

•  Графические эффекты  4 897

•  Рисование по маске  3 919

•  Перетаскивание изображений  3 301

•  Canvas Drawing  3 598

•  Рисование Луны  3 396

•  Поворот изображения  2 895

•  Рисование стержней  2 568

•  Paint on Shape  1 973

•  Генератор кроссвордов  2 738

•  Головоломка Paletto  2 160

•  Теорема Монжа об окружностях  2 841

•  Пазл Numbrix  1 945

•  Заборы и коммивояжеры  2 515

•  Игра HIP  1 557

•  Игра Go (Го)  1 493

•  Симулятор лифта  1 766

•  Программа укладки плитки  1 513

•  Генератор лабиринта  1 878

•  Проверка числового ввода  1 629

•  HEX View  1 810

•  Физический маятник  1 649

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Экономия памяти с дублирующими значениями в таблице



Оформил: DeeCo

{ 
  Often there are lots of duplicate strings in a program, 
  for example take the following database: 

  Author    Title 
  aaa        xxx 
  aaa        yyy 
  aaa        zzz 

  Suppose you want to read it into your program and 
  create one record or class per line: 
}

 while not Table.EOF do
 begin
   author := Table['Author'];
   title := Table['Title'];
   MyClass := TMyClass(author, title);
   StoreItSomewhere(MyClass);
   Table.Next;
 end;

 { 
  You end up with three different strings containting the same author. 
  No problem with that as long as there are only three of them. 
  Now suppose you have got a database of 100,000 entries and lots of 
  duplicate author names. 
  If you do the above again you will end up with a huge memory overhead 
  due to fragmentation of the free memory on the heap. 
  In addition to that freeing the objects will result in a noticable delay 
  of the program while the memory manager is busy merging free memory blocks. 
  On the other hand, Delphi's AnsiStrings use reference counting so it 
  would be possible to assign the same string to many string variables 
  without copying the contents: 
}

 Author := 'aaa';
 for i:=0 to 100000 do
 begin
   MyClass := TMyClass(Author);
   StoreItSomewhere(MyClass);
 end;

 { 
  This will create 100000 strings containting 'aaa', 
  but store the actual contents only once, 
  because they are assigned the same string constant. 

  It would be nice to do the same with the above database example, 
  wouldn't it? But how do we get Delphi to realize that 
  the content is really the same? 

  Answer: A StringList 
}

 authors := TStringList.Create;
 authors.Sorted := true;
 authors.Duplicates := dupIgnore;
 while not Table.EOF do
 begin
   author := Table['Author'];
   title := Table['Title'];

   authors.Add(author);
   authors.Search(author, Idx);
   author := authors[Idx];

   MyClass := TMyClass(author, title);
   StoreItSomewhere(MyClass);
   Table.Next;
 end;
 authors.free;

 { 
  This will only keep one string of each author's name and assign 
  it to all other occurences, even after the StringList has been freed again. 
  The wonders of reference counting! 

  I admit that the above example looks a bit odd. Nobody would load 
  such a database into memory. But I had a similar problem today and by using 
  the trick above I managed to reduce the memory usage of my program from 120 megabytes to 10 
  (basically nothing, given that it started out with 8 mb). 
}







Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте