关于值类型和引用类型的内存结构的问题 [英] Questions about memory structure of value types and reference types

查看:55
本文介绍了关于值类型和引用类型的内存结构的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




JavaScript隐藏了它的内存结构。

我知道数字,布尔值,null和undefined是值类型

(值被定向保存在变量中)。


我想知道:


- JavaScript如何区分值类型与引用类型通过

查看变量内容?

- 当我声明以下内容时,内存中分配了多少字节?

var i ;

我可以用JavaScript编程而不理解它们。

但我很好奇。


谢谢。


Sam

解决方案

Sam Kong写道:


JavaScript隐藏其内存结构。



它没有正式将它暴露给语言工具,那将是

更正确的说:-) - 但是是的它隐藏了它。


我知道数字,布尔值,null和undefined是值类型

(值被定向保存在变量中)。



使用松散类型的语言几乎不可能。

当你宣布

var myVar;

引擎不知道myVar中将存储哪些数据;即使你实现了b $ b实例化并立即赋值:

var myVar = true;

然后它提高了代码的可读性,但它引擎没有太多帮助,因为下一个语句可以改变数据类型:

var myVar = true;

myVar = new Object; //或其他东西

(上面的权利当然不是一个好的方式,可以从松散的

打字中受益:但引擎必须准备好进行这样的翻转任何变量的任何

系统勾选。


这是* a *脚本引擎的通用考虑的结束:

进一步问题是* * *引擎或*引擎是如何处理挑战的。* blockquote class =post_quotes>
- JavaScript如何通过

看到变量内容来区分值类型和参考类型?

- 当我声明以下内容时,在内存中分配了多少字节?

var i;



我不准备评论Gecko引擎。在旧的Netscape(在ECMAScript规范中这方面描述的非常多的b $ b片段)和所有

版本的Internet Explorer中,它通过创建

种带MEMO字段的数据库记录。所以它是一个固定大小的结构,但是能够通过在记录之外使用它来保留专​​有的长数据,并且只在记录本身中引用。


所以说Internet Explorer声明

var i;

内部创建新的VARIANT对象(不是那个在

VBA,但是VARIANT是C ++中的VT)

这个VARIANT对象有一个值的数据成员(这是一个

匿名C ++联盟)和一个数据成员,表明存储在工会中的信息的类型。这就是为什么我用一个

数据库记录/ MEMO字段的比喻(肯定让所有人非常沮丧

专业C ++程序员:-)

这个VARIANT对象也有数据成员,指向IDispatchEx

接口扩展基本IDispatch接口:让你的对象

成为对其他东西的引用或成为其他一些

对象的成员。

它还有一个数据成员,其标志由垃圾收集器设置。在垃圾收集器到来的同时获得
a,研究对象,如果找不到范围内的引用,则会引发此标志(这是要删除的垃圾) 。


还有一些其他字段(比如指向IUnknown的指针),但上面的

成员对于核心功能来说是最重要的。 />

脚本引擎应用程序中的所有这些结构称为

scavenger,它不是与堆混合,因为值本身

不在这里但只是参考。


(神话中的本地ECMAScript对象与内部[[值]]

是一个关于ECMA's $
free-lancers 1999年在Netscape 4.x上看到它的方式的清道夫的寓言性描述。


>从这一点开始,你的变量已准备就绪,无论是



程序员的无限幻想:-)将使它成为一个y时刻:一个

原始值,一个对象引用,一个函数,一个对象成员 - 它

将不会在寒冷时捕获引擎。

结构的字节精确大小取决于当前的操作系统,并取决于操作系统的位基数(16位,32位,64位)。您可以查看C ++规范

以便VARIANT查找。


如果您真的对夜生活感兴趣JScript引擎的结果

如果您对C ++编程有一定的了解,请在MSDN上搜索

IDispatch和IDispatchEx方法。然后你可以编写一个工具来连接到引擎,看看清道夫出现,GC从

清道夫漫游到清道夫,引用更改,垃圾被移除。


PS上面的JScript描述并不直接适用于

JScript.NET

后者是一个相当不同的野兽。


P.P.S.从优化的角度来看,很有可能将最小的短生命原始价值创建为原始价值,希望程序员的合理行为;并且在类型更改的情况下

取消引用原语,并创建具有相同

名称的VARIANT。一个纯粹的推测,但也许这样的机制是在某些引擎中实现的。


你好VK,


VK写道:


Sam Kong写道:


JavaScript隐藏其记忆结构。



它没有正式公开它的语言工具,那将是

更正确的说:-) - 但是是的它隐藏了它。


