Eric98
asked on
JNI from Servlet / free memory inside C routine
I use JNI to call a native C routine from a Servlet.
My routine is shaped like this :
JNIblahblah (JNIEnv *env, jobject obj)
{
...
char *buffer_out;
...
buffer_out = (char *)calloc(255,1);
...
return(*env)->NewStringUTF (env, buffer_out);
}
My question is : as this routine will be called very often (concurrent acces), is it MANDATORY to free 'buffer_out'
before leaving the routine ? (please explain)
Thanks for any info
My routine is shaped like this :
JNIblahblah (JNIEnv *env, jobject obj)
{
...
char *buffer_out;
...
buffer_out = (char *)calloc(255,1);
...
return(*env)->NewStringUTF
}
My question is : as this routine will be called very often (concurrent acces), is it MANDATORY to free 'buffer_out'
before leaving the routine ? (please explain)
Thanks for any info
ASKER
Actually my servlet is executed only once, and so would be the finalize.
But each time the servlet is requested, a new JNI call will be instantiated : it is the JNI call that happens very often.
So the meaning of my question is : do I have to free the ressources allocated by the C JNI call before leaving
the C, or will this be done on an automatic basis, in the same way as the ressources of a regular C program, once completed, are freed by the Operating system.
But each time the servlet is requested, a new JNI call will be instantiated : it is the JNI call that happens very often.
So the meaning of my question is : do I have to free the ressources allocated by the C JNI call before leaving
the C, or will this be done on an automatic basis, in the same way as the ressources of a regular C program, once completed, are freed by the Operating system.
>>>>>
Do I have to free theressources allocated by the C JNI call before leaving the C, or will this be done on an automatic basis,
you have to free it by urself ,u can use System.runFinalizersOnExit (true); for true blue Applications
Oops I forgot b4 but the same functionality that finalize() provides in applications s provided in servlets in destroy() so have it there but mostly the servlet class file si goingt o be in Memory so better call that free routine by urself.
Do it else u have a 'leak'
Do I have to free theressources allocated by the C JNI call before leaving the C, or will this be done on an automatic basis,
you have to free it by urself ,u can use System.runFinalizersOnExit
Oops I forgot b4 but the same functionality that finalize() provides in applications s provided in servlets in destroy() so have it there but mostly the servlet class file si goingt o be in Memory so better call that free routine by urself.
Do it else u have a 'leak'
ASKER
Thanks for your help.
Please make a response so I can give you the points.
And please tell me what true blue application means?
Please make a response so I can give you the points.
And please tell me what true blue application means?
ASKER
Thanks for your help.
Please make a response so I can give you the points.
And please tell me what true blue application means?
Please make a response so I can give you the points.
And please tell me what true blue application means?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It would seem that finalize( ) is in place because of the possibility that you’ll do something C-like by allocating memory using a mechanism other than the normal one in Java. This can happen primarily through native methods , which are a way to call non-Java code from Java. (Native methods are discussed in Appendix A.) C and C++ are the only languages currently supported by native methods, but since they can call subprograms in other languages, you can effectively call anything. Inside the non-Java code, C’s malloc( ) family of functions might be called to allocate storage, and unless you call free( ) that storage will not be released, causing a memory leak. Of course, free( ) is a C and C++ function, so you’d need call it in a native method inside your finalize( ).
It seems that since u r doing this many times,call a Native Method which is passed this returned pointer and whose only job is to free(),i.e u shift finalize() code to this method called say
void freeResources();