Last post Jan 26, 2017 08:38 AM by PatriceSc
Jan 25, 2017 09:10 AM|srtayade|LINK
I want to load user images in my aspx page. For that I'm calling my loaduserimages function (written in code behind) on page load and then populating the <div> tag using innerhtml. But this is throwing outofmemoryexception.
Please see the code snapshot attached and suggest me, if this is correct way or I should use other way.
I can't use listview control as I need to call similar function again using ajax and then I can't bind resource to listview.
Please see this for reference: http://stackoverflow.com/questions/41740582/solving-system-outofmemoryexception-using-visual-studio-diagnostic-tools
Jan 25, 2017 09:32 AM|PatriceSc|LINK
How many files do you have and what is the size for those files? data urls are best for small images and I suspect you end up currently with a huge page. Make sure your files are not bigger than needed (ie using a 2000x2000 pixels image to just show it
as a 20x20 thumbnail).
Also use the https://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx which is more efficient when you have a lot of string concatenations
or if you are working with big strings. ReadAllBytes returns a newly created array (you don't need to size first this array).
Jan 25, 2017 09:48 AM|srtayade|LINK
There are almost 33 files for current user which are of 31MB size.
Jan 25, 2017 10:13 AM|PatriceSc|LINK
This is the total size for all files? Technically speaking it should work. My first move would be to use a StringBuilder rather than string concatenation. See for example
https://support.microsoft.com/en-us/help/306822/how-to-improve-string-concatenation-performance-in-visual-c (it is also more efficient regarding space consumption).
Still it is around 1 Mb per file and you'll end up with a single 40 Mb page. Try to check if the size of each image in pixels is close to the actual size in pixel that will be used when shown in the page. If not, it might be worth to create a smaller thumbnail
for those images...
Changes would be:
// Before the loop
var sb=new System.Text.StringBuilder(); // to create the stringbuilder
// Inside the loop
sb.Append(Convert.ToBase64String(profpic); // to append the new content
// After the loop
pdeditUserimages.InnerHtml=sb.ToString(); // to assign this string content to the control
What happens now ? Do you have still the same issue? On which exact line does it fail?
Jan 25, 2017 11:01 AM|srtayade|LINK
No luck. It's throwing an exception at sb.ToString();
Jan 25, 2017 01:48 PM|PatriceSc|LINK
And the exception is? This is still and out of memory exception?
Jan 25, 2017 01:57 PM|srtayade|LINK
But now I have used way around.
I followed this option : https://www.codeproject.com/tips/775585/bind-gridview-using-ajax
Jan 26, 2017 02:54 AM|Cathy Zou|LINK
Have you solve your problem after using another way?
Jan 26, 2017 03:08 AM|srtayade|LINK
Yes. Now it's working fine, without throwing outofmemory exception.
Jan 26, 2017 08:38 AM|PatriceSc|LINK
Great it's solved. If you want still to investigate (I prefer to always understand what was wrong before switching possibly to something else), you could track the size of the StringBuilder as you go to see which size you are trying to handle and make sure
it matches what you expect. I'll likely give it a quick try to see if it seems to work on my side.
Technically speaking it should work (though 1 Mb per image seems a bit high from a bandwidth optimization perspective).