0

im using this code:

<!-- Progress bar holder -->
<div id="progress" style="width:300px; height:20px; border:1px solid #ccc; float: right; margin-top: 7px; margin-right: 7px;"></div>
<!-- Progress information -->
<div id="information" style="width; text-align: center; margin-top: 7px;"></div></div>

inside a while loop:

// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
document.getElementById("information").innerHTML="'.$i.' av '.$no_lines.' rader processade.";
</script>';
// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
// Send output to browser immediately
flush();

$i will increase with +1 every time the while loop does something, this can be very large. upwards of 300k. and currently the code crashes at ~14000 (memory usage is above 1gb ram then). it also hovers at around 30% cpu usage and makes internet usage very unpleasant while uploading

ive never worked with javascript before, and ive read up about memory leaks in javascript, however it doesnt help me with how i prevent this. i found some links which help me sort of, however i dont know how to apply them to my code http://javascript.info/tutorial/memory-leaks for example: empty(), remove(), removeData(). i also looked at this post: http://www.javascriptkit.com/javatutors/closuresleak/ it was helpful to understand what was happening but couldnt figure out what i did wrong. currently im only doing the code if $i % 100 == 0 which is so bad im not even proud of that "solution".

could anyone of you help me prevent the memory leak in my code?

asked Feb 24, 2015 at 8:55
3
  • Are you sending the JavaScript to the browser from PHP as a multipart? Commented Feb 24, 2015 at 8:58
  • I disagree with the approach you're using to send update information to the client. Though if you're using PHP your options are limited for dealing with asynchronous background process. Commented Feb 24, 2015 at 8:58
  • @Dai im using php and jeff, yes im using multipart Commented Feb 24, 2015 at 9:09

3 Answers 3

2

(Disclaimer: I'm a software engineer at Microsoft who works on the Internet Explorer Javascript engine "Chakra"):

Your Javascript code does not contain any loops which create new objects, so there is no risk of you leaking memory, though your use of innerHTML instead of direct DOM manipulation presents an opportunity for performance optimization (as using innerHTML causes the text to be parsed and processed which isn't a cheap operation, calling setAttribute("style", "width: ... is a lot cheaper.

As I wrote in my comment-reply to the question, I don't recommend the approach you're using to send progress updates to the client. This approach is known as Comet ( http://en.wikipedia.org/wiki/Comet_%28programming%29 ). But advising alternative approaches is off-topic and you haven't explained any details about this background-operation your code is executing so I'll refrain from further comment.

answered Feb 24, 2015 at 9:05
Sign up to request clarification or add additional context in comments.

Comments

0

It looks that what is happening, is you are appending more and more <script> tags to your document. Instead, try placing this code in your while:

<script language="javascript">
 var progress = document.getElementById('progress'),
 information = document.getElementById('information');
 progress.style.width = $percent;
 information.innerHTML= $i + ' av ' + $no_lines + ' rader processade.';
</script>

Looking at your variables and function names, it also looks like PHP, not the javascript. In this case, you should use setTimeout function instead of while and request progress update via AJAX.

answered Feb 24, 2015 at 9:07

1 Comment

yeah im using php, but i didnt think that would matter so i didnt include any of that code
0

You're continuously sending script blocks to the client, so your markup will grow forever.

In reality, you should probably use AJAX to call to a PHP page (or whatever server technology) to get the data you require.

This approach also stops the PHP process from being so "busy" continuously keeping an HTTP request alive.

If you used jQuery and AJAX, you just need a function that calls every X seconds e.g.

var myTimer = setInterval(2000, getData);
function getData() {
 $.ajax({
 url: "http://getData.php",
 dataType: json})
 .done(function( data ) { // data is the parsed json
 $("#progress").html(data.percentage);
 $("#information").html(data.information); 
 }
 }); 
}

Where the json looks something like

{
 percentage : 80,
 information : "some more info"
}

If you need a more "real-time" update, then you should use one of the methods of implementing COMET. You could just use a long-held HTTP request in the case above, with a timeout that re-requests the URL (which isn't great for PHP, better for other technologies).

answered Feb 24, 2015 at 9:04

6 Comments

i honestly thought this was ajax. how would i rewrite this to be an ajax code?
See the above JavaScript, you will need a PHP page willing to return JSON (or XML, but JSON is easier for this purpose). I'll add some edits.
wouldnt this solution mean id have to pass some data over to getData.php and then call it to get the data? and what would be the content of getData.php more than those 2 lines?
Without knowing what your background PHP process is doing (the purpose of the status bar), it's difficult to tell. If you had some kind of job processing, maybe you'd pass myplace.com/progress?job=12345 so it knew which job to report on. You'd expect the PHP to do the calculation of the progress (again, I don't have details) and then return the current status.
@Jeff-watskins ive tried my best all day now and i cant figure out what im doing wrong or how it should be done so ill ask for some more help. im using this code: pastebin.com/MTPrTjY7 (stripped down though) and this is the json: pastebin.com/fEhx4ryp how should this be done?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.