我知道数字,布尔值,null和undefined是值类型

(值被定向保存在变量中)。



使用松散类型的语言几乎不可能。

当你声明

var myVar;

引擎不知道myVar中将存储哪些数据;即使你实现了b $ b实例化并立即赋值:

var myVar = true;

然后它提高了代码的可读性,但它引擎没有太多帮助,因为下一个语句可以改变数据类型:

var myVar = true;

myVar = new Object; //或其他东西

(上面的权利当然不是一个好的方式,可以从松散的

打字中受益:但引擎必须准备好进行这样的翻转任何变量的任何

系统勾选。


这是* a *脚本引擎的通用考虑的结束:

进一步问题是* * *引擎或*引擎是如何处理挑战的。* blockquote class =post_quotes>
- JavaScript如何通过

看到变量内容来区分值类型和参考类型?

- 当我声明以下内容时,在内存中分配了多少字节?

var i;



我还没准备好评论Gecko引擎。在旧的Netscape(在ECMAScript规范中这方面描述的非常多的b $ b片段)和所有

版本的Internet Explorer中,它通过创建

种带MEMO字段的数据库记录。所以它是一个固定大小的结构,但是能够通过在记录之外使用它来保留专​​有的长数据,并且只在记录本身中引用。


所以说Internet Explorer声明

var i;

内部创建新的VARIANT对象(不是那个在

VBA,但是VARIANT是C ++中的VT)

这个VARIANT对象有一个值的数据成员(这是一个

匿名C ++联盟)和一个数据成员,表明存储在工会中的信息的类型。这就是为什么我用一个

数据库记录/ MEMO字段的比喻(肯定让所有人非常沮丧

专业C ++程序员:-)

这个VARIANT对象也有数据成员,指向IDispatchEx

接口扩展基本IDispatch接口:让你的对象

成为对其他东西的引用或成为其他一些

对象的成员。

它还有一个数据成员,其标志由垃圾收集器设置。在垃圾收集器到来的同时获得
a,研究对象,如果找不到范围内的引用,则会引发此标志(这是要删除的垃圾) 。


还有一些其他字段(比如指向IUnknown的指针),但上面的

成员对于核心功能来说是最重要的。 />

脚本引擎应用程序中的所有这些结构称为

scavenger,它不是与堆混合,因为值本身

不在这里但只是参考。


(神话中的本地ECMAScript对象与内部[[值]]

是一个关于ECMA's $
free-lancers 1999年在Netscape 4.x上看到它的方式的清道夫的寓言性描述。


从这一点开始,你的变量已准备就绪,无论是



程序员的无限幻想:-)将使它成为任何一个momen t:一个

原始值,一个对象引用,一个函数,一个对象成员 - 它b / b
不会在寒冷时捕获引擎。


结构的字节精确大小取决于当前操作系统和操作系统的位基数(16位,32位,64位)。您可以查看C ++规范

以便VARIANT查找。


如果您真的对夜生活感兴趣JScript引擎的结果

如果您对C ++编程有一定的了解,请在MSDN上搜索

IDispatch和IDispatchEx方法。然后你可以编写一个工具来连接到引擎,看看清道夫出现,GC从

清道夫漫游到清道夫,引用更改,垃圾被移除。


PS上面的JScript描述并不直接适用于

JScript.NET

后者是一个相当不同的野兽。


P.P.S.从优化的角度来看,很有可能将最小的短生命原始价值创建为原始价值,希望程序员的合理行为;并且在类型更改的情况下

取消引用原语,并创建具有相同

名称的VARIANT。我的一个纯粹的推测,但可能是这样的机制是在某些引擎中实现的



你的解释是惊人的。

我几乎放弃了这个问题。

你救了我。


非常感谢。

我会研究你推荐的材料。


Sam


文章< 11 ********************* @ i3g2000cwc.googlegroups .c om>,Sam

Kong< sa ******** @ gmail.comwrites


>

JavaScript隐藏了它的内存结构。
我知道数字,布尔值,null和undefined是值类型
(值被定向保存在变量中)。
<我想知道:

- JavaScript如何通过查看变量内容来区分值类型和引用类型?

- 分配了多少字节当我声明以下内容时?
var i;

我可以用JavaScript编程而不理解它们。
但我很好奇。



javascript引擎可以任何方式编程。


你的猜测可能和我们的一样好。 />

John

-

John Harris


Hi,

JavaScript hides its memory structure.
I know that numbers, booleans, null and undefined are value types
(value is directed saved in a variable).

I want to know:

- How JavaScript distinguishes value types from reference types by
seeing the variable content?

- How many bytes are allocated in memory when I declare the following?
var i;
I can program in JavaScript without understanding them.
But I''m curious.

Thanks.

Sam

解决方案

Sam Kong wrote:

JavaScript hides its memory structure.

It doesn''t officially expose it to the language tools, that would be
more correct to say :-) - but yes, it hides it.

