Skip to content

Questions about Realm lifecycle, cleanup behavior, and best practices for reducing initialization overhead #5276

@Sab1e-dev

Description

@Sab1e-dev

Hi JerryScript maintainers,

I am currently developing a script-based application system on top of JerryScript, and I would like to ask for clarification regarding Realm lifecycle management and whether my current design approach is appropriate.

Background

In my system, each application registers a large number of native functions.
If I initialize the JS environment and register all native functions at application startup, this causes a noticeable CPU stall, which negatively impacts the user experience in my GUI system.

To address this, I started using Realms. My current approach is:

Register all required native functions once on a shared/global object in advance.

Use Realms to isolate applications.

When launching an application, simply attach or reference the already-registered function objects into the target Realm.

This significantly reduces startup time, and the Realm-based isolation works very well for my use case.

Issue / Confusion

However, I noticed something unexpected regarding cleanup behavior:

When variables or functions are created inside a Realm (from JavaScript code), freeing the Realm using jerry_value_free() does not seem to trigger cleanup of those Realm-local objects. In particular, native cleanup callbacks (native_info->free_cb) are not invoked.

This leads me to the following questions.

Questions

Is a JerryScript Realm expected to be released using jerry_value_free()?

When a Realm is released or cleaned up, is it supposed to fully free all variables, functions, and objects created within that Realm?

Is it expected behavior that Realm cleanup does not trigger GC-based cleanup (including native free_cb callbacks)?

If so, what is the recommended way to properly manage native resource lifetimes when using multiple Realms?

Additional Question

Is there a more recommended or idiomatic approach in JerryScript to reduce native function registration overhead at application startup, especially in GUI or embedded systems where startup latency is critical?

Any clarification or guidance would be greatly appreciated.
Thank you for your work on JerryScript.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions