The following script is modified slightly from:

to make it run "forever". However, a memory leak occurs and eventually we see:

Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space
Exception in thread Thread:Traceback (most recent call last):
Exception in thread "MainThread" java.lang.OutOfMemoryError: Java heap space
  File "C:\jython2.5.2b2\Lib\", line 179, in _Thread__bootstrap

Can this be modified such that no memory leak occurs? Is this potentially a bug in Jython?

I am running 2.5.2b2, however this behaviour is observed on 2.5.1 also. Running Sun JDK on Win32 (XP).




import socket
import threading
import SocketServer
import time

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        data = self.request.recv(1024)
        cur_thread = threading.currentThread()
        response = "%s: %s" % (cur_thread.getName(), data)

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):

def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    response = sock.recv(1024)
    print "Received: %s" % response

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost",  0

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one
    # more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)
    # Exit the server thread when the main thread terminates
    print "Server loop running in thread:", server_thread.getName()

    i = 0
    while True:
        i += 1
        client(ip, port, "Hello World %i" % i)