jvm memory leak

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sendil kumar
    New Member
    • Sep 2007
    • 26

    #1

    jvm memory leak

    Hi All,

    My application calls some java functionalities using C++. While running, my application crashes after 1 hour. The reason it saya is "Java: Heap memory exhausted", it couldn't allocate heap memory for further operations as it was exhausted. I think, java uses garbage collector to release memory when ever the objects goes out of scope. I even tried to catch the exception,but, It was not caught. Could any one give me solutions to catch the exception or to free up the memory which the java holds up?
  • vipersniper5
    New Member
    • Dec 2007
    • 9

    #2
    How are you connecting to java from C++? JNI?The garbage collector will collect garbage when there is no longer a pointer to that object in your program. Check to make sure you are not retaining references anywhere (such as Vectors, Arrays, ect). Also if you are using JNI, as I understand it anything used in JNI call will not be dealt with by the garbage collector, and must be freed in C, I could be wrong on the specifics of this, but nothing allocated in C will be dealt with. If you are using something other than JNI, and are not retaining references, I expect the leak is coming from your C++ code, and java just happens to be what tries to allocate memory when it runs out. I've found valgrind helpful for finding memory leaks in C.

    Comment

    • kevinbeams
      New Member
      • Jan 2008
      • 1

      #3
      I too am experiencing a similar situation where I’m invoking the JNI interface from C++.

      From C++ I’m invoking via (parts of the code renamed for simplicity):


      JavaVMInitArgs vm_args;
      JavaVMOption vm_options[3];
      vm_options[0].optionString = "-Xms512M";
      vm_options[1].optionString = "-Xmx1024M";
      char* class_path_opti on = "-Djava.class.pat h="jar1.jar;jar 2.jar;jar3;jar4 .jar");
      vm_options[2].optionString = class_path_opti on;
      vm_args.version = JNI_VERSION_1_4 ;
      vm_args.options = vm_options;
      vm_args.nOption s = 3;
      vm_args.ignoreU nrecognized = JNI_FALSE;
      jint create_vm_resul t = JNI_CreateJavaV M(&jvm, (void**)&env, &vm_args);

      Then the class and method id are initialized:

      javaClass = env->FindClass("c om/wf/tiff/TiffValidator") ;
      javaMethodID = env->GetStaticMetho dID(javaClass , "validateMetWel lsFargoMinimumS tandard", "([B[B)Z");

      From there the call is made (loop) and though the inbound parameter array is “released” via the JNI interface spec, memory usage grows until and out of memory error occurs.

      {loop called from outer method}
      jint jFrontSize = (jint)frontImag eSize;
      jint jBackSize = (jint)backImage Size;
      jbyteArray jFrontBuffer = env->NewByteArray(j FrontSize);
      jbyteArray jBackBuffer = env->NewByteArray(j BackSize);
      jbyte* frontImagePtr = (jbyte*)frontIm age;
      jbyte* backImagePtr = (jbyte*)backIma ge;
      env->SetByteArrayRe gion(jFrontBuff er, 0, jFrontSize, frontImagePtr);
      env->SetByteArrayRe gion(jBackBuffe r, 0, jBackSize, backImagePtr);
      jboolean result = (jboolean) env->CallStaticObje ctMethod(javaCl ass, javaMethodID, jFrontBuffer, jBackBuffer);
      env->ReleaseByteArr ayElements(jFro ntBuffer, frontImagePtr, 0);
      env->ReleaseByteArr ayElements(jBac kBuffer, backImagePtr, 0);

      1) Is this similar to what your are seeing?
      2) Any thoughts on how this may be resolved?

      Thanks for your input!!

      Comment

      Working...