Skip to content Skip to sidebar Skip to footer

Memory Being Allocated

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 hav

Solution 1:

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.

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.

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.

Post a Comment for "Memory Being Allocated"