Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/browser/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ pub const Browser = struct {
pub fn init(app: *App) !Browser {
const allocator = app.allocator;

const env = try Env.init(allocator, .{
.gc_hints = app.config.gc_hints,
});
const env = try Env.init(allocator, .{});
errdefer env.deinit();

const notification = try Notification.init(allocator, app.notification);
Expand Down Expand Up @@ -98,6 +96,9 @@ pub const Browser = struct {
if (self.session) |*session| {
session.deinit();
self.session = null;
if (self.app.config.gc_hints) {
self.env.lowMemoryNotification();
}
}
}

Expand Down
34 changes: 14 additions & 20 deletions src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,13 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
// index.
prototype_lookup: [Types.len]u16,

// Send a lowMemoryNotification whenever we stop an executor
gc_hints: bool,

const Self = @This();

const TYPE_LOOKUP = TypeLookup{};

const Opts = struct {
gc_hints: bool = false,
};
const Opts = struct {};

pub fn init(allocator: Allocator, opts: Opts) !*Self {
pub fn init(allocator: Allocator, _: Opts) !*Self {
// var params = v8.initCreateParams();
var params = try allocator.create(v8.CreateParams);
errdefer allocator.destroy(params);
Expand Down Expand Up @@ -213,7 +208,6 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
.templates = undefined,
.allocator = allocator,
.isolate_params = params,
.gc_hints = opts.gc_hints,
.prototype_lookup = undefined,
};

Expand Down Expand Up @@ -274,6 +268,18 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
};
}

// V8 doesn't immediately free memory associated with
// a Context, it's managed by the garbage collector. So, when the
// `gc_hints` option is enabled, we'll use the `lowMemoryNotification`
// call on the isolate to encourage v8 to free any contexts which
// have been freed.
pub fn lowMemoryNotification(self: *Self) void {
var handle_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&handle_scope, self.isolate);
defer handle_scope.deinit();
self.isolate.lowMemoryNotification();
}

pub const Executor = struct {
env: *Self,

Expand Down Expand Up @@ -304,18 +310,6 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
self.endScope();
}

// V8 doesn't immediately free memory associated with
// a Context, it's managed by the garbage collector. So, when the
// `gc_hints` option is enabled, we'll use the `lowMemoryNotification`
// call on the isolate to encourage v8 to free any contexts which
// have been freed.
if (self.env.gc_hints) {
var handle_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&handle_scope, self.env.isolate);
defer handle_scope.deinit();

self.env.isolate.lowMemoryNotification(); // TODO we only need to call this for the main World Executor
}
self.call_arena.deinit();
self.scope_arena.deinit();
}
Expand Down