Chrome HTML Page Memory Usage - javascript

I have a page which I loaded in Chrome with Timeline started, after 5th second, you can see three spikes. After 7th second page is loaded and stabilized. Looks like chrome has allocated memory but didnt garbage collect them.
On nearly 11th second (with a black mark on Image 1), I press Garbage Collect on timeline and I see all memory released down to 3 MB. After memory comes down to 3MB, my page works correctly.
My target is to find memory leaks (if there are any). But after garbage collect, memory comes down and it still runs correctly.
Is there anyway to force garbage collection after page is loaded? So that on mobile devices memory will be freed automatically?
On Image 2, what are these names? They dont look as used within my library (My library's name is WebAtoms), so can I just ignore them?
My library WebAtoms has few objects shown in yellow color, what does that mean (Image 3)?
Is there any tutorial or in depth analysis of how to understand these profiles/timelines and what do they mean?
Is there any straight forward way to get information about memory leak?
Image 1
Image 2
Image 3

You have no control on garbage collection in javascript, you can delete properties , or set null values though to hint the GC .
Garbage collection is handled differently from one browser to another since most of them have their own javascript VM. So what might be true with Chrome would not be true on a native mobile browser.
As for how to optimise code ,depends on what the code is.
Wants some tips on how to use all that mess ?
https://developers.google.com/chrome-developer-tools/docs/heap-profiling

Related

Reduce Javascript memory leaks?

I have been trying to reduce javascript memory used by our web application (single-page application without MVC framework with jQuery & Bootstrap and a lot of Plugins ). And remove memory leaks we have. In our web application, there is a dashboard filled with Highcharts. It can be of any number can be 0 or can be 50 on the basis of user's choice. There are plenty of pages with Datatables, there are a lot of forms to create update data through the app.
Now I have been called to reduce memory usage. I have reduced memory leaks using google chrome developer tools by using "Heap Shot Profiling" in memory tab. As on refresh of charts I was adding a new chart and not deleting old chart. So after removing old chart which is replaced made a good impact. Still, we want to dig dipper and see what else we can improve.
Right now I am seeing the same thing with our DataTables we are adding new Datatables whenever you go to a new page. After checking I found out that we are not destroying them after, destroying them do not make that impact what, we had with Highcharts.
Here we call a jQuery ajax call to change the content of the page. We use ".html( newContent )" to change text in our main container as well as with every HTML element in which we want to change content as per reading it says it should remove all the event listeners and references to those elements should be removed but still data tables are present in memory. And maybe many other things.
Furthermore, I tried removing BODY tag and checking memory screenshots. The size of the screenshot remains same. It shows a lot of elements have been de-attached. But its impact on Javascript Memory usage and Javascript memory allocation is near to 0.
Am I on the right path to reduce Memory leaks. Or there is nothing I can do now more?
There are few screenshots.
These all screenshots are after removing the all the children of the body tag. Like if we remove all of the elements it should have low javascript memory usage but here I see change near to 0.
Here you can see we are still using 50MB in javascript Memory.
We are still having Datatables in our memory.
There is an increase in the snapshot and also increase in memory allocation. Yes, there are decreases in memory allocation at down where it shows unattached elements but it is nominal.
What should be the interpretation of all these data?
I have read a lot of StackOverflow questions regarding this and blogs for memory leaks. But nothing is making a big impact?

Memory leak in canvas with getImageData and or putImageData

I am creating a realtime video editor that allows you to add certain video effects using the canvas. You'll notice in my code, I actually have two canvas elements. The process is:
<video> begins to play invisibly using CSS.
The visible <canvas> renders the video onto the canvas.
In order to add the realtime effects, I need to have another hidden <canvas> that will process the video image and then return that processed frame to the visible <canvas>. This is where I use getImageData() and putImageData()
I am seeing a memory leak in chrome right now. I have found many threads talking about this, but I haven't been able to find a solution.
I removed the "video effects" code for testing purposes and for the fiddle. It still memory leaks without the effect processing.
FIDDLE: http://jsfiddle.net/o8z4ocLd/
Watch the Memory using Chromes Task Manager (Burger Menu -> More Tools -> Task Manager)
You will notice the memory climb and then fall back down, only to rise higher than the last time. I have watched it climb well into 1+GB of memory usage to eventually crash the tab. I have tried different variations of setTimeout and requestAnimationFrame. All seem to result in a memory leak.
edit
one thing worth mentioning is this seems to be happening in only chrome. Firefox seems to handle the garbage collecting better. I haven't seen firefox go above 500mb.
So my chrome self updated to version 45 this morning. 45 came out sept. 1st 2015 and this issue appears to be fixed! I am no longer receiving a memory leak. This update has also fixed another issue I was having in this thread:
Canvas is stretching using drawImage

How to free used memory after loading differents page using AJAX? [duplicate]

I have a very basic ajax slideshow on my website. On every scroll, the new images and response content continually increase the amount of memory used by the browser.
I've done my research and tried all suggestions to reset the XHR object on each new request, but this does absolutely nothing to help.
The slideshows are basic but may contain hundreds of slides. I want a user to be able to navigate the slideshow indefinitely without crashing their browser. Is this even possible?
Thanks, Brian
Increasing memory usage is normal. You are, after all, loading more data each time - the HTML from your AJAX response, as well as the images that are being displayed. Unless you're using Adobe Pagemill-generated HTML, that's only going to be a few hundreds of bytes of HTML/text. It's the images that will suck up the most space. Everything get stuffed into the browser's cache.
Since you're not doing anything fancy with the DOM (building sub-trees and whatnot) directly, just replacing a chunk of HTML repetitively, eventually the browser will do a cleanup and chuck some of the unused/old/stale image data from memory/cache and reclaim some of that memory.
Now, if you were doing some highly complex DOM manipulations and generating lots of new nodes on the fly, and were leaking some nodes here and there, THEN you'd have a memory problem, as those leaked nodes will eventually bury the browser.
But, just increasing memory usage by loading images is nothing to worry about, it's just like a normal extended surfing session, except you're just loading some new pictures.
If its a slideshow, are you only showing one image at a time? If you do only show one at a time and you're never getting rid of the last one you show, it will always increase the memory. If you remove the slides not being shown, it should help.

Unloading Resources on HTML with JavaScript

I'm working on a HTML 5 game, it is already online, but it's currently small and everything is okay.
Thing is, as it grows, it's going to be loading many, many images, music, sound effects and more. After 15 minutes of playing the game, at least 100 different resources might have been loaded already. Since it's an HTML5 App, it never refreshes the page during the game, so they all stack in the background.
I've noticed that every resource I load - on WebKit at least, using the Web Inspector - remains there once I remove the <img>, the <link> to the CSS and else. I'm guessing it's still in memory, just not being used, right?
This would end up consuming a lot of RAM eventually, and lead to a downgrade in performance specially on iOS and Android mobiles (which I slightly notice already on the current version), whose resources are more limited than desktop computers.
My question is: Is it possible to fully unload a Resource, freeing space in the RAM, through JavaScript? Without having to refresh the whole page to "clean it".
Worst scenario: Would using frames help, by deleting a frame, to free those frames' resources?.
Thank you!
Your description implies you have fully removed all references to the resources. The behavior you are seeing, then, is simply the garbage collector not having been invoked to clean the space, which is common in javascript implementations until "necessary". Setting to null or calling delete will usually do no better.
As a common case, you can typically call CollectGarbage() during scene loads/unloads to force the collection process. This is typically the best solution when the data will be loaded for game "stages", as that is a time that is not time critical. You usually do not want the collector to invoke during gameplay unless it is not a very real-time game.
Frames are usually a difficult solution if you want to keep certain resources around for common game controls. You need to consider whether you are refreshing entire resources or just certain resources.
All you can do is rely on JavaScript's built in garbage collection mechanism.
This kicks in whenever there is no reference to your image.
So assuming you have a reference pointer for each image, if you use:
img.destroy()
or
img.parentNode.removeChild(img)
Worth checking out: http://www.ibm.com/developerworks/web/library/wa-memleak/
Also: Need help using this function to destroy an item on canvas using javascript
EDIT
Here is some code that allows you to load an image into a var.
<script language = "JavaScript">
var heavyImage = new Image();
heavyImage.src = "heavyimagefile.jpg";
......
heavyImage = null; // removes reference and frees up memory
</script>
This is better that using JQuery .load() becuase it gives you more control over image references, and they will be removed from memory if the reference is gone (null)
Taken from: http://www.techrepublic.com/article/preloading-and-the-javascript-image-object/5214317
Hope it helps!
There are 2 better ways to load images besides a normal <img> tag, which Google brilliantly discusses here:
http://www.youtube.com/watch?v=7pCh62wr6m0&list=UU_x5XG1OV2P6uZZ5FSM9Ttw&index=74
Loading the images in through an HTML5 <canvas> which is way way faster. I would really watch that video and implement these methods for more speed. I would imagine garbage collection with canvas would function better because it's breaking away from the DOM.
Embedded data urls, where the src attribute of an image tag is the actual binary data of the image (yeah it's a giant string). It starts like this: src="data:image/jpeg;base64,/9j/MASSIVE-STRING ... " After using this, you would of course want to use a method to remove this node as discussed in the other answers. (I don't know how to generate this base64 string, try Google or the video)
You said Worst scenario: Would using frames help, by deleting a frame, to free those frames' resources
It is good to use frame. Yes, it can free up resource by deleting the frames.
All right, so I've made my tests by loading 3 different HTML into an < article > tag. Each HTML had many, huge images. Somewhat about 15 huge images per "page".
So I used jQuery.load() function to insert each in the tag. Also had an extra HTML that only had an < h1 >, to see what happened when a page with no images was replacing the previous page.
Well, turns out the RAM goes bigger while you start scrolling, and shoots up when going through a particularly big image (big as in dimensions and size, not just size). But once you leave that behind and lighter images come to screen, the RAM consumption actually goes down. And whenever I replaced using JS the content of the page, the RAM consumption went really down when it was occupying to much. Virtual Memory remained always high and rarely went down.
So I guess the browser is quite smart about handling Resources. It does not seem to unload them if you leave it there for a long while, but as soon you start loading other pages or scrolling, it starts loading / freeing up.
I guess I don't have anything to worry about after all...
Thanks everyone! =)

Free up memory in MobileSafari (iPhone/iPad) from cached images

The iPad stops loading large images after about 8 or 9 images for me, since the page runs into its allocated memory limits.
Since I'm showing these images one at a time, I'd like to remove the old ones from the browser cache so I don't hit the limit.
Any ideas on how to do this in javascript?
See this question for a good answer. In a nutshell, you should use the same image objects and set the src instead of creating new images.

Categories

Resources