Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
jni fwrite memroy issue

861183
Member Posts: 1
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);
.............
}
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
-
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. -
java codeIf 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.
-
jschell wrote:Yes, do that. The snippets provided are pretty useless.java codeIf you think code is going to help then you need to post the actual code.
>Bingo.Could somebody tell me the reason for this?You are allocating something and then not freeing it. In your C code.
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.