FIX:- Memory Leakage issue Perl Socket Programming

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.

About: Mike

Milind Koyande loves to work on new technologies specially virtualization and troubleshoot server problems. I’m an avid photographer and love to spend my free time close to nature, trying to capture its glory on my camera.


One thought on “FIX:- Memory Leakage issue Perl Socket Programming”

  1. I’m dealing with the same issue on windows, using perl tk, threads and sockets.
    I will try the fix that you have found and let you know the results…..

    Congratulations for having solved this issue….

    Best regards,

    Jose

This site uses Akismet to reduce spam. Learn how your comment data is processed.