I know that numbers, booleans, null and undefined are value types
(value is directed saved in a variable).

That is hardly possible in a loosely typed language.
When you declare
var myVar;
the engine has no idea what data will be stored in myVar; even if you
instantiate and assign a value to it right away:
var myVar = true;
then it improves the code readability but it doesn''t help too much to
the engine, because right the next statement can change the data type:
var myVar = true;
myVar = new Object; // or something else
(The right above is not a good way of course to benefit from the loose
typing: yet the engine has to be ready for such flip around at any
system tick for any variable).

That is the end of generic considerations for *a* script engine:
further it is the question of how *this* engine or *that* engine is
dealing with the challenge.

- How JavaScript distinguishes value types from reference types by
seeing the variable content?
- How many bytes are allocated in memory when I declare the following?
var i;

I''m not ready to comment on the Gecko engine. In the old Netscape (very
fragmentary described in this aspect in ECMAScript specs) and in all
versions of Internet Explorer it is solved by creating a structure of a
kind of a database record with a MEMO field. So it''s a fixed size
structure yet able to keep proprietary long data in it by having it
outside of the record and only referenced in the record itself.

So say Internet Explorer on statement
var i;
internally creates new VARIANT object (not that Variant which is in
VBA, but that VARIANT which is VT in C++)
This VARIANT object has a data member for the value (this is an
anonymous C++ union) and a data member indicating the type of
information stored in the union. That is why I used the analogy with a
database record / MEMO field (and surely made very upset all
professional C++ programmers :-)
This VARIANT object also has data member with pointer to IDispatchEx
interface extending the base IDispatch interface: that lets your object
to be a reference to something else or to be a member of some other
object.
It also has a data member with flag set by Garbage Collector. Once for
a while Garbage Collector comes, studies the object and if no in-scope
references found, it raises this flag ("that is a garbage to remove").

There is a few other fields (like a pointer to IUnknown), but the above
members are the most essential for the core functionality.

All this structure in application to the script engine is called
scavenger, and it is not to mix with a heap, because the value itself
is not here but only referenced to.

(The mythological "native ECMAScript object with internal [[values]]"
is an allegoric decription of the scavenger in the way of how ECMA''s
free-lancers saw it in 1999 on Netscape 4.x).

>From this point forward your variable is ready to be whatever

programmer''s unlimited fantasy :-) will make it be at any moment: a
primitive value, an object reference, a function, an object member - it
will not catch the engine on cold.

The byte-exact size of the structure depends on the current OS and on
the bit-base of the OS (16bit, 32bit, 64bit). You may look at C++ specs
for VARIANT to find out.

If you are really interested in the "night life" of the JScript engine
and if you have some knowledge of C++ programming, search MSDN for
IDispatch and IDispatchEx methods. Then you can write an util to
connect to the engine to see scavengers appearing, GC roaming from
scavenger to scavenger, references changing, the garbage removed.

P.S. JScript description above is not directly applicable to
JScript.NET
The latter is a rather different beast.

P.P.S. From the optimization point of view it is tempting to create at
least short-living primitive values as just primitive values in hope to
the programmer''s reasonnable behavior; and in case of type change
dereference the primitive and and to create a VARIANT with the same
name. A pure speculation from my side but maybe such mechanics is
implemented in some engines.


