alloc/index.html
2023-09-14 12:53:30 -07:00

64 lines
No EOL
2 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Massive memory allocation test</title>
</head>
<h1>
<h1>Massive memory allocation test</h1>
<p>
Allocates 16-MiB ArrayBuffers until something stops it.
May crash your system or make it sluggish.
</p>
<div id="status">Ready to start.</div>
<div><button id="start">Start</button></div>
<script async>
function el(id) {
return document.getElementById(id);
}
const status = el('status');
const button = el('start');
let running = false;
let count = 0;
// Give us 16 megs of randomized memory
// to defeat compression.
const mib = 1024 * 1024;
const chunk = 16 * mib;
const block = new Uint8Array(chunk);
for (let i = 0; i < block.length; i++) {
block[i] = Math.round(Math.random() * 255);
}
let blocks = [];
function alloc() {
blocks.push(block.slice());
count++;
status.textContent = `${count * chunk / mib} MiB allocated`;
}
let timeout = null;
function schedule() {
timeout = setTimeout(() => {
alloc();
schedule();
});
}
function cancel() {
clearTimeout(timeout);
timeout = null;
}
button.addEventListener('click', function(event) {
if (running) {
button.textContent = 'Start';
running = false;
cancel();
} else {
button.textContent = 'Stop';
running = true;
schedule();
}
});
</script>
</body>
</html>