Skip to content

Commit

Permalink
Add nil to references to mark as ready to be GC'd
Browse files Browse the repository at this point in the history
This is a POC to understand why memory isn't being deallocated normally
without having to do this.
  • Loading branch information
ankur22 committed Oct 12, 2024
1 parent 00bca99 commit 88572e5
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 0 deletions.
13 changes: 13 additions & 0 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ func (b *Browser) initEvents() error { //nolint:cyclop
if b.vuCtxCancelFn != nil {
b.vuCtxCancelFn()
}

go func() {
<-b.browserCtx.Done()

b.browserProc = nil
b.browserOpts = nil
b.conn = nil
b.context = nil
b.defaultContext = nil
b.pages = nil
b.sessionIDtoTargetID = nil
b.logger = nil
}()
}()
for {
select {
Expand Down
11 changes: 11 additions & 0 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ func NewBrowserContext(
return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err)
}

go func() {
<-b.ctx.Done()

b.browser = nil
b.opts = nil
b.timeoutSettings = nil
b.logger = nil
b.vu = nil
b.evaluateOnNewDocumentSources = nil
}()

return &b, nil
}

Expand Down
11 changes: 11 additions & 0 deletions common/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ func (c *Connection) close(code int) error {
// Stop the main control loop
close(c.done)
_ = c.conn.Close()

go func() {
<-c.ctx.Done()

c.cancelCtx = nil
c.logger = nil
c.conn = nil
c.msgIDGen = nil
c.sessions = nil
c.onTargetAttachedToTarget = nil
}()
}()

c.closeAllSessions()
Expand Down
13 changes: 13 additions & 0 deletions common/frame_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ func NewFrameManager(
id: atomic.AddInt64(&frameManagerID, 1),
}

go func() {
<-m.ctx.Done()

m.session = nil
m.page = nil
m.timeoutSettings = nil
m.mainFrame = nil
m.frames = nil
m.barriers = nil
m.vu = nil
m.logger = nil
}()

m.logger.Debugf("FrameManager:New", "fmid:%d", m.ID())

return m
Expand Down
18 changes: 18 additions & 0 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ func (fs *FrameSession) initEvents() {
}

go func() {
go func() {
<-fs.ctx.Done()

fs.session = nil
fs.page = nil
fs.parent = nil
fs.manager = nil
fs.networkManager = nil
fs.k6Metrics = nil
fs.contextIDToContext = nil
fs.isolatedWorlds = nil
fs.eventCh = nil
fs.childSessions = nil
fs.vu = nil
fs.logger = nil
fs.mainFrameSpan = nil
}()

fs.logger.Debugf("NewFrameSession:initEvents:go",
"sid:%v tid:%v", fs.session.ID(), fs.targetID)
defer func() {
Expand Down
17 changes: 17 additions & 0 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,23 @@ func (m *NetworkManager) initEvents() {
}

func (m *NetworkManager) handleEvents(in <-chan Event) bool {
go func() {
<-m.ctx.Done()

m.logger = nil
m.session = nil
m.parent = nil
m.frameManager = nil
m.credentials = nil
m.resolver = nil
m.vu = nil
m.customMetrics = nil
m.mi = nil
m.reqIDToRequest = nil
m.attemptedAuth = nil
m.extraHTTPHeaders = nil
}()

select {
case <-m.ctx.Done():
return false
Expand Down
23 changes: 23 additions & 0 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,29 @@ func (p *Page) initEvents() {
defer func() {
p.logger.Debugf("Page:initEvents:go:return",
"sid:%v tid:%v", p.session.ID(), p.targetID)

go func() {
<-p.ctx.Done()

p.Keyboard = nil
p.Mouse = nil
p.Touchscreen = nil
p.session = nil
p.browserCtx = nil
p.opener = nil
p.frameManager = nil
p.timeoutSettings = nil
p.emulatedSize = nil
p.extraHTTPHeaders = nil
p.eventCh = nil
p.eventHandlers = nil
p.mainFrameSession = nil
p.frameSessions = nil
p.workers = nil
p.routes = nil
p.vu = nil
p.logger = nil
}()
}()

for {
Expand Down
5 changes: 5 additions & 0 deletions common/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (s *Session) close() {
s.closed = true

s.emit(EventSessionClosed, nil)

go func() {
<-s.ctx.Done()
s.conn = nil
}()
}

func (s *Session) markAsCrashed() {
Expand Down

0 comments on commit 88572e5

Please sign in to comment.