[PATCH 3/5] [mamajni] Improve java inbox memory usage


Ian Bell <IBell@...>
 

From 38a3585333d41159f3e7902bd493b9c6fdfcc04f Mon Sep 17 00:00:00 2001

Message-Id: <38a3585333d41159f3e7902bd493b9c6fdfcc04f.1348836736.git.ibell@...>

In-Reply-To: <96f0ea876c43e27f628c68dca155ce2f5b82fa09.1348836736.git.ibell@...>

References: <96f0ea876c43e27f628c68dca155ce2f5b82fa09.1348836736.git.ibell@...>

From: Ian Bell <ibell@...>

Date: Fri, 28 Sep 2012 13:05:09 +0100

Subject: [PATCH 3/5] [mamajni] Improve java inbox memory usage

 

Improved memory usage when creating and destroying inbox's

in a tight loop.

 

Signed-off-by: Ian Bell <ibell@...>

---

mama/jni/src/c/mamainboxjni.c               |   15 ++++-----------

mama/jni/src/c/mamaqueuejni.c               |    7 ++++++-

mama/jni/src/com/wombat/mama/MamaInbox.java |    5 +++++

mama/jni/src/com/wombat/mama/MamaQueue.java |    5 ++++-

4 files changed, 19 insertions(+), 13 deletions(-)

 

diff --git a/mama/jni/src/c/mamainboxjni.c b/mama/jni/src/c/mamainboxjni.c

index 1aa3874..f809611 100644

--- a/mama/jni/src/c/mamainboxjni.c

+++ b/mama/jni/src/c/mamainboxjni.c

@@ -54,6 +54,7 @@ jmethodID   callbackOnDestroyId_g                =  NULL;

/* Needed during create() */

extern jfieldID    transportPointerFieldId_g;

extern jfieldID    queuePointerFieldId_g;

+extern jfieldID    queuePointerMsg_g;

extern jmethodID   messageConstructorId_g;

extern jfieldID    messagePointerFieldId_g;

@@ -97,6 +98,7 @@ JNIEXPORT void JNICALL Java_com_wombat_mama_MamaInbox_create

    

     assert(NULL!=transport);

     assert(NULL!=callback);

+    assert(NULL!=queue);

    

     closureImpl->mInbox = (*env)->NewGlobalRef(env,this);

     closureImpl->mJavaCallback = (*env)->NewGlobalRef(env,callback);   

@@ -106,18 +108,14 @@ JNIEXPORT void JNICALL Java_com_wombat_mama_MamaInbox_create

     MAMA_THROW_NULL_PARAMETER_RETURN_VOID(transportPointer, 

                                "Null parameter, MamaTransport may have already been destroyed.")

-    if(queue)/*The queue is optional*/

-    {

         queuePointer = (*env)->GetLongField(env,queue,queuePointerFieldId_g);

         MAMA_THROW_NULL_PARAMETER_RETURN_VOID(queuePointer, 

                                                "Null parameter, MamaQueue may have already been destroyed.")

         cQueue = CAST_JLONG_TO_POINTER(mamaQueue, queuePointer);

-    }

-    /*Create the reuseable Java message object*/

-    messageImpl = utils_createJavaMamaMsg(env);

-    if(NULL==messageImpl) return;/*Exception will have been thrown*/

+    /* Use the reuseable message object from our queue*/

+    messageImpl =  (*env)->GetObjectField(env,queue,queuePointerMsg_g);

     closureImpl->mReuseableMsgObject = (*env)->NewGlobalRef(env,messageImpl);

@@ -243,11 +241,6 @@ void MAMACALLTYPE destroyCB(mamaInbox inbox, void *closure)

             /* Destroy the global refs - allows objects to be garbage collected */

             if(NULL != closureImpl->mReuseableMsgObject)

             {

-                /* Clear the message field pointer in the re-usable message, it will then be destroyed by the garbage collector

-                 * when the global ref is deleted below, note that this object is only accessible to the JNI layer.

-                 */

-                (*env)->SetLongField(env, closureImpl->mReuseableMsgObject, messagePointerFieldId_g, 0);

-

                 /* Delete the global ref. */

                 (*env)->DeleteGlobalRef(env, closureImpl->mReuseableMsgObject);

             }

diff --git a/mama/jni/src/c/mamaqueuejni.c b/mama/jni/src/c/mamaqueuejni.c

index f5ea493..cd75b5d 100644

--- a/mama/jni/src/c/mamaqueuejni.c

+++ b/mama/jni/src/c/mamaqueuejni.c

@@ -71,6 +71,7 @@ extern  JavaVM*     javaVM_g;

 /*Global as also needed in mamadispatcherjni.c*/

jfieldID    queuePointerFieldId_g   =   NULL;

+jfieldID    queuePointerMsg_g   =   NULL;

 /*Global as also needed in mamajni.c*/

jmethodID   queueConstructorId_g    =   NULL;

@@ -747,7 +748,11 @@ JNIEXPORT void JNICALL Java_com_wombat_mama_MamaQueue_initIDs

     monitorClosureFieldId_g = (*env)->GetFieldID(env,

                                 class,"monitorClosure_i",

                                 UTILS_JAVA_POINTER_TYPE_SIGNATURE);

-    /*Get a reference to the subscription Callback class*/

+    queuePointerMsg_g = (*env)->GetFieldID(env,

+                                class,"reuseableMsg",

+                                "Lcom/wombat/mama/MamaMsg;");

+   

+/*Get a reference to the subscription Callback class*/

     queueMonitorCallbackClass = (*env)->FindClass(env,

             "com/wombat/mama/MamaQueueMonitorCallback");

    

diff --git a/mama/jni/src/com/wombat/mama/MamaInbox.java b/mama/jni/src/com/wombat/mama/MamaInbox.java

index 9db44ae..003d72c 100644

--- a/mama/jni/src/com/wombat/mama/MamaInbox.java

+++ b/mama/jni/src/com/wombat/mama/MamaInbox.java

@@ -64,6 +64,11 @@ public class MamaInbox

         _destroy();

     }

+    protected void finalize() throws Throwable {

+        if(0!=inboxPointer_i)

+            _destroy();

+    }

+

     private native void _destroy();

     private static native void initIDs();

diff --git a/mama/jni/src/com/wombat/mama/MamaQueue.java b/mama/jni/src/com/wombat/mama/MamaQueue.java

index c38de5e..e611658 100644

--- a/mama/jni/src/com/wombat/mama/MamaQueue.java

+++ b/mama/jni/src/com/wombat/mama/MamaQueue.java

@@ -39,12 +39,15 @@ public class MamaQueue

     /*Used to store pointer to the C closure used for queue monitoring*/

     private long monitorClosure_i  = 0;

+             public MamaMsg  reuseableMsg = null;

     public long getPointerVal()

     {

         return queuePointer_i;

     }

    

-    public MamaQueue () {};

+    public MamaQueue () {

+                             reuseableMsg = new MamaMsg(false);

+             };

     public native void create (MamaBridge bridge);

--

1.7.9.5

 




Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.