public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* [GCJ-core] dynamically unloading native libraries
@ 2008-10-07 12:29 Sylvain Marié
  2008-10-07 12:42 ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Sylvain Marié @ 2008-10-07 12:29 UTC (permalink / raw)
  To: java

Hi all,

I am working on a project involving GCJ on an embedded Linux busybox. 
We are particularly fond of the ability of GCJ to use java in "compiled mode".

In order to be able to use both native code (.so), compiled java (.so) and bytecode java 
(.jar, .class) together with a module loading system, we are running some tests to validate 
that dynamic code loading/unloading works fine.

Strangely enough this does not work on GCJ, whereas it works fine on sun's jvm (a native 
library is fully unloaded once its classloader is garbaged out).

see also : 
> openJDK, in jdk/src/share/classes/java/lang/ClassLoader.java: finalize method unloads native libraries


Is this feature planned to be inetgrated in next versions of the runtime ?
Thank you in advance for your support 
- congratulations for your amazing work so far ! 

Best regards


Sylvain

Sylvain MARIÉ
Schneider Electric DInnov, 
Software Engineering Technologies




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [GCJ-core] dynamically unloading native libraries
  2008-10-07 12:29 [GCJ-core] dynamically unloading native libraries Sylvain Marié
@ 2008-10-07 12:42 ` Andrew Haley
  2008-11-02 17:43   ` Mark Wielaard
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Haley @ 2008-10-07 12:42 UTC (permalink / raw)
  To: Sylvain Marié; +Cc: java

Sylvain Marié wrote:
> Hi all,
> 
> I am working on a project involving GCJ on an embedded Linux busybox. 
> We are particularly fond of the ability of GCJ to use java in "compiled mode".
> 
> In order to be able to use both native code (.so), compiled java (.so) and bytecode java 
> (.jar, .class) together with a module loading system, we are running some tests to validate 
> that dynamic code loading/unloading works fine.
> 
> Strangely enough this does not work on GCJ, whereas it works fine on sun's jvm (a native 
> library is fully unloaded once its classloader is garbaged out).
> 
> see also : 
>> openJDK, in jdk/src/share/classes/java/lang/ClassLoader.java: finalize method unloads native libraries
> 
> 
> Is this feature planned to be inetgrated in next versions of the runtime ?

No.  While this could be done, it would be quite tricky.

The core problem is that pointers to code (e.g. return addresses in
the stack) aren't recognized by the garbage collector as pointers to
the corresponding classes.  You'd have to teach the collector to
convert code addresses to classes, and then garbage collect the
classes.

Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [GCJ-core] dynamically unloading native libraries
  2008-10-07 12:42 ` Andrew Haley
@ 2008-11-02 17:43   ` Mark Wielaard
  2008-11-03 10:46     ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Wielaard @ 2008-11-02 17:43 UTC (permalink / raw)
  To: Andrew Haley; +Cc: Sylvain Marié, java

Hi,

Responding to a pretty old email that I only saw just now for some
reason.

On Tue, 2008-10-07 at 13:41 +0100, Andrew Haley wrote:
> Sylvain Marié wrote:
> > I am working on a project involving GCJ on an embedded Linux busybox. 
> > We are particularly fond of the ability of GCJ to use java in "compiled mode".
> > 
> > In order to be able to use both native code (.so), compiled java (.so) and bytecode java 
> > (.jar, .class) together with a module loading system, we are running some tests to validate 
> > that dynamic code loading/unloading works fine.
> > 
> > Strangely enough this does not work on GCJ, whereas it works fine on sun's jvm (a native 
> > library is fully unloaded once its classloader is garbaged out).
> > 
> > see also : 
> >> openJDK, in jdk/src/share/classes/java/lang/ClassLoader.java: finalize method unloads native libraries
> > 
> > Is this feature planned to be inetgrated in next versions of the runtime ?
> 
> No.  While this could be done, it would be quite tricky.
> 
> The core problem is that pointers to code (e.g. return addresses in
> the stack) aren't recognized by the garbage collector as pointers to
> the corresponding classes.  You'd have to teach the collector to
> convert code addresses to classes, and then garbage collect the
> classes.

