分配的内存 [英] Memory being allocated

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

问题描述

通过使用Chrome开发工具,我发现正在分配数组和对象.我遍历了代码,寻找明显的[]{}new.但是没有.我检查了创建新的[]{}new的函数,并查看了这些函数的使用位置,并且我学会了不使用它们.那么,还可以如何分配内存?

By using the Chrome Development Tools, I found out arrays and objects were being allocated. I gone through my code looking for the obvious [], {} andnew. But there isn't any. I have checked functions that create a new [], {}, new and looked to see where those functions are used, and I've learnt not to use them. So, how else can memory be allocated?

这对我来说是个问题,因为每次GC启动时,它都会阻塞主循环,并且动画会变得不一致.

This is a problem for me, because every time GC kicks in, it blocks the main loop and the animation becomes inconsistent.

推荐答案

过多地担心内存分配是徒劳的.内存将分配给所有内容,变量,数组,对象等.如果不使用变量或对象,使用javascript可以做的事情并不多,但是同样,内存的分配实际上并不是javascript的领域脚本.无论哪种方式,任何JavaScript都将使用一定程度的内存.的确,我想说的是,如果您学会避免使用"对象和数组,那么您就会被误导,或者正在学习错误的课程.

It is fruitless to worry overmuch about memory allocation. Memory will be allocated for everything, variables, arrays, objects, etc. There isn't much you could do with javascript without using a variable or an object, but again, the allocation of memory is not really the domain of a javascript script. Any and all javascript will use some degree of memory no matter what. Indeed, I would say that if you have "learned to avoid using" objects and arrays, you have been misinformed or are learning the wrong lesson.

避免循环引用,避免每个范围的消耗过多的内存,并且通常避免使用紧密循环和其他不良做法来锁定浏览器线程,这一点更为重要.例如,在for循环中,避免重新计算for声明中的限制:for (var x = 1; x < myString.length; x++)应该为var max = myString.length; for(var x = 1; x < max; x++).即使这样的优化(在大多数情况下为 micro 优化)对于javascript开发人员也并非至关重要,因为浏览器正在处理总体内存分配/消耗以及垃圾回收范围外引用.

It is far more important to avoid circular references, to avoid excessive memory consumption per scope, and to generally avoid locking up the browser thread with tight loops and other bad practices. For instance, in a for loop, avoid recalculating the limit in the for declaration: for (var x = 1; x < myString.length; x++) should be var max = myString.length; for(var x = 1; x < max; x++). Even such optimizations (micro-optimizations in most cases) are not critical to a javascript developer, for the browser is handling the overall memory allocation/consumption as well as the garbage collection of out-of-scope references.

有关避免泄漏的实践的更多信息,请查看本文: http://www.javascriptkit.com/javatutors/closuresleak/index.shtml (或类似的其他文章).否则,只要您不泄漏内存,就可以预期任何脚本都会分配/使用一定程度的内存.这是不可避免的.考虑到现代PC拥有 GB 的可用内存,因此脚本的微不足道的千字节甚至兆字节的使用并不重要-就是这样内存就可以使用了.

For more information about practical practices to avoid leaks, check out this article: http://www.javascriptkit.com/javatutors/closuresleak/index.shtml (or other articles like this). Otherwise, as long as you aren't leaking memory, it is expected that any script will allocate/use some degree of memory; it is unavoidable. Considering that the modern PC has gigabytes of memory available, your script's paltry kilobytes or even megabytes of memory use are not of much consequence - that's what the memory is there for, to use it.

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

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