Simple Way to get Memory Available

Advanced GP Tips and Techniques

Moderator: MSandro

Post Reply
User avatar
Calloway
Posts: 94
Joined: Apr 30th, '18, 00:28
Location: Eastern United States

Simple Way to get Memory Available

Post by Calloway » Feb 9th, '20, 23:14

Here's a quick little script to help see how much memory is left in GP.

Code: Select all

(toNumber (join (at (at (words (mem)) 3) 2) (at (at (words (mem)) 3) 3) '.' (at (at (words (mem)) 3) 5)))
scriptImage.png
You could use this to help guide garbage collection in your project by calling it whenever the percentage reaches a certain threshold. And since it mainly uses primitives, it won't tank your project by using it.

Spiralo-Idioïde
Posts: 6
Joined: Jun 16th, '19, 23:53

Re: Simple Way to get Memory Available

Post by Spiralo-Idioïde » Feb 11th, '20, 17:09

I'm not sure it could add anything to the built-in garbage manager wich seems to be very efficient.
Just look at this GIF (I'm using your code to get memory usage and the 'grapher' part).
garbageCollector.gif
garbageCollector.gif (2.1 MiB) Viewed 18 times
But if you think it can be usefull in some way I'd be happy to know.
Last edited by Spiralo-Idioïde on Feb 12th, '20, 07:02, edited 2 times in total.

Spiralo-Idioïde
Posts: 6
Joined: Jun 16th, '19, 23:53

Re: Simple Way to get Memory Available

Post by Spiralo-Idioïde » Feb 11th, '20, 17:32

Oh, I just noticed we can speed-up the process a lot.
The (memoryUsage) block is not a primitive, the primitive is (memStat). Using this one instead (with a division and multiplication) should make things go faster.

For example, GP manages to set a variable with the reporter you gave approximately 14 000 times per second.
By using the primitive it's 6 400 000 times per second, more than 400 times faster.

Here is a screen of the code to use the primitive.
scriptsImage.png
scriptsImage.png (3.4 KiB) Viewed 18 times

And the code I used to compare boths.

Code: Select all

GP Scripts
script 'MyClass' 423 194 {
local 'tmp' (msecsSinceStart)
setShared 'monit' 0
repeat 100000000 {
  var = (100 * ((at (memStats) 1) / 200000000))
}
setShared 'monit' (((msecsSinceStart) - tmp) / 100000000)
}

script 'MyClass' 169 349 {
local 'tmp' (msecsSinceStart)
setShared 'monit' 0
repeat 100000 {
  var = (toNumber (join (at (at (words (mem)) 3) 2) (at (at (words (mem)) 3) 3) '.' (at (at (words (mem)) 3) 5)))
}
setShared 'monit' (((msecsSinceStart) - tmp) / 100000)
}


Post Reply