Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory gradually increased using d3 for real time chart?

I tried to simulate a real time chart with dynamic data using d3.js. I am running this using IE-10 browser.

My Source Code

I come across to a problem where the memory of my IE browser will be gradually increased if left the web application running for a period of time.

I Google searched the possible reason that caused this problem.

Two things come into my mind for discussion:

  1. The timer prevents the garbage collection of IE

  2. The d3 chart does not release memory after data.shift()

My question:

  1. How could I diagnose if my problem actually originated from discussion 1 or 2 or neither?

  2. How could I solve the memory problem?

You might need to download the code and run it with some time and monitor the iexplorer.exe using resource monitor in order to identify the problem.

Thank you.

Source Code:

<html>
<head>
<title>Animated Sparkline using SVG Path and d3.js</title>
<script src="http://mbostock.github.com/d3/d3.v2.js"></script>
<style>
/* tell the SVG path to be a thin blue line without any area fill */
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
</style>
</head>
<body>

<span>
<b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis  &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b> 1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
<div id="graph1" class="aGraph" style="width:300px; height:30px;"></div>
</span>



<script>
var myTimer;
function FeedDataToChart(id, width, height, interpolation, animate, updateDelay, transitionDelay, data, startIndex) {
// create an SVG element inside the #graph div that fills 100% of the div
var graph = d3.select(id).append("svg:svg").attr("width", "80%").attr("height", "80%");

// X scale will fit values from 0-10 within pixels 0-100
var x = d3.scale.linear().domain([0, 48]).range([10, width-10]); // starting point is -5 so the first value doesn't show and slides off the edge as part of the transition
// Y scale will fit values from 0-10 within pixels 0-100
var y = d3.scale.linear().domain([0, 20]).range([height-10, 10]);

// create a line object that represents the SVN line we're creating
var line = d3.svg.line()
// assign the X function to plot our line as we wish
.x(function(d,i) { 
// verbose logging to show what's actually being done
//console.log('Plotting X value for data point: ' + d + ' using index: ' + i + ' to be at: ' + x(i) + ' using our xScale.');
// return the X coordinate where we want to plot this datapoint
return x(i); 
})
.y(function(d) { 
// verbose logging to show what's actually being done
//console.log('Plotting Y value for data point: ' + d + ' to be at: ' + y(d) + " using our yScale.");
// return the Y coordinate where we want to plot this datapoint
return y(d); 
})
.interpolate(interpolation)

var counter = startIndex;

//var myData = data.slice();

// display the line by appending an svg:path element with the data line we created above
graph.append("svg:path").attr("d", line(data));
// or it can be done like this


function redrawWithAnimation() {
// update with animation
graph.selectAll("path")
.data([data]) // set the new data
.attr("transform", "translate(" + x(1) + ")") // set the transform to the right by x(1) pixels (6 for the scale we've set) to hide the new value
.attr("d", line) // apply the new data values ... but the new value is hidden at this point off the right of the canvas
.transition() // start a transition to bring the new value into view
.ease("linear")
.duration(transitionDelay) // for this demo we want a continual slide so set this to the same as the setInterval amount below
.attr("transform", "translate(" + x(0) + ")"); // animate a slide to the left back to x(0) pixels to reveal the new value   
}

function redrawWithoutAnimation() {
// static update without animation
graph.selectAll("path")
.data([data]) // set the new data
.attr("d", line); // apply the new data values
}

function stopTimer()
{
clearInterval(myTimer);
myTimer = null;
graph.selectAll("path").data([data]).remove().append("svg:path").attr("d", line);
buffer = null;

signalGenerator();
}

function startTimer()
{
if (myTimer == null)
{
myTimer = setInterval(function() {
if (counter < data.length - 1)
{
var v = data.shift(); // remove the first element of the array
data.push(v); // add a new element to the array (we're just taking the number we just shifted off the front and appending to the end)
if(animate) 
{
redrawWithAnimation();
} 
else 
{
redrawWithoutAnimation();
}
counter++;
}
else
{
//alert("no more data in buffer");
stopTimer();
counter = startIndex;
}
}, updateDelay);
}
}

startTimer();

}


var buffer;
function signalGenerator()
{
if (buffer == null)
{
buffer = new Array(100);

var i;
for (i = 0; i < buffer.length; i++)
{
buffer[i] = Math.random() * 10;
}

FeedDataToChart("#graph1", 300, 300, "basis", true, 100, 100, buffer, 0);

}
}

function startGenerator()
{
   signalGenerator();   

}

startGenerator();

</script>

</body>
</html>
like image 433
jhyap Avatar asked Dec 16 '13 08:12

jhyap


1 Answers

I tried as you said for 2 hours and it was initially 56 MB memory usage and in the end around 56.8 MB. It means only 0.8 MB difference with some exceptional cases. But I can help you finding the exact point where memory load is occurring. Just follow the steps one by one.

  1. Open "Developer Tools" of IE by pressing F12

  2. Go to Memory (A camera symbol or CTRL+7)

  3. Click the Start Profiling Session ( Green Play button on top)

  4. Take a Heap Snap Shot to create Base Line.

  5. Now every 10 or 15 minutes take a heap snap shot

  6. Do this for how many hours you require (In your case 2 hours)

  7. Once profiling is done for desired time, stop it and analyze from beginning by comparing Heap Snap Shots.

  8. If memory difference in the beginning and end is so big, check where this memory increase started by analyzing the memory difference in the snap shot.

  9. Here you can check the difference of memory used by the process in terms of bytes or KB.

  10. Check which function or variable or operation is creating the memory issue. Most probably some calculations that are repeatedly carried out so that the variables used in these calculations won't be released from a certain point of time. I saw some "Ba, p", "n, p", "Wa, n, p" etc when analyzed the memory flow. I believe the functions that use these variables are creating the problem for you.

Note

If you use the UI Responsiveness (CTRL+5), you can easily see that the Garbage Collection is carried out by IE automatically.

like image 99
Anoop Avatar answered Oct 24 '22 07:10

Anoop