Stream generated html directly to file to reduce memory consition

This commit is contained in:
rlv-dan 2020-04-22 20:00:02 +02:00
parent aeb8ec32a1
commit 688652a703
2 changed files with 23 additions and 24 deletions

View File

@ -225,6 +225,9 @@ namespace Snap2HTML
private void backgroundWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e ) private void backgroundWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
{ {
GC.Collect();
GC.WaitForPendingFinalizers();
Cursor.Current = Cursors.Default; Cursor.Current = Cursors.Default;
tabControl1.Enabled = true; tabControl1.Enabled = true;
this.Text = "Snap2HTML"; this.Text = "Snap2HTML";

View File

@ -31,8 +31,6 @@ namespace Snap2HTML
return; return;
} }
backgroundWorker.ReportProgress( 0, "Processing content..." );
// Calculate some stats // Calculate some stats
int totDirs = 0; int totDirs = 0;
int totFiles = 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 // Let's generate the output
backgroundWorker.ReportProgress( 0, "Generating HTML file..." ); backgroundWorker.ReportProgress( 0, "Generating HTML file..." );
// Read sbTemplate // Read template
var sbTemplate = new StringBuilder(); var sbTemplate = new StringBuilder();
try try
{ {
@ -119,20 +109,18 @@ namespace Snap2HTML
writer.Write(template.Substring(0, startOfData)); writer.Write(template.Substring(0, startOfData));
var pos = 0; BuildJavascriptContentArray( content, 0, writer, backgroundWorker );
while( pos < jsContent.Length )
if( backgroundWorker.CancellationPending )
{ {
var length = 1024 * 100; backgroundWorker.ReportProgress( 0, "User cancelled" );
if( ( pos + length ) > jsContent.Length ) length = jsContent.Length - pos; return;
writer.Write( jsContent.ToString( pos, length ) ); // writing in chunks reduces memory consumtion
pos += length;
} }
writer.Write( template.Substring( startOfData + 10) ); writer.Write( template.Substring( startOfData + 10) );
} }
sbTemplate.Clear(); sbTemplate = null;
jsContent.Clear();
if( settings.openInBrowser ) if( settings.openInBrowser )
{ {
@ -340,8 +328,7 @@ namespace Snap2HTML
} }
} }
private static void BuildJavascriptContentArray( List<SnappedFolder> content, int startIndex, StreamWriter writer, BackgroundWorker bgWorker )
private static StringBuilder BuildJavascriptContentArray(List<SnappedFolder> content, int startIndex, BackgroundWorker bgWorker)
{ {
// Data format: // Data format:
// Each index in "dirs" array is an array representing a directory: // 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 // Assign an ID to each folder. This is equal to the index in the JS data array
var dirIndexes = new Dictionary<string, string>(); var dirIndexes = new Dictionary<string, string>();
for(var i=0; i<content.Count; i++) for( var i = 0; i < content.Count; i++ )
{ {
dirIndexes.Add( content[i].FullPath, ( i + startIndex ).ToString() ); dirIndexes.Add( content[i].FullPath, ( i + startIndex ).ToString() );
} }
@ -414,13 +401,22 @@ namespace Snap2HTML
result.Append( "])" ); result.Append( "])" );
result.Append( "\n" ); result.Append( "\n" );
// Write result in chunks to limit memory consumtion
if( result.Length > 10240 )
{
writer.Write( result.ToString() );
result.Clear();
}
if( bgWorker.CancellationPending ) if( bgWorker.CancellationPending )
{ {
return null; return;
} }
} }
return result; writer.Write( result.ToString() );
return;
} }
} }