Page 1 of 1
space() & garbage collection
Posted: Mon May 01, 2023 1:33 pm
by jassing
For garbage collection, do strings get cleaned up as soon as the procedure is over? I have some older routines that I used string variables and space() vs pointers & allocatememory()
when the result is less than the allocated space, is that 'extra' space cleaned up when it's out of scope or immediately?
procedure.s something()
v$ = space(1024)
newlen = someapi(@v$,1024) ; let's say v$ now holds "hello", when is that allocated space discarded?
procedurereturn v$
endproocedure
Re: space() & garbage collection
Posted: Mon May 01, 2023 6:43 pm
by Taz
Try it out, then you'll be sure.
Example to test:
viewtopic.php?t=70152
Re: space() & garbage collection
Posted: Mon May 01, 2023 7:43 pm
by AZJIO
jassing
You are copying the string to pass it to the call point. In the procedure, it will be destroyed immediately. For example, try passing not a string, but a pointer to a string, and you will see that there is nothing there.
Re: space() & garbage collection
Posted: Mon May 01, 2023 7:58 pm
by jassing
Taz wrote: Mon May 01, 2023 6:43 pm
Try it out, then you'll be sure.
Interesting read.. seemed there was an issue, but no more -- I'll poke around. In my case I am doing hundreds of calls per minute and want to be sure memory isn't an issue - if it is; I'll cover to allocating & freeing the memory myself... but it sounds like Fred addressed it.
@AZJIO - Спасибо
Cheers
-j
Re: space() & garbage collection
Posted: Mon May 01, 2023 8:35 pm
by AZJIO
If the string will not be increased then you can use code like this
i.e. the string is always within #MAX_PATH (260 characters)
i.e. there will be no memory re-allocation
Code: Select all
Procedure NewPath(*str, n)
Protected *p.string = @*str
GetModuleFileName_(0, *str, #MAX_PATH)
PathRemoveFileSpec_(*str)
*p\s + "\file.dll"
EndProcedure
Define Path$ = Space(#MAX_PATH)
NewPath(@Path$, #MAX_PATH)
Debug Path$
If the string is going to be incremented, i.e. as a regular text string, then you need to use the struct
Code: Select all
Procedure Test(*p.String)
*p\s + "\file.dll"
EndProcedure
Define Text.String
Text\s = "system"
Test(@Text)
Debug Text\s
Re: space() & garbage collection
Posted: Mon May 01, 2023 9:16 pm
by jassing
I think for my sanity, I'll go thru and use allocatememory()/freememory() instead of space()
Re: space() & garbage collection
Posted: Mon May 01, 2023 9:34 pm
by AZJIO
Using Space() allows you to work with a variable both as a string and as a pointer to data. If you are using AllocateMemory(), then you will have to use PeekS() and PokeS(), meaning you will be copying the memory to the variable multiple times. The task that is being solved above is to transfer data without copying string variables or to reduce these operations to a minimum. In fact, Space() does this by filling the memory with spaces 1 time. Now count how many times you will have PeekS() and PokeS() executed. From this you decide which option is more suitable.
By the way, AllocateMemory() also fills the data with zeros. And if this is not done, then there is a chance to read more than it is allocated. The Space() function also has a terminating Null at the end, which also makes it safe to read beyond the line.
With PeekS() you create a string variable. In this case, AllocateMemory() is doing you a disservice.
Re: space() & garbage collection
Posted: Mon May 01, 2023 10:16 pm
by jassing
@AZJIO, thank you for that - that was helpful in getting a better understanding. spasiba.
In my head this resulted in less remaining space being allocated, which is why I thought 'i'm controlling the memory, this is good"
*p = allocatememory(4096)
l =someapi( *P, 4096)
str$ = peeks(*p,l)
freememory(*p)
since the string would almost certainly be less than the allocated space, where as if i did
str$ = space(4096)
someapi(@str$,4096)
would result in 4096 being more permanently allocated, and if that was done many times a minute by various threads it would add up.
but then, if it's not even peeks()'d it seems it would be better to use allocatememory(),no?
Maybe I need to revisit everywhere I use allocatememory too... food for thought, for sure.
Re: space() & garbage collection
Posted: Tue May 02, 2023 2:31 pm
by SMaag
procedure.s something()
v$ = space(1024)
newlen = someapi(@v$,1024) ; let's say v$ now holds "hello", when is that allocated space discarded?
procedurereturn v$
endproocedure
v$ is created on Stack! What needs more time is to fill it with Spaces
when leaving the procedure, the stackpointer will be restored automatically.
There will be no memory problem!
But if someapi is a Windows-API, the API use 1BYTE-ASCII-Strings, what needs a conversion between 1ByteChar to 2ByteChar