[R] SJava memory managment

von Otte, Ingo ingo.von.otte at sdm.de
Wed Sep 4 13:32:09 CEST 2002


 
Hi everyone,
 
I am using the SJava library from the Omegahat project to call Java from R.
Recently I ran into trouble with the memory management in SJava. So I have
some questions:
 
1. What is the right way to release Java objects from R?
 
In the SJava documentation I have found no example on how to free Java
objects from R. After some research I have found a method "remove" in the
class "OmegaInterfaceManager", which seems to to what I want. Thus I tried
it to free Java objects the following way:
.Java("__Evaluator","remove",obj[[1]]), where obj[[1]] is the key of obj in
the reference database.
 
However, the following code indicates that this does not work:
 
x <- .JNew("Test")
for( i in 1:600 )
{
    print(i)
    y <- x$getLargeMap()
    print( y$size() )
    ok <- .Java( "__Evaluator", "remove", y[[1]] )
    stopifnot( ok )
}
 
The class Test is defined as follows:
 
public class Test
{
    public int[] getLargeArray()
    {
        return new int[256*1024];
    }
 
    public Map getLargeMap()
    {
        Map map = new HashMap();
        map.put( "key", getLargeArray() );
        return map;
    }
 
    public int getInt()
    {
        return 1;
    }
}
 
What happens is, that the loop fails in the 63 iteration. Since the returned
object is 1MB large, and the Java VM has a memory limit of 64 MB, my guess
is that the objects are not freed. In contrast to that, the call to "remove"
does return "TRUE". Am I doing anything wrong? Or is there a bug in SJava?
 
2. SJava looses memory when Java is called from R
 
The execution of the following code indicates, that each call to Java from R
causes the Java VM to loose memory, although it is not necessary:
 
x <- .JNew("Test")
for( i in 1:62 ) # allocate almost all memory
{
    print(i)
    y <- x$getLargeMap()
    stopifnot( !is.null(y) )
}
for( i in 1:1000000 ) # now loop without consuming memory
{
    y <- x$getInt()
    if( is.null(y) )
    {
        print(i)
        stop("out of memory!")
    }  
}
 
The second loop fails after 2242 iterations on my machine. Since no
parameters have to be passed to or from Java I would expect the second loop
to succeed.
 
3. Is it possible to specify the maximum memory usage of Java when it is
used inside R?
 
As I stated above, I observed that the Java VM has a maximum heap size of
64MB when it is started inside R. For my application it would be very useful
to configure the VM to use more memory, for example 256MB. So far, I have
found no way to configure the Java VM like this, since the only way to pass
configuration parameters seems to be javaConfig(), and javaConfig() does
only allow to specify the classpath and system parameters. 
 

I am willing to debug/fix the problem myself, but the SJava library is
shipped without the Java sources and without commented C sources. So, I
would appreciate any hint, what I am potentially doing wrong or how I can
obtain the full sources.
 
Ingo



-- 
Ingo von Otte               mailto:ingo.von.otte at sdm.de
<mailto:ingo.von.otte at sdm.de>  
sd&m AG                   http://www.sdm.de <http://www.sdm.de/>  
software design & management 
Am Schimmersfeld 7a, 40880 Ratingen, Germany 
Tel +49 2102 9957-967, Fax -50 

 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list