Subj : Re: Memory growth problem To : Mike McIntosh From : Brendan Eich Date : Mon May 19 2003 10:30 am Mike McIntosh wrote: > Hi Brendan, > > Thanks for your response(s). > > I've noticed a fundamental flaw in how I was trying to detect this. > > First, if I run a really simple script which just reads my ClientRequest > property and sets a ClientResponse I see the following calls to JS_malloc. > > Alloc of 24 bytes -- JS_NewObject --> These are freed via JS_free > Alloc of 88 bytes -- js_NewScope --> on subsequent execution > Alloc of 482 bytes -- ClientRequest --> This is released via > jstr/js_FinalizeString > > These first 2 objects are not released after first transaction but I _do_ > see them being released on the subsequent transaction. So my object dump > trace showing lots of allocated objects is incorrect. This is really showing > memory which has been allocated by the engine but which _will_ be released > during the next GC. Would that be correct?. Yes, and I should have pointed out that you could keep each scope object around, if you're clearing it after each request. No need to clear an object that's about to become garbage anyway, then make a new one for the next request. Just reuse the same object after clearing it. > Anyway, for the simplest script I can see all memory being released if I > watch JS_Alloc/JS_Free in the debugger. I also see the finalize being called > on the ClientRequest string. > > I _was_ talking about GetStringBytes and here is my code to set the client > response :- > > static JSBool gbl_SetClientResponse(JSContext *cx, JSObject *obj, uintN > argc, jsval *argv, jsval *rval) > { > TRY() > > *rval = BOOLEAN_TO_JSVAL(FALSE); > > if ((argc == 1) && JSVAL_IS_STRING(argv[0])) > { > //////////////////////////////// > // Access the context info... > // > privData* pd = (privData*)JS_GetContextPrivate(cx); > if (pd && (pd->m_hctx != 0)) > { > ////////////////////////////// > // Get the response argument > // > JSString* jszResponse = JS_ValueToString(cx, argv[0]); > if (jszResponse) > { > argv[0] = STRING_TO_JSVAL(jszResponse); > char * pszBytes = JS_GetStringBytes(jszResponse); > if (pszBytes) > { > // This sends the response string back to _our_ client > BOOL bVal = XxeSetClientResponse(pd->m_hctx, pszBytes, > strlen(pszBytes)); > if (bVal) > { > *rval = BOOLEAN_TO_JSVAL(bVal); > } > } > } > } > } > CATCH_JS() > > return JS_TRUE; > } > > I can see calls to js_PurgeDeflatedStringCache which seems to release the > strings allocated by JS_GetStringBytes. Do you want to chop jschar down to char, given Unicode and i18n concerns? Maybe you'd rather transcode the jszResponse (it's not necessarily zero-terminated, btw [as the z implies?] -- it may have NUL jschars embedded in it) into UTF-8 or something that preserves any high non-zero bytes in the jschars? > In order to get somewhere with the more complicated case I'll try to use the > trace-malloc tool (is it only available for Linux as the README states?.) It works on Windows too, now (README needs to be updated). Feel free to ping me about it if you have any questions. /be .