12.07.2015 Views

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

24.5.1 OTclOTcl, especially TclCL, provides a way to alloc<strong>at</strong>e new objects. However, it does not accordingly provide a garbage collectionmechanism for these alloc<strong>at</strong>ed objects. This can easily lead to unintentional memory leaks. Important: tools such as dmalloc<strong>and</strong> purify are unable to detect this kind of memory leaks. For example, co<strong>ns</strong>ider this simple piece of OTcl script:set <strong>ns</strong> [new Simul<strong>at</strong>or]for set i 0 $i < 500 incr iset a [new R<strong>and</strong>omVariable/Co<strong>ns</strong>tant]One would expect th<strong>at</strong> the memory usage should stay the same after the first R<strong>and</strong>omVariable is alloc<strong>at</strong>ed. However, becauseOTcl does not have garbage collection, when the second R<strong>and</strong>omVariable is alloc<strong>at</strong>ed, the previous one is not freed <strong>and</strong>hence results in memory leak. Unfortun<strong>at</strong>ely, there is no easy fix for this, because garbage collection of alloc<strong>at</strong>ed objects isessentially incomp<strong>at</strong>ible with the spirit of Tcl. <strong>The</strong> only way to fix this now is to always explicitly free every alloc<strong>at</strong>ed OTclobject in your script, in the same way th<strong>at</strong> you take care of malloc-ed object in C/C++.24.5.2 C/C++Another source of memory leak is in C/C++. This is much easier to track given tools th<strong>at</strong> are specifically designed for thistask, e.g., dmalloc <strong>and</strong> purify. <strong>ns</strong>has a special target <strong>ns</strong>-pure to build purified <strong>ns</strong> executable. First make sure th<strong>at</strong> the macroPURIFY in the <strong>ns</strong> Makefile contai<strong>ns</strong> the right -collector for your linker (check purify man page if you don’t know wh<strong>at</strong> thisis). <strong>The</strong>n simply type make <strong>ns</strong>-pure. See earlier sectio<strong>ns</strong> in this chapter on how to use <strong>ns</strong> with libdmalloc.219

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!