Thread

Posted on Wed Nov 29 07:05:36 2006 by stratimus
Thread memory not being released.

Hello all,

I have a MUD engine I'm currently writing and the basic structure at the moment is:

Main script initializes back-end connection for administation

This back-end module runs in its own thread and listens for clients to connect.

Upon connection, a new object is made with the Client class which takes commands from the client.

I have an object that is shared across all modules and threads that holds information about the server (uptime, clients connected, etc). While the in_motion method returns true, these things such as the back end and client threads keep doing their thing. The problem is that when a client leaves its memory is not erased. I'm suspecting that it's because of this object shared with everything, but the problem is that I need it to be shared so threads/modules know what's going on. Does anyone have any suggestions on a better way of doing it or a method of getting the memory released? Usually it's not a big deal but this is a server that's meant to run 24/7 and when each client takes 2MB it builds quickly.

Thanks.

Direct Responses: 3661 | Write a response
Posted on Wed Nov 29 23:20:18 2006 by jdhedden in response to 3651
Re: Thread memory not being released.
First of all, make sure you've installed the lastest versions of 'threads' and 'threads::shared' from CPAN. Older versions have been known to leak memory.

You're not clear in your message (and you didn't supply any code) as to what constitutes the 'memory for a client'. If it is stored in a 'field' in your object, then you must delete the field:
delete($obj->{client})
Also, if the client uses its own thread, and that thread exits when the client leaves, then you must either detach the thread or join it. Hope that helps.
Direct Responses: 3663 | Write a response
Posted on Thu Nov 30 00:43:29 2006 by stratimus in response to 3661
Re: Thread memory not being released.
Sorry to not elaborate more. I should probably also mention that this is on a Win32 system for the time being, as I know it handles memory slightly differently. First off, I have object $Otherbody of the Otherbody class. Each module exports that variable so it can be accessed anywhere, and it's also shared across threads. Such methods on it are:
$Otherbody->uptime $Otherbody->master_password $Otherbody->in_motion (Which returns true as long as the server should be running) $Otherbody->shutdown (Which sets in_motion to false and cleans up some things) Etc.
Basically, the main script calls module Ansible.
my $AnsibleThread = threads->create( sub { Ansible::Initiate; } );
Part of Ansible::Initiate:
while($Otherbody->in_motion) { my $sock = $socket->accept(); if($sock) { $identifier++; $Otherbody->ansible_clients(2); #Raises the count on admins connected $thread[$identifier] = threads->create( sub { my $master = new Master($sock); $master->begin; pr +int "They left"; STDOUT->flush; close($sock); } ); } usleep(1000); }
And then in the Master module, just like the Ansible module, all variables are private with the exception of $Otherbody which as I have said as shared in everything. Once the client is gone, begin ends and it's cleaned up. Am I doing this a bad way or is there just some circular reference causing this that can be fixed? I feel it might be $Otherbody causing it. Hope that is good enough, thanks.
Direct Responses: 3664 | Write a response
Posted on Thu Nov 30 01:59:06 2006 by jdhedden in response to 3663
Re: Thread memory not being released.
As I mentioned before, you need to make sure your threads are being cleaned up at some point:
$thread[$identifier]->join(); delete($thread[$identifier]);
Also, you may need to do something similar with $AnsibleThread:
$AnsibleThread->join(); undef($AnsibleThread);
Direct Responses: 3665 | Write a response
Posted on Thu Nov 30 02:32:39 2006 by stratimus in response to 3664
Re: Thread memory not being released.
That partially worked, thanks. Now instead of losing the entire 2MB it only loses about 600KB. I suspect the last bit is just minor variables that didn't get cleaned up, or the socket.
Write a response