-
Notifications
You must be signed in to change notification settings - Fork 653
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
Fail to allocate bitmap #386
Comments
Something very odd happened when I tried to debug in mplfinance.plot on the iteration that usually fails -> it went past it. Edit : this is consistently reproducible on my case. going in debug and doing a step by step works. After that, the plot window remains opened ( although blank ) for each subsequent chart,. The save is still happening. It then fails on bitmap number 740 |
The are several items of interest in your code and in your report of the issue:
for ix in df.index:
...
fig, axlist = mpf.plot(data, type='candle',volume=True,title=titleString,returnfig=True)
fig.savefig(detailsPath+titleString+'.jpg',dpi=150)
# notice in `fig.savefig()` that `fname` kwarg is ***not*** necessary, just pass file name as first argument
del fig If you need more help, please provide the information requested. |
If I break on the plot call, the charts that were kept opened when entering the previous breakpoint are all being closed and the memory gets freed. When the windows gets closed in bulk, other windows appear in the background as if they were there all the time but not getting displayed - see screenshot. Looks like breaking is giving the opportunity to python to clean up its state. Now I do not understand why it is crashing before reaching or even getting close to max physical memory. |
I'll try triggering the garbage collector regularly tomorrow. see if it makes a difference. |
@pkubryk
Let me know if any of these ideas work. And if you are able to create a more simple reproducible case. All the best. --Daniel |
@pkubryk
it works now. |
The following code creates the problem every time: import numpy
import matplotlib
from matplotlib import pyplot
#matplotlib.use("Agg") # This does indeed fix the problem but it would be better to fix the problem properly
image = numpy.zeros((256,256,3))
for i in range(500):
print(i)
pyplot.subplot(1, 1, 1) # rows, cols, itemno
pyplot.axis('off')
pyplot.imshow(image)
pyplot.savefig('plot_%d.png' % (i), dpi=250)
pyplot.close() This code causes the "Fail to allocate bitmap" every time when i = 369 |
@EricSchwerzel I have a couple other systems to test on; will try those too. Please let me know what system you are running. Please also add the following before your loop and report here the results: print('matplotlib backend=',matplotlib.get_backend())
print('matplotlib.__version__ =',matplotlib.__version__)
print('numpy.__version__ =',numpy.__version__) Thanks. --Daniel |
working ok for me on WSL2 Ubuntu: backend= Qt5Agg
matplotlib.__version__ = 3.3.4
numpy.__version__ = 1.19.2 and on Windows Powershell python: backend= Qt5Agg
matplotlib.__version__ = 3.3.3
numpy.__version__ = 1.20.0rc1 |
I am experiencing the exact same issue with this down to the error on iteration 369. The code I'm using is here: (https://github.com/dmitryr1234/Phonon-Explorer/blob/master/Python%20Code/plotDataWithFit.py), but requires some other scripts to run. I'm using Python 3.7.8 through the Windows Command Prompt in Windows Terminal. I have found that matplotlib.use("Agg") fixes my issue when nothing else would, but I still don't know what the issue was other than a memory buildup. |
More than 370 items must cause |
I am still unable to reproduce this issue. Although thank you @EvilDuncan for the simplified use-case that for you reproduces the problem. To those who can reproduce, can you please post here your OS information, amount of memory, and Also, indicate if you are running inside any kind of virtual environment, or vm, and if so which one and what your settings are. |
Windows10, 64GB, Python 3.8.8(Anaconda), matplotlib==3.4.2 |
As a software developer, I know that these sorts of bugs are a real pain. If you email me, I'd be happy to do a TeamViewer session by phone so that you can see it on my computer and do what ever tests you want. |
I would argue that this is the proper solution, if you do not need / want the GUI windows, then do not even create the GUI windows! Setting the env I think the core of the problem here is that when you import All of the GUI toolkits are implemented in c or c++ and their Python wrappers proxy the underlying objects up to us on the Python side. In order for this to segfault or leak memory there has to be some careful coordination between the Python and c++ sides. In this case when we "close" a Qt window from Python, we drop the Python side references to it, but the final clean up of the window and its resources is done via a Qt Signal/slot. The way that Qt's Signal/Slots work is that there is an internal queue, when you emit a Signal it goes on the queue and the main Qt loop will pop things off and dispatch out to the correct lots. In the tight loop of the OP, the Qt event loop is never given a chance to run so even though we may be gc'ing the Python there are still 350+ zombie windows in the background. I strongly suspect that you are running into some sort of soft resource limit. I suspect that the reason the behavior changes on debugging is that as part of the debugging process you allowed the Qt main loop to run (which solved your problem!). I think the options are:
See https://matplotlib.org/stable/users/interactive_guide.html and There is a third rather radical path (that may require changes to mplfinance) which is to not use |
You say to "do not even create the GUI windows". The source code I gave doesn't use the Windows GUI. It saves a file to disk! |
Yes it does, if your backend is # This creates:
# - matplotlib.figure.Figure
# - matplotlib.axes.Axes
# - PyQt5.XXX.QMainWindow (I do not remember the Qt modules off the top of my head)
# - ...ToolBar
# - ...Qwidget
pyplot.subplot(1, 1, 1) # rows, cols, itemno What matplotlib.use("Agg") does is select the Agg backend which does not wrap at GUI framework, see https://matplotlib.org/stable/tutorials/introductory/usage.html#backends for more details. |
Thanks.
|
I got the same failure on Windows 10, 16GB, Python 3.9.6, Matplotlib 3.4.2 (backend TkAgg), Numpy 1.20.2 Then I tried to define the figure directly and use garbage collector even with reimporting matplotlib.
but still i=369. Then I tried using an easier code again, but with range(300) and paste it two times manually in the terminal. This works and I could also see that the memory gets free in the time I copied the lines again. But as we want to use it in a script this doesn't help much, because copying this block two times in a script results again in a big loop which fails.
Last thing I tried was to put fig outside the loop and it works I stoped after 8000 iterations and it takes constantly 50MB Ram.
Edit:
Fails at i = 365 for plt.close(fig) and i = 369 for plt.close(0) instead:
|
Thank you @ALL! I also had the same issue and it worked out using |
Fantastic code example, and great workaround/solution. Both work (re-using figure, using "Agg"). Can someone explain (a) why this happens (370 is a particularly weird integer - why does it crash particularly there??), and (b) what does .use("Agg") do to prevent that? |
@KingOtto123 |
The code has a problem when i run in a new machine were i installed a new python/matplotlib That might give a clue. |
from pathlib import Path matplotlib.use('agg') It can solve my problem . |
It is September 2023 and I am also having this issue. In my case, the magic number is 185 images, just like in this StackOverflow post. Only What interesting is that I obtained 14 sets of 500 images in the middle of August and processed them without any issues. Yesterday (i.e. in about a month), I obtained 3 more sets of 500 images, tried to process one of them and got that error. Between August and September I did not upgrade anything related to Python or Anaconda or my Hardware. The only thing that received updates was my Windows. The bottom line is that the issues seems to pop up randomly. I am using Python 3.11.4, matplotlib 3.7.1. I have Windows 11 Enterprise 22H2, 32 GB of RAM, Intel Xeon on HP Z240. @alsaleem00 brought up an intersting suggestion that a matplotlib's upgrade could be responsible for the issue. matplotlib team, I would be gratefull to you if you could resolve this issue. Even though @tacaswell suggested that |
I am experiencing similar issues as described above -> my magic number on my code seems to be somewhere around 820.
the fig.clf() call is lot faster and doesn't seem to fail (at least not so fast as plt.close call), i ran 1 000 000 figures and it didn't fail. |
Hello,
I have the following code that systematically ends up in the following error after 396 charts are saved.
**Fail to allocate bitmap
Process finished with exit code -2147483645**
I tried several combinations including with plt.close all or plt.close of the figure.
I tried with jpeg and png extensions
Although the memory does not seem to be cleaned up ( linear growth ) it does not seem to be the reason since it can fail with plenty of memory left to allocate.
I checked that it was not some kind of murky limitation of the disk, by moving the creation of the file to an NVME disk.
Also updated my graphic card drivers, because why not.
As you see I also tried to reload the module every time in the loop ( desperate move :) )
Really not sure what I am doing wrong here
I updated mplfinance to 0.12.7a17, matplotlib to 3.4.1.
Python version 3.9.2
The text was updated successfully, but these errors were encountered: