jerryscript-project / jerryscript

Ultra-lightweight JavaScript engine for the Internet of Things.
https://jerryscript.net
Apache License 2.0
6.89k stars 669 forks source link

gc for strings in jerryscript #5006

Closed godmiaozi closed 2 years ago

godmiaozi commented 2 years ago

will string be reclaimed when gc runs in jerryscript. how string will be managed when it is no longer needed. for example

function PrintHello() 
{
     var hello = "hello"
}
function main()
{
    PrintHello();
}

when PrintHello call in main finished, will "hello" be reclaimed if we do not have enough memory for new objects allocation

rerobika commented 2 years ago

Hi @godmiaozi!

Defining no longer is needed is not that simple. First of all, "hello" string is not allocated in each invocation of the PrintHello function, it's stored in the function's literal pool as a constant. The parser uses the same string table for every function to prevent duplicated existence of static string constants all the string are alive until the engine is not terminated. So summarize, reclaiming the memory for static strings are not supported by the engine, however the dynamically allocated strings like "foo" + "bar" gets garbage collected when they are no longer needed of course.