hasMemory

Mark Wieder mwieder at ahsoftware.net
Fri Jun 10 00:14:17 EDT 2016


On 06/09/2016 07:35 PM, Thierry Douez wrote:
> Hi Mark,
>
> Not sure if this will help you, but anyway
> it's an interesting post.

Fortunately you and I have similar definitions of "interesting" <g>

Unfortunately it's no real help for what I was trying. After spending 
part of a day playing around with LCB, I'm concluding it's really not 
worth the effort. I got all excited seeing some of what Dar's been 
turning out, but I can't see there's anything to gain by turning a 
working library into an extension, and a lot of wasted time to lose.

Here's a working cross-platform replacement for the flaky built-in 
hasMemory function.

/**
* Return the number of free bytes
*/

on mouseUp
    put freeMemory() && "bytes" into field 1
    put cr & hasMem(2000000) after field 1
end mouseUp

function hasMem pDesiredBytes
    return freeMemory() > pDesiredBytes
end hasMem

function freeMemory
    local tPlatform
    local tFreeMem

    put the platform into tPlatform
    if "Mac" is in tPlatform then
       put availableMemOSX() into tFreeMem
    else if "Win" is in tPlatform then
       put availableMemWindows() into tFreeMem
    else
       put availableMemLinux() into tFreeMem
    end if
    return tFreeMem
end freeMemory

private function availableMemOSX
    local tFreeMem
    local tPageSize
    local tFreePages

    put shell ("vm_stat -c 1 1") into tFreeMem
    put word -2 of line 1 of tFreeMem into tPageSize
    put word 1 of line -1 of tFreeMem into tFreePages
    return tFreePages * tPageSize / 1024
end availableMemOSX

private function availableMemWindows
    local tFreeMem

    put shell ("wmic OS get FreePhysicalMemory /Value") into tFreeMem
    set the linedelimiter to "="
    return line -1 of tFreeMem * 1024
end availableMemWindows

private function availableMemLinux
    local tFreeMem

    put shell ("vmstat -s") into tFreeMem
    filter tFreeMem with "*free memory"
    return word 1 of tFreeMem * 1024
end availableMemLinux

-- 
  Mark Wieder
  ahsoftware at gmail.com




More information about the use-livecode mailing list