Forum Stats

  • 3,838,750 Users
  • 2,262,397 Discussions
  • 7,900,750 Comments

Discussions

jni fwrite memroy issue

861183
861183 Member Posts: 1
edited May 11, 2011 4:47PM in Java Native Interface (JNI)
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);
.............
}

Answers

  • 802316
    802316 Member Posts: 532
    edited May 11, 2011 11:23AM
    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.
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    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.
  • 800381
    800381 Member Posts: 486
    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.)
This discussion has been closed.