Hi VK,

VK wrote:

Sam Kong wrote:

JavaScript hides its memory structure.


It doesn''t officially expose it to the language tools, that would be
more correct to say :-) - but yes, it hides it.

I know that numbers, booleans, null and undefined are value types
(value is directed saved in a variable).


That is hardly possible in a loosely typed language.
When you declare
var myVar;
the engine has no idea what data will be stored in myVar; even if you
instantiate and assign a value to it right away:
var myVar = true;
then it improves the code readability but it doesn''t help too much to
the engine, because right the next statement can change the data type:
var myVar = true;
myVar = new Object; // or something else
(The right above is not a good way of course to benefit from the loose
typing: yet the engine has to be ready for such flip around at any
system tick for any variable).

That is the end of generic considerations for *a* script engine:
further it is the question of how *this* engine or *that* engine is
dealing with the challenge.

- How JavaScript distinguishes value types from reference types by
seeing the variable content?
- How many bytes are allocated in memory when I declare the following?
var i;


I''m not ready to comment on the Gecko engine. In the old Netscape (very
fragmentary described in this aspect in ECMAScript specs) and in all
versions of Internet Explorer it is solved by creating a structure of a
kind of a database record with a MEMO field. So it''s a fixed size
structure yet able to keep proprietary long data in it by having it
outside of the record and only referenced in the record itself.

So say Internet Explorer on statement
var i;
internally creates new VARIANT object (not that Variant which is in
VBA, but that VARIANT which is VT in C++)
This VARIANT object has a data member for the value (this is an
anonymous C++ union) and a data member indicating the type of
information stored in the union. That is why I used the analogy with a
database record / MEMO field (and surely made very upset all
professional C++ programmers :-)
This VARIANT object also has data member with pointer to IDispatchEx
interface extending the base IDispatch interface: that lets your object
to be a reference to something else or to be a member of some other
object.
It also has a data member with flag set by Garbage Collector. Once for
a while Garbage Collector comes, studies the object and if no in-scope
references found, it raises this flag ("that is a garbage to remove").

There is a few other fields (like a pointer to IUnknown), but the above
members are the most essential for the core functionality.

All this structure in application to the script engine is called
scavenger, and it is not to mix with a heap, because the value itself
is not here but only referenced to.

(The mythological "native ECMAScript object with internal [[values]]"
is an allegoric decription of the scavenger in the way of how ECMA''s
free-lancers saw it in 1999 on Netscape 4.x).

From this point forward your variable is ready to be whatever

programmer''s unlimited fantasy :-) will make it be at any moment: a
primitive value, an object reference, a function, an object member - it
will not catch the engine on cold.

The byte-exact size of the structure depends on the current OS and on
the bit-base of the OS (16bit, 32bit, 64bit). You may look at C++ specs
for VARIANT to find out.

If you are really interested in the "night life" of the JScript engine
and if you have some knowledge of C++ programming, search MSDN for
IDispatch and IDispatchEx methods. Then you can write an util to
connect to the engine to see scavengers appearing, GC roaming from
scavenger to scavenger, references changing, the garbage removed.

P.S. JScript description above is not directly applicable to
JScript.NET
The latter is a rather different beast.

P.P.S. From the optimization point of view it is tempting to create at
least short-living primitive values as just primitive values in hope to
the programmer''s reasonnable behavior; and in case of type change
dereference the primitive and and to create a VARIANT with the same
name. A pure speculation from my side but maybe such mechanics is
implemented in some engines.

You explanation is amazing.
I almost gave up on this question.
And you rescued me.

Thank you very much.
I''ll study some more on the materials you recommended.

Sam


In article <11*********************@i3g2000cwc.googlegroups.c om>, Sam
Kong <sa********@gmail.comwrites

>Hi,

JavaScript hides its memory structure.
I know that numbers, booleans, null and undefined are value types
(value is directed saved in a variable).

I want to know:

- How JavaScript distinguishes value types from reference types by
seeing the variable content?

- How many bytes are allocated in memory when I declare the following?
var i;
I can program in JavaScript without understanding them.
But I''m curious.

A javascript engine can be programmed any way that works.

Your guesses are probably as good as ours.

John
--
John Harris


这篇关于关于值类型和引用类型的内存结构的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