Return-Path: Delivered-To: apmail-incubator-harmony-dev-archive@www.apache.org Received: (qmail 74908 invoked from network); 25 Jan 2006 12:38:29 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 25 Jan 2006 12:38:29 -0000 Received: (qmail 80471 invoked by uid 500); 25 Jan 2006 12:37:35 -0000 Delivered-To: apmail-incubator-harmony-dev-archive@incubator.apache.org Received: (qmail 80405 invoked by uid 500); 25 Jan 2006 12:37:34 -0000 Mailing-List: contact harmony-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-dev@incubator.apache.org Received: (qmail 80394 invoked by uid 99); 25 Jan 2006 12:37:34 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [192.87.106.226] (HELO ajax.apache.org) (192.87.106.226) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Jan 2006 04:37:32 -0800 Received: from ajax.apache.org (ajax.apache.org [127.0.0.1]) by ajax.apache.org (Postfix) with ESMTP id C741FE0 for ; Wed, 25 Jan 2006 13:37:10 +0100 (CET) Message-ID: <1757937913.1138192630814.JavaMail.jira@ajax.apache.org> Date: Wed, 25 Jan 2006 13:37:10 +0100 (CET) From: "Paulex Yang (JIRA)" To: harmony-dev@incubator.apache.org Subject: [jira] Updated: (HARMONY-38) com.ibm.platform.struct.AbstractMemorySpy doesn't actually autoFree native memory In-Reply-To: <264549105.1138015989216.JavaMail.jira@ajax.apache.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N [ http://issues.apache.org/jira/browse/HARMONY-38?page=all ] Paulex Yang updated HARMONY-38: ------------------------------- Attachment: Harmony38-AbstractMemorySpy-patch.txt Harmony38-RuntimeMemorySpy-patch.txt Thank you, Tim. Here goes the patches for PhantomReference issue, including ones for AbstractMemorySpy and PhantomMemorySpy. > com.ibm.platform.struct.AbstractMemorySpy doesn't actually autoFree native memory > --------------------------------------------------------------------------------- > > Key: HARMONY-38 > URL: http://issues.apache.org/jira/browse/HARMONY-38 > Project: Harmony > Type: Bug > Components: Classlib > Reporter: Paulex Yang > Assignee: Tim Ellison > Priority: Critical > Attachments: Harmony38-AbstractMemorySpy-patch.txt, Harmony38-RuntimeMemorySpy-patch.txt > > com.ibm.platform.struct.IMemorySpy is a memory watcher, and the AbstractMemorySpy is a default implementation working with nio direct buffers to free the native memories when direct buffer instances are garbage collected. But currently its auto free donsn't work. > Look inside the orphanedMemory(Object obj) method, the memory is freed only if the local variable wrapper can be found from the memoryInUse, a Map which records the native memories used by direct buffers, but actually it will be removed when autoFree(PlatfromAddress) is invoked. Checking the autoFree() reference, I found that all DirectByteBuffer will invoke autoFree() in constructor, which means no memory will be freed. > A simple fix: > modify the Ln 94 in autoFree() from: > wrapper = (AddressWrapper) memoryInUse.remove(address); > to: > wrapper = (AddressWrapper) memoryInUse.get(address); > and it should work. > The steps to reproduce the bug: > 1. Add the following line in the the Ln109, which indicates the memory is auto freed by AbstractMemorySpy > System.out.println("MemorySpy autofree:"+wrapper.shadow); > 2. Run the test below: > public class MemorySpyTest { > public static void main(String[] args) throws Exception { > for (int i = 0; i < 1000; i++) { > try{ > ByteBuffer buffer = ByteBuffer.allocateDirect(1024); > buffer = null; > }catch(Exception e){ > e.printStackTrace(); > } > System.gc(); > } > } > Before modification: application exits silently. > After modification, hundres of lines printed out, like this: > ..... > MemorySpy autofree:PlatformAddress[9310024] > MemorySpy autofree:PlatformAddress[9311056] > MemorySpy autofree:PlatformAddress[9312088] > MemorySpy autofree:PlatformAddress[9313120] > MemorySpy autofree:PlatformAddress[9314152] > MemorySpy autofree:PlatformAddress[9315184] > MemorySpy autofree:PlatformAddress[9316216] > MemorySpy autofree:PlatformAddress[9317248] > ....... > Another possible pitfall in AbstractMemorySpy is it uses WeakReference and ReferenceQueue to get notification when the buffers are ready to be garbage collected. But it is not so safe yet to free the native memory, because the weakly-reachable object has its chance to be resurrected to strongly-reachable, if only some finalize() method doing this is executed. A safer solution is to use PhantomReference instead, because phantom-reachable object has no chance to survive. The only concern is the PhantomReference should be explicitly cleared by application, but that's OK because the orphanedMemory() method is a perfect candidate to do this. > I will attach the patch soon. -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira