Skip to content

Instantly share code, notes, and snippets.

@kulikala
Created May 21, 2020 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kulikala/039d7c94f46ee860f1f9e2714104cb90 to your computer and use it in GitHub Desktop.
Save kulikala/039d7c94f46ee860f1f9e2714104cb90 to your computer and use it in GitHub Desktop.
Serious canvas memory leak in Safari (both iOS / macOS)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Safari Memory Leak</title>
</head>
<body>
<div id="wrapper">
<div id="controls">
<button id="play">Play</button>
<button id="pause">Pause</button>
<label for="toggle">
<input type="checkbox" id="toggle">
Invoke memory leak
</label>
</div>
<div id="container">
<canvas id="canvas"></canvas>
</div>
</div>
<script>
const url = 'https://www.pexels.com/video/2098989/download/?search_query=&tracking_id=aace72bic2s&w=3840&h=2160'
const video = document.createElement('video')
video.setAttribute('playsinline', '')
video.setAttribute('preload', 'auto')
video.src = url
const canvas = document.getElementById('canvas')
const toggle = document.getElementById('toggle')
let requestId = 0
document.getElementById('play').addEventListener('click', async () => {
await video.play()
canvas.width = video.videoWidth / 8
canvas.height = video.videoHeight / 8
drawImage()
})
document.getElementById('pause').addEventListener('click', () => {
video.pause()
cancelAnimationFrame(requestId)
})
function drawImage () {
const ctx = canvas.getContext('2d')
if (toggle.checked) {
ctx.drawImage(
video,
video.videoWidth / 4, video.videoHeight / 4,
video.videoWidth / 2, video.videoHeight / 2,
0, 0,
canvas.width, canvas.height
)
} else {
ctx.drawImage(
video,
0, 0,
video.videoWidth, video.videoHeight,
0, 0,
canvas.width, canvas.height
)
}
requestId = requestAnimationFrame(drawImage)
}
</script>
</body>
</html>
@zhenjia-boolean
Copy link

hello ~ have you found any solution to skip this problem?

@diachedelic
Copy link

Set the canvas's width and height properties to 0 before it is garbage collected. Likewise, set the video's src property to //:0 or any other invalid URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment