Search code examples
java.netjava-native-interfaceout-of-memory

Out of Memory Exception when accessing Java Hashmap from JNI


HI. I write a program using jni.net bridge. jni.net bridge is c# wraper of JNI. Problem: i create object of hash map from c# using jni.net bridge. when i try to call put(object,object) method of hash map OutOFMemory exception occurs; "insufficient memory to continue program execution" i also catch exception and when i check is put operation was successful or not i and call get(key) method i come to know than key was entered successfully.

This error only comes when try to put new key value pair, if i want to update previous key value no error ....

need ur expert opinions. Code Is given below

This is the code

using System;
using System.Collections.Generic;
using System.Text;
using Jni.Net.Bridge;


namespace JNINetHashTable
{
    class HashTable
    {

        readonly static JClass hastTableClass;
        readonly static JClass employeeClass;

        readonly static JConstructor empconstr;
        readonly static JMethod getName;
        readonly static JMethod setName;
        readonly static JObject classObject;
        readonly static JConstructor constr;
        readonly static JMethod put;
        readonly static JMethod get;
        readonly static JMethod size;
        readonly static JMethod remove;
        readonly static JMethod clear;

        readonly static JMethod getNumber;
        readonly static JMethod setNumber;
        static JObject empObj;
        static HashTable()
        {

            employeeClass = JClass.ForName("Employee");
            Console.WriteLine("Check  Point1...!");
            empconstr = employeeClass.GetConstructor("()V");
            empObj = employeeClass.NewInstance();
            setName = employeeClass.GetMethod("setName", "(Ljava/lang/String;)V");
            getName = employeeClass.GetMethod("getName", "()Ljava/lang/String;");
            getNumber = employeeClass.GetMethod("getNumber", "()I");
            setNumber = employeeClass.GetMethod("setNumber", "(I)V");


            hastTableClass = JClass.ForName("java/util/HashMap");

            constr = hastTableClass.GetConstructor("()V");
            classObject = hastTableClass.NewInstance();


            put = hastTableClass.GetMethod("put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
            get=hastTableClass.GetMethod("get","(Ljava/lang/Object;)Ljava/lang/Object;");
            size = hastTableClass.GetMethod("size", "()I");
            remove = hastTableClass.GetMethod("remove", "(Ljava/lang/Object;)Ljava/lang/Object;");
            clear = hastTableClass.GetMethod("clear", "()V");
        }
        public  void addItem(JString key,int value)
        {

            empObj = employeeClass.NewInstance();
            setName.CallVoid(empObj,key);
            setNumber.CallVoid(empObj,value);
            int a = getNumber.CallInt(empObj);

                JObject obj = put.CallObject(classObject, key, empObj);

            Console.WriteLine("Writing Time " + a);

        }
        public  string getItem(JString key)
        {
           empObj  = get.CallObject(classObject,key);
           int a = getNumber.CallInt(empObj);
           JObject str = getName.CallObject(empObj);
           string name = JString.getString(str);
           return  name+" : "+a;
        }
        public int hashsize()
        {
            int a=size.CallInt(classObject);
            return a;
        }
        public bool removeKey(JString key)
        {
            JObject jo = remove.CallObject(classObject, key);
            if (jo != null)
                return true;
            else return false;

        }
        public void clearAll()
        {
            clear.CallVoid(classObject);
        }
    }

}

JClass is the class of c# which wraps the jclass of jni as per implementation of the JNI.net.bridge is there any other question to make it more clear...?


Solution

  • It is solved, issues was in callobject method of JMethod Class.

    if(r==IntPtr.Zero) return null; JThrowable.CheckAndThrow (); return new JObject(r);

    Becasue jobject constructor throws an exception if ptr is null;