Skip to content

High memory consumption #94

Open
Open

Description

@petyusa

Hi,

I have an app that generates cards for a boardgame. The text varies a lot, so I set the fontsize dynamically, in a do/while loop, where I check if the textBox is Truncated or not. here's a simple example:

RichString textBox = null;
var fontSize = 1000;
do
{
    fontSize--;
    textBox = new RichString
    {
        MaxHeight = 512,
        MaxWidth = 512
    };
    textBox.Add("test test test test test", fontSize: fontSize);
} while (textBox.Truncated);

The issue is, if the while loop runs a lot, the memory consumption of my app skyrockets. Here's an example snapshot:
image

As you can see, there are more then 16 million StyleManagers, and there's a huge Dictionary<string, Topten.RichTextKit.Style>.
I'm not sure if it's a bug, or I misuse it. Can you have a look at it?

Activity

  1. changed the title [-]clear[/-] [+]High memory consumption[/+] on Nov 11, 2024
  2. toptensoftware commented on Nov 12, 2024

    @toptensoftware
    Owner

    I'm not sure how you're getting 16M styles - unless you're actually using 16M different style combinations?

    To be clear, in your example, you could be creating up to 1,000 styles the first time you run through that code. The second time though those same 1,000 styles should be re-used. See here for the code that checks the style cache for existing values.

    When I tried your code, the first time through it did create a lot of styles, but when I ran the same code again, the previously created styles were re-used. Perhaps set a breakpoint on the line linked above and see if you can see why it's creating so many styles.

    btw: This would much faster (about 50x) if you used a binary search. eg: this code finds the same answer as yours with 11 attempts (instead of 800+). It will use less memory too.

     var min = 1;
     var max = 1000;
     while (max - min > 1)
     {
         var mid = (min + max) / 2;
         textBox = new RichString
         {
             MaxHeight = 512,
             MaxWidth = 512
         };
         textBox.Add("test test test test test", fontSize: mid);
         if (textBox.Truncated)
             max = mid;
         else
             min = mid;
     }```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions