problem with release command
Archives Forums/BlitzMax Bug Reports/problem with release command
| ||
when I run the release example: Rem Release removes the internal reference caused by creating an integer handle to a type. End Rem Type MyType Field bigmap[1024*1024] End Type GCCollect Print GCMemAlloced() a=New MyType GCCollect Print GCMemAlloced() Release a GCCollect Print GCMemAlloced() I get the next output: Building .release Compiling:.release.bmx flat assembler version 1.69.14 (1048575 kilobytes memory) 3 passes, 4366 bytes. Linking:.release.debug.exe Executing:.release.debug.exe 15204 4209544 4209544 Process complete from this output it seems that 'release a' doesn't remove the reference of a to the allocated memory for MyType and therefore the last GCCollect doesn't release the memory that was allocated for MyType memory and which was supposed to be free by release a command. my OS is windows 8.1. |
| ||
There's nothing wrong with it, it's just a really badly-written example. The problem is that GCCollect doesn't free up the allocated memory in time for the next Print command. Try the code below - you'll see that after a couple of seconds, memory usage returns to normal. Rem Release removes the internal reference caused by creating an integer handle to a type. End Rem Type MyType Field bigmap[1024*1024] End Type Graphics 640,480 'need this so we can hit Esc to quit the while/wend loop - makes no difference to the example GCCollect Print GCMemAlloced() a=New MyType GCCollect Print GCMemAlloced() Release a GCCollect While Not KeyHit(key_escape) Print GCMemAlloced() Delay 1 Wend |
| ||
I am very confused because in the next example the garbage collector free the memory immediately and doesn't need some seconds or even less to free the memory (it works like it should): Type MyType Field bigmap[1024*1024] End Type GCCollect Print GCMemAlloced() a:mytype=New MyType GCCollect Print GCMemAlloced() a=Null GCCollect Print GCMemAlloced() and now the output is correct: Building .release Compiling:.release.bmx flat assembler version 1.69.14 (1048575 kilobytes memory) 3 passes, 4309 bytes. Linking:.release.debug.exe Executing:.release.debug.exe 15204 4209544 15204 Process complete it seems that when we put the address of the allocated memory into a variable of the same type (not integer) and free the reference to it by putting a null into it later it works fine but work less well when we use integer to hold the address and later using release on the integer variable. sorry but I steal think there is a problem here because I would expect the 2 methods to work the same. |
| ||
They do both work the same on my PC - I tested both. Memory isn't released for at least a couple of seconds. |
| ||
in my computer on the first method it took the gc some seconds to release the memory but on the second method the gc release the memory immediately . why the difference ? |
| ||
Your second bit of code is not the same. In your first code: a=New MyType In your second code: a:MyType=New MyType The first does not work immediately with GC, the second one does. |
| ||
so can I assume that the second method of a:MyType=New MyType and a=null is better then the method of a=New Type and release a ? |
| ||
Using integer variables for objects is allowed in order to ease the transition for programmers who already know an earlier Blitz language. But it is a bad practice and you should not do it. You should always make variables the appropriate type and forget that you ever saw the Release command. |
| ||
thank you Floyd. now it is clear. |
| ||
Just to add, it's good practice to use "Strict" (or "SuperStrict") at the top of your code. These two commands force you, to varying levels, to declare variables before you use them. Pretty sure none of this code would even compile in Strict mode. |
| ||
Use SuperStrict at the top of your code. It *forces* you to define the types of everything - which is not a bad thing. |
| ||
Please don't use 'Release'! As Floyd says, it was to make life easier for people coming from Blitz3D etc, but it is slow and makes for ugly code! |