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

Memory leak with attributes (#25881) #32944

Open
Infiziert90 opened this issue Nov 19, 2019 · 15 comments
Open

Memory leak with attributes (#25881) #32944

Infiziert90 opened this issue Nov 19, 2019 · 15 comments
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Feedback Waiting on the submitter for answers PyQGIS Related to the PyQGIS API

Comments

@Infiziert90
Copy link

Infiziert90 commented Nov 19, 2019

Describe the bug
#25881
same area, but with different approach ...
In the docs you can find this approach for getting all attributes: [attr for attr in feature], so i did that and after a few seconds my memory was completely full.

When using attributes() it works fine.

How to Reproduce
Use a large dataset with NULL attributes

for feat in layer.getFeatures():
    [attr for attr in feat]

QGIS and OS versions
3.10.0

@Infiziert90 Infiziert90 added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Nov 19, 2019
@roya0045
Copy link
Contributor

can you explain what you are trying to accomplish?

@Infiziert90
Copy link
Author

Infiziert90 commented Nov 19, 2019

for feat in layer.getFeatures():
    [attr for attr in feat.attributes()]

In the c++ docs you can find for attributes() a note, that you also could accomplish the same when you use the way from my bug report.

https://qgis.org/api/classQgsFeature.html#a5a473f89868c234d53cea67504cd2f2e

But that way is leaking huge amount of memory for me...

@roya0045
Copy link
Contributor

And if you do ?

for feat in layer.getFeatures():
     feat.attributes()

@Infiziert90
Copy link
Author

When using attributes() it works fine.

@roya0045
Copy link
Contributor

And if you try

for feat in layer.getFeatures():
     [ attr for attr in feat]

As stated in the doc?

@Infiziert90
Copy link
Author

Memory leak

@roya0045
Copy link
Contributor

It might be the list comprehension and your dataset that causes leakage.

@Infiziert90
Copy link
Author

The leak is also present when using the normal for loop way.
And yes it is the dataset, but I think with the same problem from #25881

@Infiziert90
Copy link
Author

Same behavior with 3.12.0

@Infiziert90
Copy link
Author

Steps for reproduction ...

  1. open python console
  2. console ->
>>>layer = QgsVectorLayer(r"PATH\altlpar0.dbf", "", "ogr")
>>>for _ in range(1000):
...    for feat in layer.getFeatures():
...        t = [attr for attr in feat]
...        # let the gc cleanup t (no difference)
...        # del t

altlpar0.zip

@gioman gioman added the PyQGIS Related to the PyQGIS API label Feb 28, 2020
@Infiziert90
Copy link
Author

Tested it with 3.16.3 again and it still has the same memory leaking effect

@Infiziert90
Copy link
Author

The python docs also says that this way of doing it is possible: https://qgis.org/pyqgis/master/core/QgsFeature.html?highlight=qgsfeature#qgis.core.QgsFeature.attributes (in the note section)

while being at it again, i tried some things and documented it with screenshots, showing that qgis is even holding onto the leaked memory:
Same code as in #32944 (comment)
QGIS1

similiar approach but using the attributes function:
QGIS2

@roya0045
Copy link
Contributor

There is the possibility that the creation of an iterator in python is the issue, though this may come from sip as this process is not present in c++ and only built in python

SIP_PYOBJECT __iter__();

@nyalldawson
Copy link
Collaborator

I can't reproduce on current versions, is this still an issue?

@nyalldawson nyalldawson added the Feedback Waiting on the submitter for answers label Feb 18, 2025
@Infiziert90
Copy link
Author

I can't reproduce on current versions, is this still an issue?

Tried it with 3.40.3-Bratislava under Windows 11 Version 2009 and it still spiked above 12 GB memory usage.
I opened up RAMMap after the process had finished and the console returned back to being interactable, this tool showed me an active usage of over 4 GB with the a total of 12 GB being allocated as private memory by qgis-bin.exe.

Leaks:

layer = QgsVectorLayer(r"PATH\altlpar0.dbf", "", "ogr")
for _ in range(1000):
    for feat in layer.getFeatures():
        t = [attr for attr in feat]

The version below results in a memory usage of 286 MB, with no memory spikes at all.
Presumably because the GC is actually clearing things up.

No Leak:

layer = QgsVectorLayer(r"PATH\altlpar0.dbf", "", "ogr")
for _ in range(1000):
    for feat in layer.getFeatures():
        t = [attr for attr in feat.attributes()]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Feedback Waiting on the submitter for answers PyQGIS Related to the PyQGIS API
Projects
None yet
Development

No branches or pull requests

4 participants