主题:  Disposing of objects in RAM

jerrowolf

职务:普通成员
等级:1
金币:0.0
发贴:83
注册:2001/1/4 20:28:12
#12001/5/21 8:33:10

Disposing of objects in RAM

This article is an excerpt from the DreamLight Director Talisman, www.dreamlight.com/insights/07/, which contains information about managing memory,

sound, the actor list, LingOOP, behaviors and more.

How can you free memory that's occupied by a large object? Simply break all references to the object and it will be deleted. Breaking all references to an object

means resetting all variables that are pointing to the object (including any references in the actorlist and so on), to VOID. That will do the trick.

One way to accomplish this is to set the variable to value(void) as follows:

set gMyGroovyObject = value(void) You could also use:

set gMyGroovyObject = 0 Neither method requires any additional code or external handlers, and Director's loose typing easily allows putting the integer 0 into

what used to hold an object pointer. Both methods release the object from RAM.

The main benefit of using VOID is that it may be a touch more obvious that you are clearing out a pointer rather than creating an integer value.

Both approaches release the memory used to hold the object. Both approaches also retain an unused variable. Whether the variable is filled with the integer 0 or

VOID makes no difference. Both versions still hold onto the minimum amount of memory to store a simple value.

Variables in Lingo require a certain amount of RAM based upon their contents. As far as RAM is concerned, there are two types of variables: simple and complex.

Simple variables are used to hold integers, symbols, and pointers. Each of these simple types is a fixed size, which is currently 8 bytes. The other type of variable

uses a pointer to refer to a second data area of memory, which holds the actual contents. These complex variables hold strings, lists, and objects. When all

pointers and references to the additional data of the complex variable are broken, the RAM used by the additional data portion is reclaimed.

For example, if we set a variable called myobjectptr equal to a new object, we end up with the variable myobjectptr containing a pointer to the object's additional

data:

myobjectptr -> myobjectdata myobjectptr would take up 8 bytes; myobjectdata would take up additional RAM to hold the object data.

If myobjectptr is the only variable pointing to this particular object, we can simply set myobjectptr =0 or VOID and Lingo will reclaim all memory used by the

myobjectdata area. The complex variable myobjectptr still takes up the same size as a simple type, but the extended data portion is reclaimed. myobjectptr will

now take up the same amount of RAM as a simple variable type until it goes out of scope and is thereby destroyed and fully reclaimed.

The scope of different Lingo variables are:

Local scope - Variables created within a handler are created with local scope. They will exist until the handler ends, at which time they are destroyed. If myobjectptr

was a local variable which has been voided out, it will retain the minimum amount of RAM of a simple type until the handler ends.
Object scope - Property variables within an object or a list exist in the additional data area of the object pointed to by the reference variable. They will exist until

the container object itself is no longer referenced by any other variables. If myobjectptr was a property variable of a container object and myobjectptr has been

voided out, it will retain the minimum amount of RAM of a simple type until all references to the object are broken and the containing object itself is destroyed.
Global scope - Variables explicitly declared as global have global scope. They exist forever and are not destroyed until Director or the projector quits. You may use

clearglobals to set all the globals to void. This would release any extended data the globes have been referring to, provided there are no other references to such

data. Even when voided, the globals themselves will continue to hold onto the minimum amount of RAM used by a simple type. If myobjectptr was a global variable

that has been voided out, it will retain the minimum amount of RAM of a simple type until Director or the projector quits.


In summary, simple variable types use the minimum amount of RAM, and you can't clear them out until they go out of scope. Complex variable types use the

same amount of RAM as simple types to hold a pointer, as well as an additional amount of RAM to hold the complex data itself. You can free up all the additional

RAM used by complex variable types by simply zeroing or voiding out the variable pointer. As long as there are no other variables that point to the complex data

itself, it will be released.

中文:
内存中对象的配置。(摘自Macromedia.com)
--译:jerrowolf

本文摘自Dreamlight Director Talisman。 www.dreamlight.com/insights/07/, 那里包含很多内存管理,声音,演员表,面向对象的lingo(lingOOP,自己理解的^_^)的信息。还有behavior和其他的信息。

如何来释放那些被大对象所占用的内存呢?最简单的方法是:切断所有与对象的联系,它就被清除了。切断联系的意思是将所有指向该对象的变量重置为void(包括在演员表中的任何联系),通过如下语句可以实现它:set gMyGroovyObject = value(void) ,或者你也可以用set gMyGroovyObject = 0来实现。两种方法都不需要额外的代码或者外部句柄,Director允许通过给对象的指针赋予0值,来将其从内存中清除掉。这两种方法都有效。

而用void的好处就是,这样看上去更像是将其从内存中清楚,而不是给它赋一个0值。

这两种方法都可以释放内存中的对象,也都能保留未被使用的变量。这个变量是0还是void都没差别,他们都会占用最少的内存来存储一个值。

lingo中变量占用内存的大小取决于这个变量存储内容的需要。从占用内存的角度来讲,变量被分为两种:简单变量和复杂变量,简单变量存储整数,符号,和指针。他们的大小是固定的,都是8字节。而复杂变量则用一个指针来指向内存中的另外一部分内容,那里存放着实际的内容。
这样的复杂变量存储字符串,表,还有对象。当切断与”内存中的另外一部分内容“相关联的所有指针和联系,这部分被占用的内存就会被释放。例如,如果我们定义一个叫myobjectptr的变量来存储一个新的对象,他就存储了一个指向新对象的指针:myobjectptr占用的空间=指针本身占用的8字节+新对象所占用的内存空间。

如果myobjectptr只是一个指向这个特定对象的一个指针,我们可以简单地用set myobjictptr=0 或者 VOIDl来释放这个对象所占用的内存空间,但是myobjectptr却还占用着一个简单变量所应该占用的空间。但是其余部分已经被释放了,在这个变量离开作用域而被彻底销毁时,它才真正地全部释放它所占用的内存。

不同的lingo变量作用域有:

1、本地作用域:-一个句柄中创建的变量拥有本地作用域。当句柄结束时,它也就被销毁了,它的本地作用域就结束了。如果myobjectptr是一个本地作用域的变量,那么当它所在句柄未结束之前,它始终占用一个最小的内存空间。

2、对象作用域:- 这是一种属性变量,他存在于一个对象中,或者对象中被其他关联变量所指向的表当中。属性变量将一直存在,直到包含它的那个对象再没有任何变量与之关联。如果 myobjectptr是一个对象中的属性变量,并且myobjectptr已经被清空,那么,他将一直占用一个
最小的内存空间,直到包含他的那个对象销毁,也就是与对象相关联的其他变量全部切断。

3、全局作用域:很明显,全局变量拥有全局作用域。他们将在电影或者工程退出之前一直存在。你可以使用clearglobals命令来清空所有全局变量,但这只能使全局变量的指向为空,除非他根本就没指向什么。尽管已经被清空,但是他还是将占用简单变量所需要占用的内存空间。如果
myobjectptr是一个全局变量,并且被清空了。那么他将始终占用一部分空间,直到电影或者工程退出。

总之,简单变量类型占用的内存最少。在没有脱离他们的作用域之前,你无法真正的清楚他们。而复杂变量也占用同样的内存空间,不过,他还要占用额外的空间来容纳复杂数据自己。对于那些复杂变量或者没有任何其他变量指向的复杂数据本身,你可以通过简单地将复杂变量清0或
者使之为空,来释放那些额外占用的内存。



rainy_5d

职务:普通成员
等级:2
金币:1.0
发贴:225
注册:2000/10/23 19:44:16
#22001/5/21 21:58:08
谢谢jerrowolf。