Page 1 of 1

#MaxMem: How much is too much?

Posted: 03 Oct 2013, 09:21
by JnLlnd
My program process a text file. Part of the process is more efficiently executed with all the content of the file in one string variable. This is the only large variable in the script.

To allow processing of files larger than 64 MB, I need to increase the maximal variable size using the #MaxMem instruction.

But how do I know how much I can increase the size of variables without exceeding the memory capacity of the PC?

In other words, two questions:

1) Is there a DLLCall function that would tell me how much RAM is available to my script?

2) From my understanding, I could not ajust the #MaxMem directive according the the RAM available. But is there another approach to this issue?

Thanks!

Re: #MaxMem: How much is too much?

Posted: 03 Oct 2013, 10:21
by LinearSpoon
FileRead already ignores #MaxMem and just reads the whole file into a variable.

If you're really worried about coming across a huge file that would exceed your RAM, try a file parsing loop or a file object to only read a portion at a time...

Re: #MaxMem: How much is too much?

Posted: 03 Oct 2013, 12:29
by JnLlnd
LinearSpoon wrote:FileRead already ignores #MaxMem and just reads the whole file into a variable.
Hum... That's good to know. Thank you for this info. Of course, if I read a file bigger than available RAM, the script will break.
LinearSpoon wrote:If you're really worried about coming across a huge file that would exceed your RAM, try a file parsing loop or a file object to only read a portion at a time...
Yes, this is my fallback solution. But since some processing has to be applied to the content as a whole (for example, determine a character that is *not* actually used in the file), this will require a bit of additional coding.

Until I can do that, i was wondering if a command (DLLCall?) could tell me how much memory is available, check the file size and display a clean error message if the RAM is not sufficient.

Re: #MaxMem: How much is too much?

Posted: 03 Oct 2013, 21:53
by lexikos
If a) you exceed #MaxMem or b) a memory allocation fails due to insufficient free memory, you get an error message and the current script thread is aborted. Either way, basically the same result, so having an artificial limit isn't at all useful for allowing your script to work. Instead,
The purpose of limiting each variable's capacity is to prevent a buggy script from consuming all available system memory.
If your script is functioning as intended and you're reaching the limit, I'd suggest adding #MaxMem 4095 and then forgetting about it. It's really not very useful, and has actually been removed from v2 (i.e. there's no arbitrary limit).

Re: #MaxMem: How much is too much?

Posted: 03 Oct 2013, 22:11
by JnLlnd
Good points, Lexikos. Then, I could spot in my code the commands that could make the script reach the "Out of memory" limit and use Try/Catch to return a proper error message to the user.

Are the Try/Catch very processor intensive if inserted in a very critical section of a script? If yes, instead of using Try/Catch in every iteration of a critical loop, I could run it only once in a while creating
a phony large variable?

Thank you all again for your sound advice.

Re: #MaxMem: How much is too much?

Posted: 04 Oct 2013, 08:07
by DataLife
Check out jNizM's script here http://www.auto-hotkey.com/boards/viewt ... ?f=6&t=142

MemoryInfo v0.1

Info about Memory:
Total, Free & Used Memory, Clear Memory

Re: #MaxMem: How much is too much?

Posted: 04 Oct 2013, 08:53
by JnLlnd
Yes! Just found the reply from jNizM to a similar discussion on the old forum. Exactly what I was looking for.

With this, no need to Try/Catch. I'll remove the #MaxMem restriction and test if enough RAM is available before loading a file in a variable.

Thanks!

Re: #MaxMem: How much is too much?

Posted: 04 Oct 2013, 12:45
by jNizM