Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

object and variables scope question

32 views
Skip to first unread message

Svetlana Rakov

unread,
Dec 17, 2002, 3:43:14 PM12/17/02
to

hi guys.
i have a question regarding memory management in javascript.
let's say i have a function:

function f()
{
var str = "12-34-56";
var ar = str.split('-');
}

during execution, at some point, 'ar' will have values '12','34','56', so
it is an array. did split() call something like ar = ...new Array() ...?
do i have to do anything to deallocate memory (destroy 'ar')? what is
procedure to deallocate javascript objects? custom objects?
does a local variable of any type will be destroyed atomaticaly?
thanks in advance.
arthur amshukov


Laurent Bugnion, GalaSoft

unread,
Dec 18, 2002, 5:00:06 AM12/18/02
to
Hi,

split() allocates a new Array, as you found out.

JavaScript has a garbage collector. When an object is not referenced
anymore, anywhere in the current script, it is collected and the memory
will be freed... sometimes.

The problem is that you don't really have a way to know when the garbage
collector will free the memory. It is a task with low priority (because
it is time consuming).

In most cases, however, you don't really have to care about this.

To force an object to be collected and later deleted, set all its
references to null.

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Grant Wagner

unread,
Dec 18, 2002, 10:34:44 AM12/18/02
to
"Laurent Bugnion, GalaSoft" wrote:

IE has the undocumented CollectGarbage() method, but I don't know how much good
it does. Quite frankly however, given the types of tasks that client-side
JavaScript has to do, and the amounts of data it is usually dealing with, it's
simplistic enough to let the browser manage memory for you. In situations where
intermitant garbage collection may cause "performance" problems (such as in
animation), or you are dealing with huge amounts of information, then my first
suggestion would be to re-examine the use of client-side JavaScript to solve
the problem.

These are all general rules, of course there are situations where you have a
huge JavaScript array which has to be sorted on the client, etc. In these
cases, you optimize as best you can and hope that the performance is good
enough. Here's an example:

This is really really bad:

<script language="JavaScript" type="text/javascript">
// this array is actually about 200 stations long, output by server-side code
var asStations = [ '<option value="" selected>All Stations</option>',
'<option value="8002008">ACHESON TERMINAL - * (8002008) (W7)</option>',
'<option value="6004402">AGASSIZ - * (6004402) (E5)</option>',
'<option value="7006018">ALAMEDA (7006018) (E3)</option>' ];
</script>
<!-- later on page -->
<td>CPS Station: <script>
document.writeln('<select>');
for (var i = 0; i < asStations.length; i++) {
document.writeln(asStations[i]);
}
document.writeln('</select>');
</script></td>
<td>Grain Station: <script>
document.writeln('<select>');
for (var i = 0; i < asStations.length; i++) {
document.writeln(asStations[i]);
}
document.writeln('</select>');
</script></td>

Much, much, better:

<script language="JavaScript" type="text/javascript">
var asStations = [ '" selected>All Stations',
'8002008">ACHESON TERMINAL - * (8002008) (W7',
'6004402">AGASSIZ - * (6004402) (E5',
'7006018">ALAMEDA (7006018) (E3' ];

var sStations = '<option value="' + asStations.join(')</option>\r\n<option
value="') + ')</option>';
</script>
<!-- later on page -->
<td>CPS Station: <script>
document.writeln('<select>', sStations, '</select>');
</script></td>
<td>Grain Station: <script>
document.writeln('<select>', sStations, '</select>');
</script></td>

Not only does it send less data to the browser, but it's significantly faster
to render in all browsers.

--
| Grant Wagner <gwa...@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://developer.netscape.com/docs/manuals/javascript.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtmlrefs.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html


0 new messages