PHP 类对象和内存使用

发布于 2024-11-06 06:27:22 字数 95 浏览 1 评论 0原文

如果我有很多单个类的实例,类本身的大小(代码行数、方法数量)对所需的内存有任何影响吗?

我想知道将一些较少使用的方法移动到其他地方是否会对内存使用和性能有好处。

If I have a lot of instances of a single class, does the size of the class itself (the lines of code, the number of methods) have any influence on the required memory?

I'm wondering if it would be good for memory usage and performance to move some of the less-used methods to other places.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那片花海 2024-11-13 06:27:22

编译器在 include() 编辑时只会读取该类定义一次。如果实例化大量类实例,方法的数量和代码行数不应该对使用的内存量产生任何有意义的影响。然而,成员变量的数量当然会影响内存的使用。

The class definition would be read by the compiler only once at the time it is include()ed. The number of methods and number of lines of code should not have any meaningfull effect on the amount of memory used if you instantiate lots of class instances. However, the number of member variables will of course affect memory usage.

酒中人 2024-11-13 06:27:22

有些人会讨厌这个建议;有些人会讨厌这个建议。但是,如果您的类中有不特定于实例的方法/属性,请将它们设为静态方法/属性。非静态类方法/属性应该只是那些特定于实例的方法/属性。

一般来说,这不会对内存使用有太大帮助(使属性静态将有助于内存)。单个实例仅保存非静态类属性,并且无论有多少个实例,类方法仅在内存中保存一次。静态属性保存在全局级别(不要与全局工作区混淆),因此无论有多少实例,它们仅在内存中保存一次。

Some people will hate this suggestion; but if you have methods/attributes in your class that aren't specific to the instance, make them static methods/attributes. The non-static class methods/attributes should only be those that are instance specific.

Generally speaking, this won't help memory usage much (making attributes static will help memory). The individual instance only holds non-static class attributes, and the class methods are only held in memory once, no matter how many instances. Static attributes are held at a global level (don't confuse with global workspace), so they are only held in memory once, no matter how many instances.

伴随着你 2024-11-13 06:27:22

对象的内存使用量与数组的内存使用量相当。一个类会多消耗一点点字节。但这是不可测量的,除非您一次创建几千个对象(在这种情况下,您真正​​的问题是另一个问题)。

在幕后,您始终拥有一个用于类属性的字典,并且类定义具有一个用于现有方法的关联字典。后者在任何一种情况下都存在,并且仅添加另一个方法只会添加几个字节。事实上,这就像在主函数字典中注册一个全局函数一样。

所以不,避免类声明中的方法不会节省内存。这是不明智的,因为对象本身不会因此使用更多内存。方法列表不与对象实例关联。

The memory usage of objects is on par with that of arrays. A class eats a teensy bit more bytes. But that's not measurable unless you create a few thousand objects at once (in which case your real problem is another one).

Behind the scenes you always have a dictionary for the class attributes, and the class definition has an associated dictionary for the existing methods. The latter exists in either case, and just adding another method will add just a few bytes. In fact it's as much as registering a global function in the main function dictionary would.

So no, avoiding methods in your class declaration won't save memory. And it's not sensible, because the objects itself won't use more memory because of that. The method list isn't associated to the object instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文