使用html来模拟测试浏览器能分配的最大内存

2023-08-15  本文已影响0人  ShootHzj

测试html如下,观察崩溃前的页面或观测console.log

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Memory Crash Test</title>
</head>
<body>
    <div id="memoryInfo">Allocated 0 MB of memory</div>
    
    <script>
        const arrayOfBuffers = [];
        const memoryInfoDiv = document.getElementById('memoryInfo');
        
        function allocateMemory() {
            try {
                const buffer = new ArrayBuffer(10 * 1024 * 1024);
                arrayOfBuffers.push(buffer);
                
                const memoryMessage = `Allocated ${arrayOfBuffers.length * 10} MB of memory`;
                
                console.log(memoryMessage);
                
                memoryInfoDiv.textContent = memoryMessage;
                
                setTimeout(allocateMemory, 100);
            } catch (error) {
                const errorMessage = `Failed to allocate more memory after ${arrayOfBuffers.length * 10} MB`;
                
                console.error(errorMessage, error);
                
                memoryInfoDiv.textContent = errorMessage;
            }
        }
        
        allocateMemory();
    </script>
</body>
</html>

上一篇 下一篇

猜你喜欢

热点阅读