Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 499059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:56:49+00:00 2026-05-13T05:56:49+00:00

In my application, I have a lot of RichTextBoxes that are created dynamically in

  • 0

In my application, I have a lot of RichTextBoxes that are created dynamically in run time. I realized that the application has a memory leak, which is caused by the RichTextBox controls. To prove that the memory leaks because of the control I wrote the following test method:

for (int i = 0; i < 3000; i++)
        {
            Control rich = new RichTextBox();
            pnl.Content = rich;
        }
GC.Collect();
GC.WaitForPendingFinalizers();

pnl is a ContentControl that is declared in Xaml code.
If you run the following code, you can see that the memory usage is growing rapidly.

Any ideas how to solve the problem?
I considered creating an object pool, but this would complicate my application, and I rather avoid it.


edit: I’ve added call to the garbage collector to demonstrate that the objects are not garbage collected – there is no improvement in the memory usage with and without the call to the GC collect method.
Note that calling rich.Dispose within the loop eliminates the memory usage growth.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:56:49+00:00Added an answer on May 13, 2026 at 5:56 am

    Found this elsewhere, and it seems to be correct as far as my testing is concerned.

    When a FlowDocument is created,
    relatively expensive formatting
    context objects are also created for
    it in its StructuralCache. When you
    create multiple FlowDocs in a tight
    loop, a StructuralCache is created for
    each FlowDoc. Let’s you called
    Gc.Collect at the end of the loop,
    hoping to recover some memory.
    StructuralCache has a finalizer
    releases this formatting context, but
    not immediately. The finalizer
    effectively schedules an operation to
    release contexts at
    DispatcherPriority.Background.

    So the RichTextBox (or FlowDocument) in case is not leaking just waiting on a background thread to cleanup. When it runs exactly who knows. I wish this just implemented a dispose method that would force a cleanup immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project which will end up have a lot of application
I have a legacy application that I inherited that passes a lot of XML
In my WPF application I have a lot of custom dialog boxes that pop
I have an application that has the following java files: Services: AccountService.java UserService.java MessageService.java
I am currently writing an application which will have a lot of transaction. Each
I have application that generates a lot of numbers and writes them to table
I have a Django application that have a lot of images items that have
In an application I have a lot of classes that are apparently not descendants
In our application we have a lot of sensitive configuration settings, which we are
I have an application (NOT MY Coding) which have a lot of crystal reports.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.