Recent

Author Topic: [Solved] Out of Memory  (Read 12869 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12950
  • FPC developer.
Re: Out of Memory
« Reply #15 on: October 26, 2018, 10:35:22 pm »
Change to 64-bit.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 632
    • Double Dummy Solver - free download
Re: Out of Memory
« Reply #16 on: October 26, 2018, 10:57:13 pm »
If I change to 64 bit, will the app run on 32-bit PCs. Most of my users only have them.
Lazarus 4.8 FPC 3.2.2 x86_64-win64-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12950
  • FPC developer.
Re: Out of Memory
« Reply #17 on: October 26, 2018, 11:04:15 pm »
If I change to 64 bit, will the app run on 32-bit PCs. Most of my users only have them.

True. Maybe you can differentiate between those who have large datasets and not.

The trouble is copying of a tstringlist takes twice the memory. Around 1GB you are thus hitting the 2GB limit of 32-bit.

You can go a bit further with {$peflags IMAGE_FILE_LARGE_ADDRESS_AWARE}, but you are skirting the edge, and the next increase might bump you over again.
« Last Edit: October 26, 2018, 11:22:05 pm by marcov »

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Out of Memory
« Reply #18 on: October 26, 2018, 11:16:21 pm »
[...]

However, I RUN OUT OF MEMORY when I tried to execute SL.SaveToFile after the main loop.

I need another approach to create the output file. Any suggestions?

Just a thought: you could try writing directly to the file instead of using a TStringList as a kind of stepping-stone. That will surely use quite a lot less memory.

It all depends on what you're really trying to do.
« Last Edit: October 26, 2018, 11:18:17 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 632
    • Double Dummy Solver - free download
Re: Out of Memory
« Reply #19 on: October 27, 2018, 02:01:04 am »
I've solved my problem by using TFileStream. I've replaced every SL.Add() with AddtoStream() . Then
Code: Pascal  [Select][+][-]
  1. procedure AddToStream(s : string):
  2. begin
  3.   s := s+#13#10; // carriage return/line feed
  4.   fs.Write(s[1], length(s));
  5. end;

Of course fs is created and freed in the appropriate spot.
Lazarus 4.8 FPC 3.2.2 x86_64-win64-win32/win64

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Out of Memory
« Reply #20 on: October 27, 2018, 02:46:31 am »
Unless you need a specific line termination you should use LineEnding:

Code: Pascal  [Select][+][-]
  1. procedure AddToStream(s : string):
  2. begin
  3.   s := s + LineEnding; // platform dependant line termination
  4.   fs.Write(s[1], length(s));
  5. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 632
    • Double Dummy Solver - free download
Re: [Solved] Out of Memory
« Reply #21 on: October 27, 2018, 03:40:11 am »
Thanks
Lazarus 4.8 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 19449
  • Glad to be alive.
Re: [Solved] Out of Memory
« Reply #22 on: October 27, 2018, 07:28:24 am »
If you write strings to a stream why not use TStream.WriteAnsiString;
Any "programmer" that knows only one programming language is not a programmer

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 632
    • Double Dummy Solver - free download
Re: [Solved] Out of Memory
« Reply #23 on: October 27, 2018, 06:20:22 pm »
When I tried to use
Code: Pascal  [Select][+][-]
  1. s := s + LineEnding;
  2. fs.WriteAnsiString(s);
 
3 bytes of non-text info (or something like that) was put in front of every line in my output file.

How would I use WriteAnsiString to achieve an output line that looks like
Code: Pascal  [Select][+][-]
  1. fs.Write(s[1],length(s));
Lazarus 4.8 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 19449
  • Glad to be alive.
Re: [Solved] Out of Memory
« Reply #24 on: October 27, 2018, 06:52:03 pm »
TStream.WriteAnsiString already adds a line ending......
So:
Code: Pascal  [Select][+][-]
  1. fs.WriteAnsiString(s);
is enough...

You are doubling it up...
« Last Edit: October 27, 2018, 06:53:48 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: [Solved] Out of Memory
« Reply #25 on: October 27, 2018, 07:57:32 pm »
If you write strings to a stream why not use TStream.WriteAnsiString;
Be careful. This writes the string length in front of the character array. Maybe this is not wanted. The code posted above definitely writes only the characters.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 632
    • Double Dummy Solver - free download
Re: [Solved] Out of Memory
« Reply #26 on: October 27, 2018, 08:08:24 pm »
Without Lineending, the file is not easily readable or editable in something like Notebook. Weird characters still appear. I haven't been able to get WriteAnsiString to provide output like Write(s[1],length(s)), after adding LineEnding to "s".
Lazarus 4.8 FPC 3.2.2 x86_64-win64-win32/win64

Zoran

  • Hero Member
  • *****
  • Posts: 1990
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: [Solved] Out of Memory
« Reply #27 on: October 27, 2018, 08:37:14 pm »
Without Lineending, the file is not easily readable or editable in something like Notebook. Weird characters still appear. I haven't been able to get WriteAnsiString to provide output like Write(s[1],length(s)), after adding LineEnding to "s".

You didn't notice what Werner said. So, stick to what you originally wrote, there is nothing wrong in your code. Here is what I would do:

Code: Pascal  [Select][+][-]
  1. procedure AddToStream(S: String):
  2. const
  3. { Yes, you can use platform dependent LineEnding constant, as Lucamar suggested;
  4.  but I guess you probably want your files to be consistent on all platforms, so: }
  5.   CRLF = #13#10;
  6.   L = Length(CRLF);
  7. begin
  8.   if S > '' then
  9.     fs.Write(S[1], Length(S));
  10.   fs.Write(CRLF[1], L);
  11. end;
  12.  
« Last Edit: October 27, 2018, 08:40:42 pm by Zoran »
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

 

TinyPortal © 2005-2018