Memory leak with a memory streamWhat strategies and tools are useful for finding memory leaks in .NET?Creating a byte array from a streamHow do I copy the contents of one stream to another?Is a memory leak created if a MemoryStream in .NET is not closed?How do I save a stream to a file in C#?Memory Leak in C#How to convert an Stream into a byte[] in C#?Displaying live video from a raw uncompressed byte source in C#: WPF vs. Win formsHow do I generate a stream from a string?Why and How to avoid Event Handler memory leaks?

Why airport relocation isn't done gradually?

Extreme, but not acceptable situation and I can't start the work tomorrow morning

Why is my log file so massive? 22gb. I am running log backups

How to answer pointed "are you quitting" questioning when I don't want them to suspect

Is this food a bread or a loaf?

Landing in very high winds

Are objects structures and/or vice versa?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Lied on resume at previous job

How to manage monthly salary

Doomsday-clock for my fantasy planet

"listening to me about as much as you're listening to this pole here"

Is a vector space a subspace of itself?

Is Social Media Science Fiction?

What does 'script /dev/null' do?

Where to refill my bottle in India?

What causes the sudden spool-up sound from an F-16 when enabling afterburner?

Information to fellow intern about hiring?

LWC and complex parameters

How to deal with fear of taking dependencies

Denied boarding due to overcrowding, Sparpreis ticket. What are my rights?

What do the Banks children have against barley water?

Why is the design of haulage companies so “special”?

Map list to bin numbers



Memory leak with a memory stream


What strategies and tools are useful for finding memory leaks in .NET?Creating a byte array from a streamHow do I copy the contents of one stream to another?Is a memory leak created if a MemoryStream in .NET is not closed?How do I save a stream to a file in C#?Memory Leak in C#How to convert an Stream into a byte[] in C#?Displaying live video from a raw uncompressed byte source in C#: WPF vs. Win formsHow do I generate a stream from a string?Why and How to avoid Event Handler memory leaks?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I know this code is far from perfect but in my case this was the only
way to do it correctly because im embedding WPF in C#, and when
applying text regulary the Spellcheck does not work correctly



So this is my code:



RichTextBox temphotfix = new RichTextBox();
temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
MemoryStream stream = new MemoryStream(byteArray);
range.Load(stream, DataFormats.Rtf);
range = null;
temp = null;
byteArray = null;
temphotfix.Dispose();
stream.Dispose();


I stress tested this, and it seems like ever about 5 times the script gets ran, it adds about 1 MB ram.



What am i doing wrong, i litterly made everyting i used null, or desposed them.










share|improve this question



















  • 4





    Garbage Collection isn't going to happen when you set things to null or call Dispose. It runs when it deems the memory pressure requires it.

    – juharr
    Mar 22 at 2:18






  • 1





    The good news is that with 4GB free RAM, you can afford run this about 20,000 times before running out of memory. So see what happens when you run it 1,000,000 times - and if you run out of memory then, you've got a problem.

    – MineR
    Mar 22 at 2:59






  • 2





    you can use Using block to decorate the MemoryStream or as well on RichTextBox that will help this object gets deallocated as soon as it comes out of scope, instead of calling Dispose.

    – Abhinaw Kaushik
    Mar 22 at 4:11






  • 4





    Use a memory profiler to diagnose memory problems.

    – Eric Lippert
    Mar 22 at 5:27











  • Youd did not stress test this. That would have ended with an OOM exception or (more likely) with an Ok.

    – Henk Holterman
    Mar 23 at 20:26


















-1















I know this code is far from perfect but in my case this was the only
way to do it correctly because im embedding WPF in C#, and when
applying text regulary the Spellcheck does not work correctly



So this is my code:



RichTextBox temphotfix = new RichTextBox();
temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
MemoryStream stream = new MemoryStream(byteArray);
range.Load(stream, DataFormats.Rtf);
range = null;
temp = null;
byteArray = null;
temphotfix.Dispose();
stream.Dispose();


I stress tested this, and it seems like ever about 5 times the script gets ran, it adds about 1 MB ram.



What am i doing wrong, i litterly made everyting i used null, or desposed them.










