hasMemory

Mark Waddingham mark at livecode.com
Fri Jun 10 10:05:50 CEST 2016


On 2016-06-10 06:14, Mark Wieder wrote:
> Here's a working cross-platform replacement for the flaky built-in
> hasMemory function.

The builtin 'hasMemory' function is not flaky - the question it asks 
just has no meaning on modern operating systems (and should probably 
actually be removed!).

For example, change your mouseUp to this:

on mouseUp
    put freeMemory() into tBytesAvailable

    repeat with i = 1 to tBytesAvailable * 4
       put "a" after tString
    end repeat

    put "apparently there were" && tBytesAvailable && \
        "bytes of memory available" & return after field 1
    put "but I managed to allocate a string which requires" && \
        the length of tString && "bytes" & return after field 1
end mouseUp

And try running it on a modern Mac (although I have no reason to suspect 
it would be different on any other platform we support). In my case I 
got:

    apparently there were 8894284 bytes of memory available
    but I managed to allocate a string which requires 35577136 bytes

The concept of 'free memory' on multi-user, pre-emptive, user/kernel 
space split operating systems doesn't exist as the kernel allocates any 
physical resources it has (including disk space to provide virtual 
memory) to where it is needed at any point it wishes.

For example, a large amount of physical memory pages at any one time is 
likely to be used to cache blocks on disks for files which are currently 
in use. As the majority of these pages (those which have not been 
modified in memory) can be dumped at any time, that measure does not 
reflect how much memory any one process could allocate. Similarly, 
processes which are inactive and consuming physical memory are likely to 
have most of their memory usage paged to disk (i.e. into virtual memory) 
when another active process needs more memory.

Warmest Regards,

Mark.

P.S. The 'hasMemory' function in LiveCode actually does the best it can 
do - it sees if it can allocate a contiguous block of memory of the size 
that has been requested (using malloc) and if that succeeds, it frees 
the block and returns true. This should mean that (assuming nothing on 
the system suddenly consumes all physical and virtual ram) you should be 
able to do an action which requires that amount of memory immediately 
after:

void MCLegacyEvalHasMemory(MCExecContext& ctxt, uinteger_t p_bytes, 
bool& r_bool)
{
	char *t_buffer = nil;
	r_bool = nil != (t_buffer = (char*)malloc(p_bytes));
	free(t_buffer);
}

-- 
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps



More information about the use-livecode mailing list