But couldn't/shouldn't we do it for JNI native code that was loaded
through the ClassLoader when that ClassLoader is garbage collected?
I believe that is what Sylvain is testing?
We seem to never even call JNI_OnUnload on the jni libraries. Which
seems to suggest we never ever try to unload them even when they are no
longer needed.

Cheers,

Mark

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [GCJ-core] dynamically unloading native libraries
  2008-11-02 17:43   ` Mark Wielaard
@ 2008-11-03 10:46     ` Andrew Haley
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Haley @ 2008-11-03 10:46 UTC (permalink / raw)
  To: Mark Wielaard; +Cc: Sylvain Marié, java

Mark Wielaard wrote:
> 
> Responding to a pretty old email that I only saw just now for some
> reason.
> 
> On Tue, 2008-10-07 at 13:41 +0100, Andrew Haley wrote:
>> Sylvain Marié wrote:
>>> I am working on a project involving GCJ on an embedded Linux busybox. 
>>> We are particularly fond of the ability of GCJ to use java in "compiled mode".
>>>
>>> In order to be able to use both native code (.so), compiled java (.so) and bytecode java 
>>> (.jar, .class) together with a module loading system, we are running some tests to validate 
>>> that dynamic code loading/unloading works fine.
>>>
>>> Strangely enough this does not work on GCJ, whereas it works fine on sun's jvm (a native 
>>> library is fully unloaded once its classloader is garbaged out).
>>>
>>> see also : 
>>>> openJDK, in jdk/src/share/classes/java/lang/ClassLoader.java: finalize method unloads native libraries
>>> Is this feature planned to be inetgrated in next versions of the runtime ?
>> No.  While this could be done, it would be quite tricky.
>>
>> The core problem is that pointers to code (e.g. return addresses in
>> the stack) aren't recognized by the garbage collector as pointers to
>> the corresponding classes.  You'd have to teach the collector to
>> convert code addresses to classes, and then garbage collect the
>> classes.
> 
> But couldn't/shouldn't we do it for JNI native code that was loaded
> through the ClassLoader when that ClassLoader is garbage collected?

We could, but that isn't the subject of the question.  At least I didn't
think it was.

The answer to the original poster is that yes, it can be done, but it
would require some work.

> I believe that is what Sylvain is testing?

As far as I could see the question was about unloading of gcj-compiled
java code.  If the question is about JNI libraries, then yes, it still
requires some work but it should be easier.

> We seem to never even call JNI_OnUnload on the jni libraries. Which
> seems to suggest we never ever try to unload them even when they are no
> longer needed.

Sure, but making sure that in every case we can track all live references to
native code is not trivial.

Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [GCJ-core] dynamically unloading native libraries
  2008-11-05 10:45 Sylvain Marié
