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
Eric98Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbormannCommented:
from TiJ

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();
0
Eric98Author Commented:
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.
0
mbormannCommented:
>>>>>
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'
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Eric98Author Commented:
Thanks for your help.
Please make a response so I can give you the points.
And please tell me what true blue application means?
0
Eric98Author Commented:
Thanks for your help.
Please make a response so I can give you the points.
And please tell me what true blue application means?
0
mbormannCommented:
thanks Eric,

Move code  in urWebServerDocRoot\servlet\*.class to someplace else.this is explained in Jason's book,faster loading of servlets.

Check out cool sites
http://www.servlets.com/
http://www.purpletech.com/servlet-faq/
http://www.j-nine.com/pubs/applet2servlet/Applet2Servlet.html
http://www.servletsource.com/code.html

I would suggest buy
Java Servlet Programming
Published by O'Reilly
by
Jason Hunter

i learnt many good things in this good book

also true blue appl r those having main()
feel free to ask any questions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.