Dart中const值的内存管理 [英] Memory management of const values in Dart

查看:194
本文介绍了Dart中const值的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用dart语言将const值存储在何处以及多长时间?例如 const Duration()(链接到下面的img)。我知道const和全局值存储在堆栈中,直到应用程序关闭为止。

Where and how long const values in classes being stored in dart language? For example const Duration()(link to the img below). I know that const and global values are stored in a stack until the app is close. Thank you for your answer in advance.

推荐答案

要使我的评论带有一个答案,当您谈论存储在堆栈或堆中的值时,您是在谈论变量。变量以多种不同的形式存在,例如局部变量,全局变量,类成员变量,闭包变量等。每种类型的变量都以不同的方式存储在不同的位置,但是(非常)概括地说,所有变量要么都存储在堆栈中(在程序的后面是线性的内存部分)执行路径)或堆(如果不是应用程序的生命周期,则在较长的过程中或多或少会存在或多或少存在的结构化的内存块。)将值分配给变量时,您就是告诉程序去变量在内存中的位置并使其等于该值。

To officialize my comments with an answer, when you talk about values being stored on the stack or on the heap, you are talking about variables. Variables exist in a bunch of different forms, such as local variables, global variables, class-member variables, closured variables, and so on. Each type of variable gets stored in a different place in a different way, but a (very) broad nutshell explanation is that all variables either get stored on the stack (a linear section of memory that follows the program's execution path) or the heap (a less structured blob of memory that more or less exists for the duration of a long process if not the app's lifetime.) When you assign a value to a variable, you are telling the program to go to that variable's location in memory and make it equal to that value.

常量完全是一个不同的概念。没有声明,实例化或给它们赋值,并且询问它们是存储在堆栈还是堆上是无意义的,因为它们不是变量。常量是在编译时已知并已分类的值,例如 1 true Hello World 。当声明一个常量时(例如 const SECONDS_IN_MINUTE = 60; ),您没有实例化变量,实际上是在创建一个已知值的别名,编译器将其替换为一个已知值使用该常数。 (即代码 int FiveMinutes = SECONDS_IN_MINUTE * 5; 将被编译为 int fourMinutes = 60 * 5; *)

Constants are a different concept entirely. They are not declared, instantiated, or assigned a value, and asking whether they are stored on the stack or on the heap is nonsensical because they are not variables. A constant is a value that is known and catalogued at compile time, such as 1, true, and "Hello World". When you declare a constant (e.g. const SECONDS_IN_MINUTE = 60;) you are not instantiating a variable, you are in essence creating an alias to a known value that the compiler substitutes in everywhere the constant is used. (i.e. The code int fiveMinutes = SECONDS_IN_MINUTE * 5; will be compiled into int fiveMinutes = 60 * 5;*)

同样, const Duration(seconds:1)不是变量或在运行时创建的对象,而是是在程序编译时以及运行之前已知的值。这就是为什么当您声明一个常量构造函数时,该类只必须具有 final 字段,并且参数只能是本身是常量的类型的原因,因为可以用

Likewise, const Duration(seconds: 1) is not a variable or an object that gets created at runtime but is a value that is known when the program is compiled and before it ever runs. This is why when you declare a constant constructor the class has to only have final fields and the parameters can only be types that are themselves constant, since an object that can be defined with non-constant fields is by definition not a constant.

此外,Dart支持称为<规范>规范常量的概念,这意味着每个常量创建指向相同值结构的点。例如:

What's more, Dart supports a concept called canonical constants, which means that every constant you create points to the same value structure. For example:

var a = Duration(seconds: 1);
var b = Duration(seconds: 1);
var c = Duration(seconds: 1);
var d = Duration(seconds: 1);

每个 a b c d 变量存储的是非恒定值,这意味着您有四个不同的 Duration 对象是分别创建的。另一方面:

Each of the a, b, c, and d variables are storing non-constant values, which means you have four different Duration objects that are created separately from each other. On the other hand:

var a = const Duration(seconds: 1);
var b = const Duration(seconds: 1);
var c = const Duration(seconds: 1);
var d = const Duration(seconds: 1);

为这些变量中的每个变量分配的常量值与 seconds相同,这意味着它们每个都指向相同的 Duration 值。当您创建使用许多可以保持不变的值的应用程序时,这是大量优化的源泉。 (例如,如果您有很多 Padding 小部件具有相同的填充,请将所有 EdgeInsets 更改为常量将阻止他们每次使用时创建新的 EdgeInsetsGeometry 。)

Each of these variables are assigned constant values with the same value for seconds, which means they each point to the same value of Duration. This is a source of a lot of optimization when you are creating an app that uses a lot of values that could be made constant. (For example, if you have a lot of Padding widgets that have the same padding, changing all the EdgeInsets to be constant will prevent them from creating a new EdgeInsetsGeometry every time you use it.)

*:或类似的东西,假设优化遍历当然不会将 60 * 5 更改为 300 或其他此类预测性优化。

*: Or something resembling this, assuming of course an optimization pass doesn't change 60 * 5 into 300 or other such predictive optimizations.

这篇关于Dart中const值的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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