Page 1 of 1
Does FreeBrush frree Brush immediatelly?
Posted: Tue Mar 04, 2025 10:28 am
by Bugala
I am checking this to see if I can reduce the required amount of Memory needed.
I have a program that is essentially doing the following:
Code: Select all
for n = 1 to 10
Local ID
ID = LoadBrush(NIL, "file n")
SaveBrush(ID, file)
FreeBrush(ID)
next
I am wondering here that will this mean that there is only one BRUSH in memory at a time, or is there some sort of limitation like because Local ID (which means there are actually several Local ID:s existing from each For-Next loop) is pointing towards the ID number of BrushID userdata (from NIL), it wont get removed until the whole n=1 to 10 For-Next loop is done?
Re: Does FreeBrush frree Brush immediatelly?
Posted: Tue Mar 04, 2025 2:12 pm
by jPV
FreeBrush() frees the brush data from memory immediately, so in that loop you're having only one brush in memory at once. The local ID variable is being replaced on every round and memory reserved for the previous ID value is freed by the garbage collector at some point, but it's not that many bytes that it'd matter.
You can test it, for example, like this:
Code: Select all
mem = GetMemoryInfo(#FASTMEMORY)
for n = 1 to 10
Local ID
ID = LoadBrush(NIL, "image.png")
;SaveBrush(ID, file)
FreeBrush(ID)
DebugPrint(Int((mem - GetMemoryInfo(#FASTMEMORY))/1024/1024) .. " MB wasted")
next
WaitLeftMouse()
And when checking if your program uses more memory than it should, remember to run it with the Resource Monitor turned on (-resourcemonitor shell argument), and see if amount of any resource (like brushes) is increasing constantly while you use the program. Amount of all resources should stay at some constant (and sensible) max amount if you've done everything right.
Edit: more info about the resource monitor and memory usage here:
https://www.hollywood-mal.com/docs/html ... itor_.html
Re: Does FreeBrush frree Brush immediatelly?
Posted: Wed Mar 05, 2025 10:28 pm
by airsoftsoftwair
jPV wrote: ↑Tue Mar 04, 2025 2:12 pm
FreeBrush() frees the brush data from memory immediately, so in that loop you're having only one brush in memory at once.
There's one exception, though: If layers are on and the brush is still used by a layer, the brush won't be freed immediately but will just be made inaccessible while still keeping it in memory because it's needed by the layer. It will automatically be freed as soon as it's no longer referenced by a layer. The same is true for all other objects that can be added as layers.
Re: Does FreeBrush frree Brush immediatelly?
Posted: Thu Mar 06, 2025 7:13 am
by Bugala
@jpv, thanks from that "
GetMemoryInfo()" part, hadnt noticed command like that before.