Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High memory consumption #94

Open
petyusa opened this issue Nov 11, 2024 · 1 comment
Open

High memory consumption #94

petyusa opened this issue Nov 11, 2024 · 1 comment

Comments

@petyusa
Copy link

petyusa commented Nov 11, 2024

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?

@petyusa petyusa changed the title clear High memory consumption Nov 11, 2024
@toptensoftware
Copy link
Owner

toptensoftware commented Nov 12, 2024

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants