microsoft / PowerToys

Windows system utilities to maximize productivity
MIT License
109.69k stars 6.46k forks source link

[Text Extractor] Memory over consumption #25336

Open AkazaRenn opened 1 year ago

AkazaRenn commented 1 year ago

Microsoft PowerToys version

0.69.0

Installation method

PowerToys auto-update

Running as admin

No

Area(s) with issue?

TextExtractor

Steps to reproduce

  1. Enable Text Extractor
  2. Run it once
  3. Check memory usage in Task Manager

✔️ Expected Behavior

Minimize the memory consumption at least when it is inactive.

❌ Actual Behavior

It eats about 700 MB of memory even when IDLE, this is way too much, especially when it isn't even doing anything.

Other Software

No response

stefansjfw commented 1 year ago

Hi, could you attach /bugreport? Thanks

microsoft-github-policy-service[bot] commented 1 year ago

Hi there!

We need a bit more information to really debug this issue. Can you add a "Report Bug" zip file here? You right click on our system tray icon and just go to report bug. Then drag the zipfile from your desktop onto the GitHub comment box in this issue. Thanks!
Report Bug

AkazaRenn commented 1 year ago

Hi, could you attach /bugreport? Thanks

Sorry for the late reply, here's the log PowerToysReport_2023-04-23-18-49-19.zip Screenshot 2023-04-23 185213

stefansjfw commented 1 year ago

@TheJoeFin Any ideas here? I can reproduce this (although in my case is not ~700MB, but ~300MB). So, triggering TextExtractor and just closing it consumes some memory and stays like that.

TheJoeFin commented 1 year ago

I have found a similar issue in Text Grab. I implemented a Stream Wrapper and called the Garbage Collector directly. This didn't solve the issue entirely, but it is the recommended mitigation. While working on this issue I have found several discussions that this is a bug in WPF. Apparently MemoryStream does not garbage collect quickly. I have also found with sufficient memory pressure, and/or time the memory is released.

Let's keep this issue open to track implementing the mitigations, or other fixes anyone else recommends.

TheJoeFin commented 1 year ago

@stefansjfw I have used the WrappingStream around the MemoryStreams here in my Fork, Can you see if the overuse of memory condition is improved for you? I am open to other ideas of how to release the memory once the OCR is done.

https://github.com/TheJoeFin/PowerToys/blob/main/src/modules/PowerOCR/PowerOCR/Helpers/WrappingStream.cs

CC @jaimecbernardo

crutkas commented 1 year ago

Think we may need to force a GC or something. I wouldn't say it is a leak but it lingers.

TheJoeFin commented 1 year ago

I do call GC.Collect(); after the OCR occurs. This does little to fix the issue.

Is this what you thought we should try or was there something else?

crutkas commented 1 year ago

we should look at the profiling tools here, something is being kept in memory and i think it is the img some how

XenoPanther commented 1 year ago

I'm still having this issue in 0.72. Memory stays around 425 MB after using the tool PowerToysReport_2023-08-01-18-36-25.zip

TheJoeFin commented 1 year ago

@XenoPanther this is still a known issue, I am looking for a reliable way to force the system to GC the image.

TheJoeFin commented 10 months ago

@lindexi you have done some work with Image GC issues in WPF. Any chance you would take a look at this issue and see if there are any improvements to be made here?

I have tried this fix with no meaningful reduction in memory:

oldImage.Source = null;
oldImage.UpdateLayout();