classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memory allocation


From: Etienne Gagnon
Subject: Re: memory allocation
Date: Mon, 28 Apr 2003 15:00:15 -0400
User-agent: Mozilla/5.0 (X11; U; Linux ppc; en-US; rv:1.3) Gecko/20030327 Debian/1.3-4

Archie Cobbs wrote:
The only way I could think of would be e.g. allocating memory by creating
byte[] arrays instead of using malloc(). This seems awkward.

Allocating a byte array and then obtaining a direct pointer to its
elements (or non-moveable copy) using GetByteArrayElements is effectively
the cleanest approach to allocate memory ** through JNI **.  [Note that the
native code must also call ReleaseByteArrayElements to "free" the array.]

Now, the only advantage I see to this approach is that it allows the JVM's 
garbage
collector to free this memory on exit.  Yet, this can also be a problem if
the native code needs to access this memory after calling DestroyVM.

This brings the following question:

What's wrong with using malloc/free in native code, in the first place?

Personally I think that there shouldn't be any problem for native code to
directly call malloc & free.

On the other hand, the GetByteArrayElements/ReleaseByteArrayElements pair should
be used to store pointers to native data in Java objects.  One shouldn't use 
int/long
fields as pointer size is inherently platform dependent (16,16,32,64,128 bits).

Etienne

--
Etienne M. Gagnon, Ph.D.             http://www.info.uqam.ca/~egagnon/
SableVM:                                       http://www.sablevm.org/
SableCC:                                       http://www.sablecc.org/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]