Skip to main content

1.6.0_10 and 11, Memory leak returning variables from Java to JavaScript

1 reply [Last post]
robjd
Offline
Joined: 2008-09-25
Points: 0

Environment: Windows XP SP2, IE7, JScript 5.7, JRE 1.6.0_11-b03 and 1.6.0_10

Memory allocated in Java and returned as a JavaScript variable value is not garbage collected in Java when the JavaScript variable is re-used.

In the following simplified example, a populated HashMap is returned from the applet test1 method to the JavaScript variable hash1.
The JavaScript variable hash1 is re-used on the next iteration so I would expect it should be marked for garbage collection.

In 1.6.0_7 and previous releases, the memory associated with this Java HashMap is freed implicitly.
In 1.6.0_10 and 1.6.0_11 the HashMap memory is never freed.

Is this is what we should expect, a feature of the significant LiveConnect changes that were part of 1.6.0_10?
Do we need to rewrite our code to call new Java methods from JavaScript explicitly freeing the memory, in this example calling the freeMem method?

Has anybody else experienced this problem since 1.6.0_10?

Thanks
Rob

Example code ...

TestRLApplet.java

import java.applet.Applet;
import java.util.HashMap;

public class TestRLApplet extends Applet
{

private HashMap hash1;

public TestRLApplet()
{
}

public void init()
{
}

public HashMap test1()
{
hash1 = new HashMap();
for (int i = 0; i <10; i++) {
hash1.put(Integer.toString(i),Integer.toString(i));
}
return (hash1);
}

public void freeMem() {
hash1.clear();
hash1 = null;
}

}

TestClient.htm

function window_onload()
{
var hash1;
testAppplet = document.TestRLApplet;

for (cnt=0; cnt < 1000; cnt++)
{
hash1= testAppplet.test1();
}

}

Java Console

Java Plug-in 1.6.0_10
Using JRE version 1.6.0_10 Java HotSpot(TM) Client VM

----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to
----------------------------------------------------

Memory: 5,056K Free: 2,551K (50%) ... completed.
Memory: 7,996K Free: 3,499K (43%) ... completed.
Memory: 7,996K Free: 1,038K (12%) ... completed.
Memory: 13,180K Free: 3,745K (28%) ... completed.
Memory: 22,844K Free: 5,614K (24%) ... completed.
Memory: 37,984K Free: 15,009K (39%) ... completed.
Memory: 37,984K Free: 13,069K (34%) ... completed.
Memory: 37,984K Free: 6,125K (16%) ... completed.
Memory: 65,088K Free: 25,107K (38%) ... completed.
Memory: 65,088K Free: 21,201K (32%) ... completed.
Memory: 65,088K Free: 13,381K (20%) ... completed.
Memory: 65,088K Free: 7,967K (12%) ... completed.
Memory: 65,088K Free: 5,013K (7%) ... completed.
Memory: 65,088K Free: 414K (0%) ... completed.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
robjd
Offline
Joined: 2008-09-25
Points: 0

I realise that this is not the correct forum for a Plug-in issue. I have marked it as answered in this forum and raised it instead on the Java Plug-in forum:

http://forums.java.net/jive/thread.jspa?threadID=56085