X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 786D3D1B1C8 for ; Tue, 22 Jun 2004 00:41:46 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 76366-08 for ; Tue, 22 Jun 2004 03:41:37 +0000 (GMT) Received: from yass.opencloud.co.nz (yass.opencloud.co.nz [203.79.85.162]) by svr1.postgresql.org (Postfix) with ESMTP id 164DED1B170 for ; Tue, 22 Jun 2004 00:41:36 -0300 (ADT) Received: from 203-79-104-60.cable.paradise.net.nz ([203.79.104.60] helo=opencloud.com) by yass.opencloud.co.nz with asmtp (Exim 3.35 #1 (Debian)) id 1BccA1-0007aA-00; Tue, 22 Jun 2004 15:41:25 +1200 Message-ID: <40D7AABD.10503@opencloud.com> Date: Tue, 22 Jun 2004 15:42:53 +1200 From: Oliver Jowett User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 To: "Marcus Andree S. Magalhaes" Cc: sylvain.laurent@elca.ch, pgsql-jdbc@postgresql.org Subject: Re: Bug (and fix): leaks of TCP connections when connected References: <40D7937A.3040703@opencloud.com> <61835.200.174.151.240.1087873026.squirrel@200.155.0.57> In-Reply-To: <61835.200.174.151.240.1087873026.squirrel@200.155.0.57> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200406/144 X-Sequence-Number: 10346 Marcus Andree S. Magalhaes wrote: > > >>In theory the discarded connections should eventually be garbage >>collected and closed, right? So at least the leak is bounded. > > I don't think so. We had a similar problem here. > > After many hours of debugging, we came to the fact that either > java garbage collector does _not_ close open connections It's not the GC that directly closes the open connections; it just causes unreachable objects to be collected. It's the responsibility of object that holds out-of-heap resources to do whatever cleanup (OS-level close() calls etc) is needed when it becomes unreachable, traditionally via finalize() (although you can also use ReferenceQueue). I'd expect the socket implementation to do this. > or do it > after a long time (much longer than one would expect). Typically finalization and Reference clearing happens only on full collections (it's not guaranteed to happen this way, though), so I'd expect the connections to be collected then. It can take some time for a full GC to happen, depending on your heap settings. We see problems in NIO buffer allocation when System.gc() is disabled (see below) on a quiescent system that can have intervals of over a day between full GCs. If you leak sockets or some other resource rapidly enough that you run out of the resources at the OS level (or indirectly hit a resource limitation on the remote side e.g. server connection limit) before the owning objects are collected, you will have a problem. NIO direct buffer allocation hits exactly this issue, and ends up issuing a System.gc() when it thinks it's out of direct buffer space to try to avoid the problem. There are some bugs in Sun's implementation though which make it hard to use (it forces a stop-the-world GC which is bad if you are using the CMS collector, and there is a race between the allocating thread returning from System.gc() and buffer references being cleared in the reference-handler thread). Assuming those problems can be fixed, there's an argument for doing the same thing in the socket allocation code (and anywhere else that allocates out-of-heap resources) too. It becomes a question of how far to take it: if the server refuses a connection with "too many connections", should you GC and try again if that collected any connections to the server in question? See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5025281 (apologies for rambling :) -O