Subj : Re: Memory management of contexts etc. To : netscape.public.mozilla.jseng From : Heath Raftery Date : Mon Oct 06 2003 11:52 pm Brendan Eich wrote: > First, why the asymmetry between compile (which creates both a > runtime and a context in it) and main, which destroys the runtime? > See js.c for a balanced example (create rt; create cx; do stuff; > destroy cx; destroy rt). Good question. Partly due to concerns about having the best resource usage across threads, and partly because it was hacked together. I took your advice and had a few goes. At first I simply create rt; create cx; compile function; fork; execute function in threads; delete cx; delete rt;. But I'm pretty sure this was dangerous/limiting in respect to the threads, so in the end, I restricted the cx's to the threads. Now it looks more like this: rt = JS_NewRuntime(1L * 1024L * 1024L); JSContext *cx = JS_NewContext(rt, 4L * 1024L); global = JS_NewObject(cx, &global_class, NULL, NULL); JS_InitStandardClasses(cx, global); JSFunction *compiledPAC = compilePAC(cx, arg1); for (;;) //enter an endless loop, handling requests { switch(fork()) //spawn a new process to handle the request { case 0: //the child JSContext *threadcx = JS_NewContext(rt, 4L * 1024L))) result = executePAC(threadcx, compiledPAC, requestURL, requestHost))) JS_DestroyContext(threadcx); } } JS_DestroyContext(cx); JS_DestroyRuntime(rt); Things seem to be well and dandy with this method, but should I still be concerned about my use of a seperate thread to execute the script I compiled earlier? It all seems to work, and each thread appears to be happy running in its own context. Thank you so much for your help, I'm very happy to have come this far! -- Heath ________________________________________________________ | *Nothing is foolproof to a sufficiently talented fool* | | _\|/_ | |________________________________________m(. .)m_________| .