`
wj98127
  • 浏览: 268181 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

Dynamic script generation and memory leaks

    博客分类:
  • Ajax
阅读更多

An interesting piece by Neil Fraser shows that using JSON-P with generated script nodes can be quite a memory leak. Normally you'd add information returned from an API in JSON-P with a generated script node:

JAVASCRIPT: 
script = document.createElement('script');
script.src = 'http://example.com/cgi-bin/jsonp?q=What+is+the+meaning+of+life%3F';
script.id = 'JSONP';
script.type = 'text/javascript';
script.charset = 'utf-8';
// Add the script to the head, upon which it will load and execute.
var head = document.getElementsByTagName('head')[0];
head.appendChild(script) 

 

The issue there is that you clog up the head of the document with lots of script nodes, which is why most libraries (like the YUI get() utility) will add an ID to the script element and remove the node after successfully retrieving the JSON data:

JAVASCRIPT: 

var script = document.getElementById('JSONP');
script.parentNode.removeChild(script);

 

 

The issue with this is that browsers do remove the node but fail to do a clean garbage collection of the JavaScript at the same time. This means to cleanly remove the script and its content, you need to also delete the properties of the script by hand:

JAVASCRIPT: 

// Remove any old script tags.
  var script;
  while (script = document.getElementById('JSONP')) {
    script.parentNode.removeChild(script);
    // Browsers won't garbage collect this object.
   // So castrate it to avoid a major memory leak.
    for (var prop in script) {
      delete script[prop];
    }
  }

 

from :http://ajaxian.com/archives/dynamic-script-generation-and-memory-leaks

分享到:
评论

相关推荐

    Detected memory leaks简单检测方法

    ### Detected Memory Leaks 简单检测方法 #### 背景介绍 在软件开发过程中,内存泄漏是一个常见的问题,特别是在使用C/C++等需要手动管理内存的语言进行开发时更为突出。内存泄漏不仅会导致程序占用的内存持续增长...

    Finding memory leaks发现内存的泄漏(6KB)

    这篇“Finding memory leaks发现内存的泄漏”可能是关于如何定位和解决内存泄漏的技术指南。 在C++编程中,程序员需要手动管理内存,通过`new`关键字申请内存,然后通过`delete`关键字释放内存。如果忘记释放或错误...

    论文研究-A Tool for Testing Java Memory Leaks.pdf

    标题《论文研究-A Tool for Testing Java Memory Leaks.pdf》和描述《一个测试Java内存泄漏的工具,党万春,钱巨,虽然Java语言有自带的垃圾回收机制,Java程序仍然存在由无效引用带来的内存泄漏。而内存泄漏会导致...

    How catch memory leaks with very little effort (7KB)

    标题 "How catch memory leaks with very little effort (7KB)" 提示了这是一个关于如何轻松检测内存泄漏的技术分享。内存泄漏是编程中的一个常见问题,尤其是在C++等不自动管理内存的语言中,程序员需要手动分配和...

    MemoryLeaks:内存泄漏的常见情况和使用MLeaksFinder的检测

    平常我们都会用 Instrument 的 Leaks / Allocations 或其他一些开源库进行内存泄露的排查,但是检查过程非常繁琐,而且不清晰,最主要的是Abandoned memory不会被检测出来。 Leaks 从苹果的开发者文档里可以看到,一...

    Java memory leaks

    ### Java内存泄漏详解 #### 摘要 本文旨在探讨如何在IBM WebSphere Application Server (WAS) 3.5、4.01 和 5.0 版本上诊断和定位 Java 内存泄漏问题,包括分布式环境和 z/OS 环境。文章将介绍一种通用的方法论,...

    Eliminating Memory Leaks in Symbian OS C++ Projects

    ### 消除Symbian OS C++项目中的内存泄漏 #### 概述 在Symbian OS中,内存管理是一项至关重要的任务,特别是在C++项目中。本文将深入探讨如何在Symbian OS环境中检测、避免内存泄漏,并提供具体方法来解决这些问题...

    Finding memory leaks

    在压缩包中的文件,如`memory_leaks.shtml.htm`,可能包含了更详细的内存泄漏检测和解决方法。`VC Empire.htm`可能涉及到在Visual C++环境中如何处理内存泄漏的问题。阅读这些文件可以获取更多实用技巧和案例分析。 ...

    checkmemoryleak

    内存泄漏是程序运行过程中的常见问题,特别是在C和C++这样的低级编程语言中,由于程序员需要手动管理内存,不正确的内存分配和释放可能导致内存泄漏。`__wrap_malloc`是一种调试技术,用于检测由`malloc()`函数引起...

    BREW Memory Leak Checker 20120219

    本程序是一个在模拟器上定位BREW应用... It can give the call stack of memory leaks (including the locations that MALLOC/REALLOC been called, and also the locations that ISHELL_CreateInstance been called).

    Objective C Memory Management Essentials(PACKT,2015)

    You will begin with a basic understanding of memory management, and why memory leaks occur in an application, moving on to autorelease pools and object creation/storage to get an idea of how memory ...

    memory leakge & initialization & invalid pointer

    Memory leaks occur when a program allocates memory but fails to release it back to the system, causing the application to consume more and more memory over time. This can lead to poor performance and ...

    MemoryAnalyzer-1.7.0.20170613-macosx.cocoa.x86_64.zip

    The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption. Use the Memory Analyzer to analyze productive heap dumps with...

    Accelerated .NET Memory Dump Analysis

    Learn how to analyze CLR 4 .NET application and service crashes and freezes, navigate through memory dump space (managed and unmanaged code) and diagnose corruption, leaks, CPU spikes, blocked ...

    BREW Memory Leak Checker 20120219 (with src)

    本程序是一个在模拟器上定位BREW应用... It can give the call stack of memory leaks (including the locations that MALLOC/REALLOC been called, and also the locations that ISHELL_CreateInstance been called).

    ember-memory-leaks-codemod:用于修复Ember应用程序中的内存泄漏的codemod的集合

    用法要从该项目运行特定的codemod,您将运行以下命令: npx ember-memory-leaks-codemod <TRANSFORM> path/of/files/ or/some**/*glob.js# oryarn global add ember-memory-leaks-codemodember-memory-leaks-codemod...

    Leaks内存泄露检测

    Leaks工具是苹果提供的一款强大的内存分析工具,用于检测应用程序中的内存泄露情况。本篇文章将详细阐述Leaks检测内存泄露的基本原理、使用方法,以及如何在实际开发中避免内存泄露。 首先,内存泄露是指程序在申请...

    MemoryAnalyzer-1.2.0.20120530-win32.win32.x86_64.zip

    The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption. Use the Memory Analyzer to analyze productive heap dumps with ...

    Bounds Checker6.01 for Delphi

    BoundsChecker automatically pinpoints static, stack and heap memory errors, and resource leaks. Unlike ordinary memory-checking tools, BoundsChecker validates the latest Windows APIs including ...

    Expert.Android

    436 pages Publisher: Apress; 1 edition (July 3, 2013) Language: English ISBN-10: 1430249501 ISBN-13: 978-1430249504 What you’ll learn ...How to eliminate memory leaks and poor-performing code

Global site tag (gtag.js) - Google Analytics