From 688652a70397369329f15794de9196ffb00333ce Mon Sep 17 00:00:00 2001 From: rlv-dan Date: Wed, 22 Apr 2020 20:00:02 +0200 Subject: [PATCH] Stream generated html directly to file to reduce memory consition --- Snap2HTML/frmMain.cs | 3 ++ Snap2HTML/frmMain_BackgroundWorker.cs | 44 ++++++++++++--------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/Snap2HTML/frmMain.cs b/Snap2HTML/frmMain.cs index 8a23de3..839375e 100644 --- a/Snap2HTML/frmMain.cs +++ b/Snap2HTML/frmMain.cs @@ -225,6 +225,9 @@ namespace Snap2HTML private void backgroundWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e ) { + GC.Collect(); + GC.WaitForPendingFinalizers(); + Cursor.Current = Cursors.Default; tabControl1.Enabled = true; this.Text = "Snap2HTML"; diff --git a/Snap2HTML/frmMain_BackgroundWorker.cs b/Snap2HTML/frmMain_BackgroundWorker.cs index c1efe04..13d323e 100644 --- a/Snap2HTML/frmMain_BackgroundWorker.cs +++ b/Snap2HTML/frmMain_BackgroundWorker.cs @@ -31,8 +31,6 @@ namespace Snap2HTML return; } - backgroundWorker.ReportProgress( 0, "Processing content..." ); - // Calculate some stats int totDirs = 0; int totFiles = 0; @@ -47,19 +45,11 @@ namespace Snap2HTML } } - // Convert to string with JS data object - var jsContent = BuildJavascriptContentArray( content, 0, backgroundWorker ); - if( backgroundWorker.CancellationPending ) - { - backgroundWorker.ReportProgress( 0, "User cancelled" ); - return; - } - // Let's generate the output backgroundWorker.ReportProgress( 0, "Generating HTML file..." ); - // Read sbTemplate + // Read template var sbTemplate = new StringBuilder(); try { @@ -119,20 +109,18 @@ namespace Snap2HTML writer.Write(template.Substring(0, startOfData)); - var pos = 0; - while( pos < jsContent.Length ) + BuildJavascriptContentArray( content, 0, writer, backgroundWorker ); + + if( backgroundWorker.CancellationPending ) { - var length = 1024 * 100; - if( ( pos + length ) > jsContent.Length ) length = jsContent.Length - pos; - writer.Write( jsContent.ToString( pos, length ) ); // writing in chunks reduces memory consumtion - pos += length; + backgroundWorker.ReportProgress( 0, "User cancelled" ); + return; } writer.Write( template.Substring( startOfData + 10) ); } - sbTemplate.Clear(); - jsContent.Clear(); + sbTemplate = null; if( settings.openInBrowser ) { @@ -340,8 +328,7 @@ namespace Snap2HTML } } - - private static StringBuilder BuildJavascriptContentArray(List content, int startIndex, BackgroundWorker bgWorker) + private static void BuildJavascriptContentArray( List content, int startIndex, StreamWriter writer, BackgroundWorker bgWorker ) { // Data format: // Each index in "dirs" array is an array representing a directory: @@ -357,7 +344,7 @@ namespace Snap2HTML // Assign an ID to each folder. This is equal to the index in the JS data array var dirIndexes = new Dictionary(); - for(var i=0; i 10240 ) + { + writer.Write( result.ToString() ); + result.Clear(); + } + if( bgWorker.CancellationPending ) { - return null; + return; } } - return result; + writer.Write( result.ToString() ); + + return; } }