I recently participated in the development of a project, and since the project is a browser-based fat client (RIA) application, there are a lot of iframes in the page. Later tests found that the browser memory has always been high, and the more iframe pages it opens, the greater the memory usage, which is especially obvious in IE series browsers. Even if all open iframe pages are closed, the memory usage has not decreased significantly. IE browser becomes very stuck when the memory usage reaches about 400M. Analysis found that the iframe was not released, so the memory occupied by all closed iframes was released. Although it cannot be completely released, the iframe memory usage will not continue to grow, and the memory usage of the entire application is controlled at around 150M.
/** * Dynamically create an iframe * @param dom Create an iframe container, that is, create an iframe in the dom. The dom can be a div, span or other tag. * @param src web page path opened in iframe* @param onload This event is triggered after the iframe is loaded, which can be empty* @return Return the created iframe object*/ function createIframe(dom, src, onload){ //Create iframe in the document var iframe = document.createElement("iframe"); //Set the style of iframe iframe.style.width = '100%'; iframe.style.height = '100%'; iframe.style.margin = '0'; iframe.style.padding = '0'; iframe.style.overflow = 'hidden'; iframe.style.border = 'none'; //Binding the onload event of iframe if(onload && Object.prototype.toString.call(onload) === '[object Function]'){ if(iframe.attachEvent){ iframe.attachEvent('onload', onload); }else if(iframe.addEventListener){ iframe.addEventListener('load', onload); }else{ iframe.onload = onload; } } iframe.src = src; //Load the iframe into dom.appendChild(iframe); return iframe; } /** * Destroy the iframe and release the memory occupied by the iframe. * @param iframe The iframe object that needs to be destroyed*/ function destroyIframe(iframe){ //Point the iframe to a blank page, which can free up most of the memory. iframe.src = 'about:blank'; try{ iframe.contentWindow.document.write(''); iframe.contentWindow.document.clear(); }catch(e){} //Remove iframe from the page iframe.parentNode.removeChild(iframe); }