PureBasic Forum
https://www.purebasic.fr/english/

[Done] Function Space does not free memory
https://www.purebasic.fr/english/viewtopic.php?f=4&t=71568
Page 1 of 2

Author:  User_Russian [ Mon Oct 15, 2018 9:55 pm ]
Post subject:  [Done] Function Space does not free memory

Function Space does not free memory. In the task manager, the process uses almost 800 MB of RAM.
Code:
Space(200000000)
Space(400000000)
Space(2)
CallDebugger

Author:  Sicro [ Mon Oct 15, 2018 11:05 pm ]
Post subject:  Re: Function Space does not free memory

Yes, it seems that the freeing of string variables has still not been implemented correctly.
Look here: Free Strings / Memory

Author:  Josh [ Tue Oct 16, 2018 12:12 am ]
Post subject:  Re: Function Space does not free memory

The other question is, where are these strings stored and why should they ever overwrite each other? This code should never be executed by Pb.

Author:  User_Russian [ Tue Oct 16, 2018 11:30 am ]
Post subject:  Re: Function Space does not free memory

Writing to a string variable does not solve the problem.
Code:
s.s=Space(200000000)
s=""
s=Space(40000000)
s=""
s=Space(2)
s=""
CallDebugger

Author:  Fred [ Sun Dec 02, 2018 10:18 am ]
Post subject:  Re: Function Space does not free memory

Fixed.

Author:  Sicro [ Sat Jan 12, 2019 8:09 pm ]
Post subject:  Re: [Done] Function Space does not free memory

Why is the Space() function allowed to free memory, but not:
Code:
s.s = #Null$
; or
s.s = ""
?

Code:
Procedure$ FreeString()
  ProcedureReturn ""
EndProcedure

a$ = Space(1e9) ; Create a string with 1.000.000.000 whitespace characters
CompilerSelect 5
     CompilerCase 0 : a$ = #Null$       ; Thereafter, 1,9 GiB RAM are still in use
     CompilerCase 1 : a$ = ""           ; Thereafter, 1,9 GiB RAM are still in use
     CompilerCase 2 : a$ = Space(0)     ; Thereafter, 1,3 MiB RAM are still in use
     CompilerCase 3 : a$ = Hex(0)       ; Thereafter, 1,3 MiB RAM are still in use
     CompilerCase 4 : a$ = Str(0)       ; Thereafter, 1,3 MiB RAM are still in use
     CompilerCase 5 : a$ = FreeString() ; Thereafter, 1,3 MiB RAM are still in use
CompilerEndSelect
Repeat
   Delay(1)
ForEver

Author:  Fred [ Tue Jan 15, 2019 12:57 pm ]
Post subject:  Re: [Done] Function Space does not free memory

literal string affectation doesn't create a new buffer, that's why.

Author:  User_Russian [ Tue Jan 15, 2019 1:50 pm ]
Post subject:  Re: [Done] Function Space does not free memory

Fred, this line should free the existing buffer.
Code:
s.s = ""
But instead zeroed initial bytes.
Code:
s.s = Space(200000000)
Debug @s
s = ""
Debug @s
Debug ShowMemoryViewer(@s, 10)
Code:
s.s = "12345678"
Debug @s
s = ""
Debug @s
Debug ShowMemoryViewer(@s, 10)
This is a bug!

Author:  Fred [ Tue Jan 15, 2019 2:13 pm ]
Post subject:  Re: [Done] Function Space does not free memory

No, it's the intended behaviour. The buffer is freed only on the next real allocation (in real world code it won't be an issue).

Author:  User_Russian [ Tue Jan 15, 2019 2:34 pm ]
Post subject:  Re: [Done] Function Space does not free memory

Fred wrote:
it's the intended behaviour.
Where in help it is written about it?

The help is written.
Quote:
#Null$ special string constant to affect a real null value to a string (to free it completely). It can be used as well for API function which sometimes accept NULL instead of a string address.
But it does not work.
Code:
s.s = "12345678"
s = #Null$
ShowMemoryViewer(@s, 10)
Code:
s.s = "12345678"
s = ""
s = #Null$
s = #Empty$
ShowMemoryViewer(@s, 10)
Then why do we need a constant #Null$?

Why did you decide not to free the string buffer? What is the advantage of this?

Author:  Fred [ Tue Jan 15, 2019 2:47 pm ]
Post subject:  Re: [Done] Function Space does not free memory

#Null$ correctly free the previous string buffer, it just don't reduce the shared (working space buffer) string buffer.

Author:  Sicro [ Tue Jan 15, 2019 6:19 pm ]
Post subject:  Re: [Done] Function Space does not free memory

Fred wrote:
literal string affectation doesn't create a new buffer, that's why.
Thanks, now it makes sense.

Fred wrote:
#Null$ correctly free the previous string buffer, it just don't reduce the shared (working space buffer) string buffer.
Ok, this will free a PB internal buffer, but what is the advantage for us?
I have now created a thread for this: Add description for #Null$

Author:  User_Russian [ Tue Jan 15, 2019 6:21 pm ]
Post subject:  Re: [Done] Function Space does not free memory

Constant #Null$ must to free string and to zero pointer, otherwise constant #Null$ does not make sense.
Code:
s.s
Debug @s
s = "12345678"
Debug @s
s = #Null$
Debug @s ; <- Must be 0.

Author:  fryquez [ Tue Jan 15, 2019 6:32 pm ]
Post subject:  Re: [Done] Function Space does not free memory

User_Russian wrote:
Constant #Null$ must to free string and to zero pointer, otherwise constant #Null$ does not make sense.
It does, in previous versions. But it's broken in 5.70.

Author:  Fred [ Wed Jan 16, 2019 10:31 am ]
Post subject:  Re: [Done] Function Space does not free memory

Ok, so it's a regression. I will look into it.

Page 1 of 2 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/