简体   繁体   English

JSON对象内存分配与变量

[英]JSON object memory allocation vs variable

I have a question related to memory allocation of JSON object. 我有一个与JSON对象的内存分配有关的问题。 Will JSON take more memory than individual variable. JSON是否会比单个变量占用更多的内存。 For Ex: 例如:

var testVar1 = "testVar1"; //  8*2 bytes for char + integer pointer (8bytes)
var testVar2 = "testVar1";//  8*2 bytes for char + integer pointer (8bytes)
var testVar3 = "testVar1";//  8*2 bytes for char + integer pointer (8bytes)

vs
testVariables = {
"testVar1":"testVar1",
"testVar2":"testVar2",
"testVar3":"testVar3"
}   

When I open up memory profiler, testVariables retain memory seem to be more than sum of individual variable memory( retain). 当我打开内存分析器时,testVariables的保留内存似乎大于单个变量内存的总和(retain)。 Please let me know which is a better solution 请让我知道哪个是更好的解决方案

When you create 3 new variables, the Javascript engine will be create 3 new properties in the scope object and affect their values. 当您创建3个新变量时,JavaScript引擎将在范围对象中创建3个新属性并影响它们的值。

When you create a object witch contain 3 property. 创建对象时,女巫包含3个属性。 The engine create 1 new property to the scope object. 引擎为范围对象创建1个新属性。 It affect a new instance of Object to it that contain the 3 property references and their values. 它会影响到包含3个属性引用及其值的Object的新实例。

So in second case you have 1 reference and 1 object instance more than the first case. 因此,在第二种情况下,与第一种情况相比,您有1个引用和1个对象实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM