Simple Way to get Memory Available

Advanced GP Tips and Techniques

Moderator: MSandro

Post Reply
User avatar
Calloway
Posts: 100
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: 11
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 21770 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: 11
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 21770 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)
}


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

Re: Simple Way to get Memory Available

Post by Calloway » Feb 28th, '20, 14:32

I like your method a lot more lol, also you can right click it and press text code to copy and paste that into the forums
(if people want to import, they open a workspace and paste it then highlight all the text and press ctrl+b)

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

Re: Simple Way to get Memory Available

Post by Spiralo-Idioïde » Mar 2nd, '20, 17:47

Calloway wrote:
Feb 28th, '20, 14:32
I like your method a lot more lol
Thanks you.
Calloway wrote:
Feb 28th, '20, 14:32
also you can right click it and press text code to copy and paste that into the forums
(if people want to import, they open a workspace and paste it then highlight all the text and press ctrl+b)
Waw, we can use Ctrl+b ? That's really, really, awesome. Thanks for the advice I'll do that starting from now.

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

Re: Simple Way to get Memory Available

Post by Calloway » Mar 5th, '20, 23:08

Just out of curiosity what were you doing before to get the primitives lol

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

Re: Simple Way to get Memory Available

Post by Spiralo-Idioïde » Mar 6th, '20, 07:10

I don't remember perfectly but I had already used this exact same block to print memory usage next to the FPS. When seeing your code I wanted to know wich one was faster than the other

Post Reply