@ 2008-11-05 11:08 ` Andrew Haley
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Haley @ 2008-11-05 11:08 UTC (permalink / raw)
  To: Sylvain Marié; +Cc: java, mark

Sylvain Marié wrote:

> Thank you for putting the topic back on the table,
> 
>> We seem to never even call JNI_OnUnload on the jni libraries.
> 
> That's exactly my point - and also even libraries that do not contain JNI at all
> (simple .so without any relation to java - I know it is not a very useful case but it is
> very simple to test with a dummy .so).
> 
> But the root cause of the problem is a bit more complex: 
> 
> a) As you mentioned, ClassLoaders don't unload unused native libraries when they are finalized.
> 
> but also
> 
> b) ClassLoaders don't even seem to be finalized when they are not used any more.
> (no alive instances, etc.)
> 
> So even correcting a) would not help, we should first try to understand why 
> ClassLoaders are never gc'ed.

Yes.  I can't think of any immediate reason why they aren't: it's a matter
worth investigating.



Andrew.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [GCJ-core] dynamically unloading native libraries
@ 2008-11-05 10:45 Sylvain Marié
  2008-11-05 11:08 ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Sylvain Marié @ 2008-11-05 10:45 UTC (permalink / raw)
  To: java, mark; +Cc: aph

Hi Mark,

Thank you for putting the topic back on the table,

> We seem to never even call JNI_OnUnload on the jni libraries.

That's exactly my point - and also even libraries that do not contain JNI at all
(simple .so without any relation to java - I know it is not a very useful case but it is
very simple to test with a dummy .so).

But the root cause of the problem is a bit more complex: 

a) As you mentioned, ClassLoaders don't unload unused native libraries when they are finalized.

but also

b) ClassLoaders don't even seem to be finalized when they are not used any more.
(no alive instances, etc.)

So even correcting a) would not help, we should first try to understand why 
ClassLoaders are never gc'ed.


Once again, this seems to be done in openJDK (at least item a, and maybe the b):

-------------------ClassLoader.java------------------------------------
protected void finalize() {
            synchronized (loadedLibraryNames) {
                if (fromClass.getClassLoader() != null && handle != 0) {
                    /* remove the native library name */
                    int size = loadedLibraryNames.size();
                    for (int i = 0; i < size; i++) {
                        if (name.equals(loadedLibraryNames.elementAt(i))) {
                            loadedLibraryNames.removeElementAt(i);
                            break;
                        }
                    }
                    /* unload the library. */
                    ClassLoader.nativeLibraryContext.push(this);
                    try {
                        unload();
                    } finally {
                        ClassLoader.nativeLibraryContext.pop();
                    }
                }
            }
        }
----------------------------------------------------------------------------------

Kind regards,


Sylvain


> ----- Message de Mark Wielaard <mark@klomp.org> sur Sun, 02 Nov 2008
> 18:43:12 +0100 -----
> 
> Pour:
> 
> Andrew Haley <aph@redhat.com>
> 
> cc:
> 
> Sylvain Marié <sylvainmarie2@yahoo.fr>, java@gcc.gnu.org
> 
> Objet:
> 
> Re: [GCJ-core] dynamically unloading native libraries
> 
> Hi,
> 
> Responding to a pretty old email that I only saw just now for some
> reason.
> 
> On Tue, 2008-10-07 at 13:41 +0100, Andrew Haley wrote:
> > Sylvain Marié wrote:
> > > I am working on a project involving GCJ on an embedded Linux busybox. 
> > > We are particularly fond of the ability of GCJ to use java in 
> "compiled mode".
> > > 
> > > In order to be able to use both native code (.so), compiled java
> (.so) and bytecode java 
> > > (.jar, .class) together with a module loading system, we are 
> running some tests to validate 
> > > that dynamic code loading/unloading works fine.
> > > 
> > > Strangely enough this does not work on GCJ, whereas it works 
> fine on sun's jvm (a native 
> > > library is fully unloaded once its classloader is garbaged out).
> > > 
> > > see also : 
> > >> openJDK, in jdk/src/share/classes/java/lang/ClassLoader.java: 
> finalize method unloads native libraries
> > > 
> > > Is this feature planned to be inetgrated in next versions of theruntime ?
> > 
> > No.  While this could be done, it would be quite tricky.
> > 
> > The core problem is that pointers to code (e.g. return addresses in
> > the stack) aren't recognized by the garbage collector as pointers to
> > the corresponding classes.  You'd have to teach the collector to
> > convert code addresses to classes, and then garbage collect the
> > classes.
> 
> But couldn't/shouldn't we do it for JNI native code that was loaded
> through the ClassLoader when that ClassLoader is garbage collected?
> I believe that is what Sylvain is testing?
> We seem to never even call JNI_OnUnload on the jni libraries. Which
> seems to suggest we never ever try to unload them even when they are no
> longer needed.
> 
> Cheers,
> 
> Mark
> 




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-11-05 11:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-07 12:29 [GCJ-core] dynamically unloading native libraries Sylvain Marié
2008-10-07 12:42 ` Andrew Haley
2008-11-02 17:43   ` Mark Wielaard
2008-11-03 10:46     ` Andrew Haley
2008-11-05 10:45 Sylvain Marié
2008-11-05 11:08 ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).