Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Arraygrösse und Stack Overflow (https://www.delphipraxis.net/214682-arraygroesse-und-stack-overflow.html)

johndoe049 21. Feb 2024 12:13

Arraygrösse und Stack Overflow
 
Hallo,
wir haben im Moment einen Stack Overflow, wenn wir ein "kleines" Array definieren.

array [0..1000] of array [0..10] of array [0..10000] of integer

Brauchen wir für eine Selektionsberechnung.

Array ist derzeit statisch definiert. Würde das bei einem dynamischen Array funktionieren?

Eigentlich brauchen wir das noch grösser, aber Datenbank auf SSD ist langsamer als der Arbeitsspeicher...

Kas Ob. 21. Feb 2024 12:36

AW: Arraygrösse und Stack Overflow
 
Hi,

This small array ("kleines") made me spill my coffee !

The size of this "Small" is
1001*11*10001 = 110121011 element then for integers
110121011 * 4 = 440484044 byte , and this is more than 400 Megabyte !

So, no the stack will no hold that, you have to switch to dynamic arrays and it will (or might) work if you have enough RAM to allocate such continuous allocation.
Or if possible divide the calculation process into real small pieces, even then don't use the stack allocated, use the heap.

johndoe049 21. Feb 2024 12:46

AW: Arraygrösse und Stack Overflow
 
Zitat:

Zitat von Kas Ob. (Beitrag 1533732)
Hi,

This small array ("kleines") made me spill my coffee !

The size of this "Small" is
1001*11*10001 = 110121011 element then for integers
110121011 * 4 = 440484044 byte , and this is more than 400 Megabyte !

So, no the stack will no hold that, you have to switch to dynamic arrays and it will (or might) work if you have enough RAM to allocate such continuous allocation.
Or if possible divide the calculation process into real small pieces, even then don't use the stack allocated, use the heap.

Hallo,

wie schaltet man das auf Heap um? Arbeitsspeicher ist genug vorhanden (ca. 196 GB Ram)

Klaus01 21. Feb 2024 13:08

AW: Arraygrösse und Stack Overflow
 
.. wenn Du ein dynamisches Array nimmst, das kommt auf den Heap

Grüße
Klaus

himitsu 21. Feb 2024 13:12

AW: Arraygrösse und Stack Overflow
 
Ein statisches Array, als lokale Variable, liegt nunmal direkt auf dem Stack, so wie alle lokalen Variablen.

* ja, den Stack kann man vergrößern, aber 400 MB wäre sowieso nicht möglich
* also nein, besser ist es das Array eben nicht dort zu haben.

z.B. als dynamisches Array
oder als Pointer im Heap (sonstwo im virtuellen Programm-Speicher)

Dann ist es sowieso schwer, einen zusammenhängenden Block dieser Größe in einem 32 Bit-Programm reservieren zu wollen. (hast da standardmäßig nur 2 GB und das auch nicht relativ schön fragmentiert)
Erfahrungsgemäß geht es bis 700MB "meistens" noch gut, aber absichtlich provozieren würde ich das nicht wollen.

* Vielleicht also besser komplett anders herangehen.
Ist das Array überhaupt zum Großzeil mit Daten gefüllt? Wenn nicht, dann besser als Liste/Dictonary.

* oder als mehrdimensionals dynamisches
Delphi-Quellcode:
array of array of array of integer
mit
Delphi-Quellcode:
SetLength(x, 1001, 11, 10001);
.
Hier wären es dann 1001*11 Mal 10001*4 "kleine" Blöckchen im Speicher.

Blup 21. Feb 2024 13:18

AW: Arraygrösse und Stack Overflow
 
Oder auch so:
Delphi-Quellcode:
type
  PMyArray = ^TMyArray;
  TMyArray = array [0..1000] of array [0..10] of array [0..10000] of integer;

procedure TestMyArray;
var
  MyArray: PMyArray;
begin
  New(MyArray);
  try

  finally
    Dispose(MyArray);
  end;
end;

Kas Ob. 21. Feb 2024 13:23

AW: Arraygrösse und Stack Overflow
 
Hallo,

wie schaltet man das auf Heap um? Arbeitsspeicher ist genug vorhanden (ca. 196 GB Ram)[/QUOTE]

As Klaus said, dynamic allocations are on the heap (array or whatever).

You may want to look at the answers for this question
https://stackoverflow.com/questions/...uble-in-delphi

As for more in depth details, i think there is many here will not suffer from language barrier to explain any more questions you might have.

Personally i would stay from generics and anonymous methods when there is complex and long calculations, mostly to keep it simple and readable.

Also you can might consider separate you construct (your data build) to array of matrixes, this way you will not need one big continuous piece of RAM to fit it, this might be easier to manage and will lead to more readable code, but this is personal choice.

himitsu 21. Feb 2024 13:25

AW: Arraygrösse und Stack Overflow
 
Wie bereits gesagt wurde,
z.B. als Variable in einem Objekt,
oder als Pointer auf dein Array (New, GetMem, GetMemory, ...)
oder eben als dynamisches Array (welches intern ein Pointer ist)

Uwe Raabe 21. Feb 2024 13:31

AW: Arraygrösse und Stack Overflow
 
Zitat:

Zitat von Klaus01 (Beitrag 1533736)
.. wenn Du ein dynamisches Array nimmst, das kommt auf den Heap

Insbesondere kommt es in kleineren Blöcken. Lediglich die letzten
Delphi-Quellcode:
array of Integer
werden jeweils als ein Block benötigt. Alle anderen enthalten Pointer auf die Sub-Arrays.

Zitat:

Zitat von johndoe049 (Beitrag 1533733)
Arbeitsspeicher ist genug vorhanden (ca. 196 GB Ram)

Das kommt darauf an ob es eine 32 oder 64-Bit Anwendung ist. Bei 32-Bit kann der Heap auch nicht den Hauptspeicher voll ausnutzen. Mit 400 MB in Blöcken von max. 40KB bist du aber wohl noch im grünen Bereich.

Zitat:

Zitat von Blup (Beitrag 1533738)
Delphi-Quellcode:
type
  PMyArray = ^TMyArray;
  TMyArray = array [0..1000] of array [0..10] of array [0..10000] of integer;

Das würde ich nicht empfehlen, da das weiterhin ein zusammenhängendes Array erzeugt - lediglich auf dem Heap. Das kann unter Win32 bei 400MB schon engwerden.

Mal abgesehen davon ließe sich das sowieso auch deutlich einfacher schreiben:
Delphi-Quellcode:
TMyArray = array[0..1000, 0..10, 0..10000] of integer;
Besser wäre sowas:
Delphi-Quellcode:
type
  TMyArray = array of array of array of Integer;

procedure Test;
var
  arr: TMyArray;
begin
  SetLength(arr, 1001, 11, 10001);
  ...
end;

johndoe049 21. Feb 2024 13:42

AW: Arraygrösse und Stack Overflow
 
[QUOTE=himitsu;1533737]
* Vielleicht also besser komplett anders herangehen.
Ist das Array überhaupt zum Großzeil mit Daten gefüllt? Wenn nicht, dann besser als Liste/Dictonary.

Leider ja. Es werden Vorselektionen aus einer Datenmenge von ca. 18.000.000 Daten in Chargen Daten zusammengefasst und dann die jeweiliga Abweichungen/Gleichheiten ermittelt und gruppiert.

Das ganze solange, bis alle Daten geprüft wurden. Dann werden die Gruppen geprüft und irgendwann kommt als Ergebniss eine Produktionsabweichungsanalyse heraus.

Mit so einer Datenmenge hatten wir bisher nichts gemacht.

Anwendung ist 64bit. Sollte mit dem Arbeitsspeicher kein Problem sein.


Zitat:

Mal abgesehen davon ließe sich das sowieso auch deutlich einfacher schreiben: TMyArray = array[0..1000, 0..10, 0..10000] of integer;
Wir übernehmen einen Sourcecode von einem anderen Programmierer. Bevor wir was optimieren, müssen wir das erst mal möglichst nahe umbauen. Bisher wird die Auswertung nicht im Arbeitsspeicher sondern mit Dateien auf Festplatte gemacht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:54 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz