jump to navigation

Problem bad_alloc in ns2 May 30, 2014

Posted by aghus in Bebas.

I faced bad_alloc problem in ns2 when I apply many nodes into simulation. After googling some times I see the problem is caused by a lack of memory. I found the solution by minimizing number of packet header in ns2 simulation.

I copy an article*, as follows …

The implementation of the Packet data structure of NS2 does not math the realities. The packet in ns2 simulation keeps all packet headers for any protocols implemented in NS2. For example, a DSR routing packet may keep DSDV, AODV, or even a PING application header. For this reason, till today, a packet used in ns2 simulation, would have a header size around 40~64KB. And NO packet would be deleted to release the memory it holds until the end of the simulation.
So for a typical simulation with 100 nodes in ns2 around 1M packets exchanged (of course, you may reuse the packets already being freed through Packet::free(Packet*). To learn the implementation of it, please check file common/packet{.h,.cc} ), you may hold 10% of it, 100K packets, and you may use a memory at least 100K*64KB -> 6.4GB, which definitely would crash your computer (even it is a super server).
So How you can do it? And how you can run a simulation like 500 nodes there? You have 2 ways to do:
Suggested by ns2 manual, putting the below codes in your tcl script

add-packet-header DSR ARP LL MAC CBR IP

to simulation a CBR application on UDP with DSR as routing protocol in a wireless ad hoc network (OOPS!, UDP is not a header. This method is effective, but it requires you to understand most packets header you need.
Another way is my way, changing the tcl library for the packet headers. You may find the tcl library file for packet headers in ns2/tcl/lib/ns-packet.tcl, you may find a procedure starting as foreach prot {. You can comment out all the headers you don’t recognize, like all the Routing protocols you dont know, all Routers, all Multicast, all Transport Protocols except you need, all application layer protocols, some of the Wireless, some of the Mobility, Ad-Hoc Networks, Sensor Nets and all the Other. Finally, you may just have all the below left

foreach prot {
# Common:
# Transport Protocols
# Wireless
# Mobility
} {
add-packet-header $prot

If you are creating your own packet header, put them here.
Typically, after this way, you may just have a packet size as 256B. So even you have 500 nodes, and 10M packets need to be exchanged during the simulation. You just need 256MB (if 10% packets held) for it, which is lower than any common configuration of current PCs.

*www dot cs dot binghamton dot edu /~kliu/research/ns2code/



1. DaoThiNga - August 11, 2015

Thank you so much! I got this problem and now I fixed it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: