This discussion is archived
3 Replies Latest reply: May 11, 2011 1:47 PM by 800381 RSS

jni fwrite memroy issue

861183 Newbie
Currently Being Moderated
I am trying to call the c function from java using jni. From java function I am passing 64 K as byte array. From the C program I am writing it to a file appending. The getNativeHeapFreeSize is getting decreased after each invokeSVM function call. And and the program throws Out of memory error. Could somebody tell me the reason for this?

java code

     synchronized(res)
     {
//          byte[] copyBuf = new byte[640*480*2];//[args[0].length];
//          System.arraycopy(args[0], 0, copyBuf, 0, 640*480*2);
          
          int ret = invokeSVM(args[0], res);
          //----
          Log.d(TAG, "getNativeHeapSize = " + String.valueOf(Debug.getNativeHeapSize()));
          Log.d(TAG, "getNativeHeapFreeSize = " + String.valueOf(Debug.getNativeHeapFreeSize()));
          Log.d(TAG, "getNativeHeapAllocatedSize = " + String.valueOf(Debug.getNativeHeapAllocatedSize()));
          //----
          Log.d(TAG, "XXXXXXXXXXXXXXXXXXXXXX Value: " + String.valueOf(ret) + "xxxxxxxxxxx");
               
          if(ret == 0)
          {
               Log.e(TAG, "HHHHHHHHH SVM Algorithm not success HHHHHHHH");
          }
     }
               return res;               
          }


c code
function invokeSVM ()
{
...........
          fp = fopen ("/sdcard/image_after.yuv","a+");
          fwrite (outArray, 1, 640*480*3, fp);
          fclose (fp);
.............
}
  • 1. Re: jni fwrite memroy issue
    802316 Pro
    Currently Being Moderated
    I am surprised you are just getting an OutOfMemoryError because are passing a 64K byte buffer you are trying to write 640*480*3 bytes = 900 KB from it. This means you are reading a random area of memory.

    The logging messages will be using memory, so every time you execute this block I would expect the free memory to decrease.
  • 2. Re: jni fwrite memroy issue
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    java code
    If you think code is going to help then you need to post the actual code.
    Could somebody tell me the reason for this?
    You are allocating something and then not freeing it. In your C code.
  • 3. Re: jni fwrite memroy issue
    800381 Explorer
    Currently Being Moderated
    jschell wrote:
    java code
    If you think code is going to help then you need to post the actual code.
    Yes, do that. The snippets provided are pretty useless.
    >
    Could somebody tell me the reason for this?
    You are allocating something and then not freeing it. In your C code.
    Bingo.

    Probably calling malloc(), new, or something similar to get a buffer for GetByteArrayRegion(), then not freeing the buffer. Although I suppose it's possible that he's not calling DeleteLocalRef() on the passed-in byte buffer and that's preventing it from being GC'd. I've never really tried to figure out how that works or doesn't work.

    And if all you're going to do is do one big write, don't use fopen/fwrite/fclose in append mode, just use open/lseek/write/close:
    int fd = open( filename, O_WRONLY );
    lseek( fd, 0, SEEK_END );
    write( fd, buffer, bytes );
    close( fd );
    You don't want nor need the buffering that comes with fopen() et al when that's all you're doing.

    And FWIW, you REALLY need to check return values from all of those, except maybe close() (what are you going to do if you can't close() it? Nothing, but maybe it's nice to know.)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points