share|improve this question



















  • 4





    Garbage Collection isn't going to happen when you set things to null or call Dispose. It runs when it deems the memory pressure requires it.

    – juharr
    Mar 22 at 2:18






  • 1





    The good news is that with 4GB free RAM, you can afford run this about 20,000 times before running out of memory. So see what happens when you run it 1,000,000 times - and if you run out of memory then, you've got a problem.

    – MineR
    Mar 22 at 2:59






  • 2





    you can use Using block to decorate the MemoryStream or as well on RichTextBox that will help this object gets deallocated as soon as it comes out of scope, instead of calling Dispose.

    – Abhinaw Kaushik
    Mar 22 at 4:11






  • 4





    Use a memory profiler to diagnose memory problems.

    – Eric Lippert
    Mar 22 at 5:27











  • Youd did not stress test this. That would have ended with an OOM exception or (more likely) with an Ok.

    – Henk Holterman
    Mar 23 at 20:26














-1












-1








-1








I know this code is far from perfect but in my case this was the only
way to do it correctly because im embedding WPF in C#, and when
applying text regulary the Spellcheck does not work correctly



So this is my code:



RichTextBox temphotfix = new RichTextBox();
temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
MemoryStream stream = new MemoryStream(byteArray);
range.Load(stream, DataFormats.Rtf);
range = null;
temp = null;
byteArray = null;
temphotfix.Dispose();
stream.Dispose();


I stress tested this, and it seems like ever about 5 times the script gets ran, it adds about 1 MB ram.



What am i doing wrong, i litterly made everyting i used null, or desposed them.










share|improve this question
















I know this code is far from perfect but in my case this was the only
way to do it correctly because im embedding WPF in C#, and when
applying text regulary the Spellcheck does not work correctly



So this is my code:



RichTextBox temphotfix = new RichTextBox();
temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
MemoryStream stream = new MemoryStream(byteArray);
range.Load(stream, DataFormats.Rtf);
range = null;
temp = null;
byteArray = null;
temphotfix.Dispose();
stream.Dispose();


I stress tested this, and it seems like ever about 5 times the script gets ran, it adds about 1 MB ram.



What am i doing wrong, i litterly made everyting i used null, or desposed them.







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 5:35









Uwe Keim

27.7k32134216




27.7k32134216










asked Mar 22 at 1:54









Mika SDMika SD

12




12







  • 4





    Garbage Collection isn't going to happen when you set things to null or call Dispose. It runs when it deems the memory pressure requires it.

    – juharr
    Mar 22 at 2:18






  • 1





    The good news is that with 4GB free RAM, you can afford run this about 20,000 times before running out of memory. So see what happens when you run it 1,000,000 times - and if you run out of memory then, you've got a problem.

    – MineR
    Mar 22 at 2:59






  • 2





    you can use Using block to decorate the MemoryStream or as well on RichTextBox that will help this object gets deallocated as soon as it comes out of scope, instead of calling Dispose.

    – Abhinaw Kaushik
    Mar 22 at 4:11






  • 4





    Use a memory profiler to diagnose memory problems.

    – Eric Lippert
    Mar 22 at 5:27











  • Youd did not stress test this. That would have ended with an OOM exception or (more likely) with an Ok.

    – Henk Holterman
    Mar 23 at 20:26













  • 4





    Garbage Collection isn't going to happen when you set things to null or call Dispose. It runs when it deems the memory pressure requires it.

    – juharr
    Mar 22 at 2:18






  • 1





    The good news is that with 4GB free RAM, you can afford run this about 20,000 times before running out of memory. So see what happens when you run it 1,000,000 times - and if you run out of memory then, you've got a problem.

    – MineR
    Mar 22 at 2:59






  • 2





    you can use Using block to decorate the MemoryStream or as well on RichTextBox that will help this object gets deallocated as soon as it comes out of scope, instead of calling Dispose.

    – Abhinaw Kaushik
    Mar 22 at 4:11






  • 4





    Use a memory profiler to diagnose memory problems.

    – Eric Lippert
    Mar 22 at 5:27











  • Youd did not stress test this. That would have ended with an OOM exception or (more likely) with an Ok.

    – Henk Holterman
    Mar 23 at 20:26








4




4





Garbage Collection isn't going to happen when you set things to null or call Dispose. It runs when it deems the memory pressure requires it.

– juharr
Mar 22 at 2:18





Garbage Collection isn't going to happen when you set things to null or call Dispose. It runs when it deems the memory pressure requires it.

– juharr
Mar 22 at 2:18




1




1





The good news is that with 4GB free RAM, you can afford run this about 20,000 times before running out of memory. So see what happens when you run it 1,000,000 times - and if you run out of memory then, you've got a problem.

