将文字/对象/类传递给具有数千个调用的函数的内存含义 [英] Memory implications of passing literals/objects/classes to functions with thousands of calls
问题描述
我正在编写的框架涉及一个Figure
类,该类在Renderer
类上调用函数SetTranslate( x, y )
.反过来,Renderer
类必须存储传递给SetTranslate
的参数(x, y)
.
A framework I'm writing involves a Figure
class calling a function SetTranslate( x, y )
on a Renderer
class. In turn, the Renderer
class has to store the arguments (x, y)
passed to SetTranslate
.
可能有成千上万个数字,因此,单个用户操作可能会触发成千上万个此类调用.
相对于此类调用的文字,我有点担心使用对象或类对内存的影响(以及垃圾回收的影响).
I'm a bit concerned about the memory implications (and garbage collection ones) of using objects or classes compared to literals for such calls.
详细信息如下.
请原谅我在下面的代码中使用类语法,但这与实际问题没有任何区别.可以轻松地用标准JS函数&替换Class
语法.原型语法.
Do forgive me for using class-like syntax in the code below, but it makes no difference to the actual question. One can easily substitute the Class
syntax with standard JS functions & prototype syntax.
在下面的所有选项中,图形类使用属性rect
构造,该属性是类Rect
的新实例:
In all the options below, the figure class is constructed with a property rect
that is a new instance of a class Rect
:
Figure = new Class({
// Constructor
initialize: function() {
this.rect = new Rect( 0, 0, 10, 10 );
}
});
选项1-使用原始值参数
Figure类:
Option 1 - using primitive value parameters
The Figure class:
Figure = new Class({
render: function( aRenderer ) {
aRenderer.setTranslate( this.rect.x, this.rect.y );
}
});
Renderer类:
The Renderer class:
Renderer = new Class({
// Constructor
initialize: function() {
this.translation = {
x: 0,
y: 0
};
},
setTranslate: function( x, y ) {
this.translation.x = x;
this.translation.y = y;
}
});
关注点
如果我没记错的话,这里不涉及内存分配,因此没有真正的问题-这很简单.
Concerns
If I'm not mistaken, there's no memory allocation involved here, so no real concerns - it's straightforward.
Figure类:
Figure = new Class({
render: function( aRenderer ) {
aRenderer.setTranslate( { x: this.rect.x, y: this.rect.y } );
}
});
Renderer类:
The Renderer class:
Renderer = new Class({
// Constructor
initialize: function() {
this.translation = {
x: 0,
y: 0
};
},
setTranslate: function( aTranslation ) {
this.translation = aTranslation;
}
});
关注点
我的问题是是否使用
Concerns
My question is if by using
aRenderer.setTranslate( { x: this.rect.x, y: this.rect.y } );
创建了一个新对象.另外,通过执行
a new object is created. Also, by executing
this.translation = aTranslation;
先前的对象现在不会进入垃圾回收吗?
wouldn't the previous object now go to garbage collection?
Figure类:
Figure = new Class({
render: function( aRenderer ) {
// rect.getTopLeft() returns new Point( x, y );
aRenderer.setTranslate( this.rect.getTopLeft() );
}
});
Renderer类:
The Renderer class:
Renderer = new Class({
// Constructor
initialize: function() {
this.translation = new Point( 0, 0 );
},
setTranslate: function( aTranslation ) {
this.translation = aTranslation;
}
});
关注点
getTopLeft()
返回new Point( x, y )
的事实明确表明内存分配将是其中的一部分:
Concerns
The fact that getTopLeft()
returns a new Point( x, y )
makes it explicit that memory allocation will be part of this:
// rect.getTopLeft() returns new Point( x, y );
aRenderer.setTranslate( this.rect.getTopLeft() );
很明显,这一行:
this.translation = aTranslation;
先前的translation
对象(类)将进入垃圾回收.
the previous translation
object (class) will go into garbage collection.
这与选项2有什么不同吗?
Is this any different from option 2?
并且由于这是我的理想解决方案(Point类中的方法可能对setTranslate
中的其他事物有用),由于每个用户操作可能会进行数千次此类调用(因此选项1和2,就是)?
And since this is the ideal solution for me (the Point class has methods that could be useful for other things within setTranslate
) would it be a problem given that there may be thousands of such calls per user action (compared to option 1 and 2, that is)?
推荐答案
我认为您会发现对象池是控制此类内存行为的非常有效的方法.预先分配并发需要的对象数量,然后重新使用它们以避免垃圾回收问题.
I think you will find that object pools are a very effective way of controlling the memory behaviour of something like this. Allocate up front the number of objects that you need concurrently and then reuse them to avoid garbage collection issues.
要回答您的问题,
aRenderer.setTranslate( { x: this.rect.x, y: this.rect.y } );
将创建一个新对象,是的,在您的示例中,先前的翻译对象将有资格使用GC(假设没有其他引用).
will create a new object and yes, in your example the previous translate object would be eligible for GC (assuming there are no other references to it).
这篇关于将文字/对象/类传递给具有数千个调用的函数的内存含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!