Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: FIX:- Memory Leakage issue Perl Socket Programming  (Read 3445 times)

0 Members and 1 Guest are viewing this topic.

sajugovind

  • Guest
FIX:- Memory Leakage issue Perl Socket Programming
« on: December 11, 2013, 05:35:56 pm »


Today through this Article I am going to share a fix for memory leakage in Perl Socket Programming.

Many times we use IO::Socket module to establish sockets in code and while creating a object using following code snippet:-

my $socket = IO::Socket::INET->new (PeerAddr=>$remote_ip,PeerPort=>$port,Proto=>’tcp’);
my $output=<$socket> ;

This all was done using multi threading in a while(1) loop and the moment code was executed it started to consume a whopping 265MB memory. This increased gradually to almost 600MB and finally eat up the 2.5GB swap memory and thus was memory overflow.

We have searched inside out of numerous forums and found a thread that had almost the same issue we had. Thus we went through the details and found an issue a very interesting fact about sockets.
It stated the fact that these sockets have a circular reference in them. And that clicked and then we simply placed a single liner to fix this bug:-

eval { close $socket; };undef $socket;

After making above changes our code started consuming memory much lesser than the previous one. It was playing around 125-150MB and didn’t go beyond that value.

This valuable FIX is shared by my friend and colleague “Hussain Dahodwala” who works in Research and Development wing of ESDS.