From: Zhigang Wang Subject: [PATCH] python/xc: add missing Py_DECREF() to fix a memory leak Date: Fri, 28 Aug 2015 17:35:18 -0400 Message-ID: <1440797718-28045-1-git-send-email-zhigang.x.wang@oracle.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1ZVRJA-0006KL-DB for xen-devel@lists.xenproject.org; Fri, 28 Aug 2015 21:35:36 +0000 Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t7SLZXe3022122 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 28 Aug 2015 21:35:33 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userv0022.oracle.com (8.13.8/8.13.8) with ESMTP id t7SLZX1H016738 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Fri, 28 Aug 2015 21:35:33 GMT Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by userv0121.oracle.com (8.13.8/8.13.8) with ESMTP id t7SLZWbi029064 for ; Fri, 28 Aug 2015 21:35:32 GMT List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: Zhigang Wang List-Id: xen-devel@lists.xenproject.org Python PyList_Append() will increase reference count of the item. We have to decrease its reference count to let it garbage collected. We missed the Py_DECREF() for 'cpuinfo_obj' here, thus we have a memory leak. The memory leak could be easily confirmed by: # python >>> import xen.lowlevel.xc >>> xc = xen.lowlevel.xc.xc() >>> for i in range(1000): xc.getcpuinfo(1) And check the python process memory usage before and after: # ps f -o vsize,rss,%mem,size,cmd -p Signed-off-by: Zhigang Wang --- tools/python/xen/lowlevel/xc/xc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/python/xen/lowlevel/xc/xc.c b/tools/python/xen/lowlevel/xc/xc.c index 2c36eb2..9a161d3 100644 --- a/tools/python/xen/lowlevel/xc/xc.c +++ b/tools/python/xen/lowlevel/xc/xc.c @@ -1210,6 +1210,7 @@ static PyObject *pyxc_getcpuinfo(XcObject *self, PyObject *args, PyObject *kwds) for (i = 0; i < nr_cpus; i++) { cpuinfo_obj = Py_BuildValue("{s:k}", "idletime", cpuinfo_ptr->idletime); PyList_Append(cpuinfo_list_obj, cpuinfo_obj); + Py_DECREF(cpuinfo_obj); cpuinfo_ptr++; } -- 2.1.0