– MineR
Mar 22 at 2:59





The good news is that with 4GB free RAM, you can afford run this about 20,000 times before running out of memory. So see what happens when you run it 1,000,000 times - and if you run out of memory then, you've got a problem.

– MineR
Mar 22 at 2:59




2




2





you can use Using block to decorate the MemoryStream or as well on RichTextBox that will help this object gets deallocated as soon as it comes out of scope, instead of calling Dispose.

– Abhinaw Kaushik
Mar 22 at 4:11





you can use Using block to decorate the MemoryStream or as well on RichTextBox that will help this object gets deallocated as soon as it comes out of scope, instead of calling Dispose.

– Abhinaw Kaushik
Mar 22 at 4:11




4




4





Use a memory profiler to diagnose memory problems.

– Eric Lippert
Mar 22 at 5:27





Use a memory profiler to diagnose memory problems.

– Eric Lippert
Mar 22 at 5:27













Youd did not stress test this. That would have ended with an OOM exception or (more likely) with an Ok.

– Henk Holterman
Mar 23 at 20:26






Youd did not stress test this. That would have ended with an OOM exception or (more likely) with an Ok.

– Henk Holterman
Mar 23 at 20:26













1 Answer
1






active

oldest

votes


















-1














As I told above in comment you can using, you can try this code. hope this should help.



 using (RichTextBox temphotfix = new RichTextBox())

temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
using (MemoryStream stream = new MemoryStream(byteArray))

range.Load(stream, DataFormats.Rtf);

range = null;
temp = null;
byteArray = null;
//temphotfix.Dispose();
//stream.Dispose();






share|improve this answer























  • I did not fix it, but i tink it does help a little bit.

    – Mika SD
    Mar 22 at 21:42











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55291781%2fmemory-leak-with-a-memory-stream%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














As I told above in comment you can using, you can try this code. hope this should help.



 using (RichTextBox temphotfix = new RichTextBox())

temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
using (MemoryStream stream = new MemoryStream(byteArray))

range.Load(stream, DataFormats.Rtf);

range = null;
temp = null;
byteArray = null;
//temphotfix.Dispose();
//stream.Dispose();






share|improve this answer























  • I did not fix it, but i tink it does help a little bit.

    – Mika SD
    Mar 22 at 21:42















-1














As I told above in comment you can using, you can try this code. hope this should help.



 using (RichTextBox temphotfix = new RichTextBox())

temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
using (MemoryStream stream = new MemoryStream(byteArray))

range.Load(stream, DataFormats.Rtf);

range = null;
temp = null;
byteArray = null;
//temphotfix.Dispose();
//stream.Dispose();






share|improve this answer























  • I did not fix it, but i tink it does help a little bit.

    – Mika SD
    Mar 22 at 21:42













-1












-1








-1







As I told above in comment you can using, you can try this code. hope this should help.



 using (RichTextBox temphotfix = new RichTextBox())

temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
using (MemoryStream stream = new MemoryStream(byteArray))

range.Load(stream, DataFormats.Rtf);

range = null;
temp = null;
byteArray = null;
//temphotfix.Dispose();
//stream.Dispose();






share|improve this answer













As I told above in comment you can using, you can try this code. hope this should help.



 using (RichTextBox temphotfix = new RichTextBox())

temphotfix.Font = new Font(temphotfix.Font.Name, 14);
System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(omschrijving.Document.ContentStart, omschrijving.Document.ContentEnd);
temphotfix.Text = oms;
string temp = temphotfix.Rtf;
byte[] byteArray = Encoding.ASCII.GetBytes(temp);
using (MemoryStream stream = new MemoryStream(byteArray))

range.Load(stream, DataFormats.Rtf);

range = null;
temp = null;
byteArray = null;
//temphotfix.Dispose();
//stream.Dispose();







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 4:22









Abhinaw KaushikAbhinaw Kaushik

370212




370212












  • I did not fix it, but i tink it does help a little bit.

    – Mika SD
    Mar 22 at 21:42

















  • I did not fix it, but i tink it does help a little bit.

    – Mika SD
    Mar 22 at 21:42
















I did not fix it, but i tink it does help a little bit.

– Mika SD
Mar 22 at 21:42





I did not fix it, but i tink it does help a little bit.

– Mika SD
Mar 22 at 21:42



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55291781%2fmemory-leak-with-a-memory-stream%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript