Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Critical memory leak with J/Link

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47530] Critical memory leak with J/Link
  • From: "J. McKenzie Alexander" <jalex at lse.ac.uk>
  • Date: Thu, 15 Apr 2004 03:39:57 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I've encountered a curious, and completely disabling, memory leak when 
I use J/Link.  The memory leak is such that a program can easily 
consume over 1.5GB of virtual memory and will crash the system if left 
to run for an extended period of time.  The leak occurs under both 
Windows XP and Mac OS X (Panther), with both machines running 
Mathematica 5.0 (J/Link version 2.1.1).  The XP box has Sun Java 
1.4.1_03 (build 2) installed, and the OS X box has java 1.4.2_03 (build 
1.4.2_03-117.1) installed.
	
Here's the simplest example I've found.

	// MemoryLeaker.java
	import com.wolfram.jlink.*;
	public class MemoryLeaker {
	    Expr expr;
	    public MemoryLeaker(Expr e) {
	        expr = e;
	    }
	    public Expr getExpr() {
	        return expr;
	    }
	}

Once MemoryLeaker is compiled, load it into Mathematica and use it as 
follows (adjusting the AddToClassPath command as necessary, of course).

	In[1]:=
	Needs["JLink`"];
	<<DiscreteMath`Combinatorica`

	In[3]:=
	AddToClassPath["/Users/jalex/Source/tmp/"];

	In[4]:=
	LoadJavaClass["MemoryLeaker"];

	In[5]:=
	g=GridGraph[50,50];

	In[6]:=
	memoryLeaker=JavaNew["MemoryLeaker",
	      NormalizeVertices[g]
	      ];

	In[7]:=
	Do[
	    memoryLeaker@getExpr[],{50}
	    ];

The above run leaves J/Link, on my OS X box, in the following state:

ID		Name	%CPU	Real Memory	Virtual Memory	
1287	 J/Link	0.00		265.50 MB	823.50 MB	

Memory is not freed with calls to JavaGC[].  It is only freed with 
QuitJava[] or quitting the kernel.

The real kicker is that if you replace line 6 with the following:

	In[6]:=
	memoryLeaker=JavaNew["MemoryLeaker", g ];

no leak occurs.  However, if you replace line 6 with

	In[6]:=
	g2 = NormalizeVertices[g];
	memoryLeaker = JavaNew["MemoryLeaker", g2];

you still get the memory leak.

Would someone please explain what I'm doing wrong in the above example 
or, if I'm not doing anything wrong, a work-around?

Cheers,

Jason
--
Dr J. McKenzie Alexander
Department of Philosophy, Logic and Scientific Method
London School of Economics and Political Science
Houghton Street, London WC2A 2AE


  • Prev by Date: Re: Solvability sensitive to small changes in numerical input
  • Next by Date: Re: Solvability sensitive to small changes in numerical input
  • Previous by thread: Re: Append to a file?
  • Next by thread: Re: Critical memory leak with J/Link