question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak when using unary_union

See original GitHub issue

When using unary_union it seems that some memory leaks occur.

For example when running the above code the virtual memory used by the python process increase continuously.

from shapely.geometry import Polygon, LineString
from shapely.ops import unary_union

a = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
b = LineString([(0, 0), (1, 1)])
for _ in range(1000000):
    c = unary_union((a, b))

I am facing the issue on a CentOS 7 machine, with Python3, Shapely 1.6.4.post2 (from pip) and Proj 4.8.0.

EDIT: the same append with cascaded_union

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sgilliescommented, Aug 18, 2020

Confirmed. I ran the code under valgrind with 100 loops and I see

==22834== 8,000 (6,400 direct, 1,600 indirect) bytes in 100 blocks are definitely lost in loss record 4,274 of 4,288
==22834==    at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22834==    by 0x90B9264: geos::geom::GeometryFactory::createMultiPolygon(std::vector<geos::geom::Geometry*, std::allocator<geos::geom::Geometry*> >*) const (in /home/sean/.local/pipx/venvs/fiona/lib/python3.7/site-packages/shapely/.libs/libgeos--no-undefined-fab82081.so)
==22834==    by 0x8AB63A8: GEOSGeom_createCollection_r (in /home/sean/.local/pipx/venvs/fiona/lib/python3.7/site-packages/shapely/.libs/libgeos_c-5031f9ac.so.1.13.1)
==22834==    by 0x6D42DAD: ffi_call_unix64 (in /usr/lib/x86_64-linux-gnu/libffi.so.6.0.4)
==22834==    by 0x6D4271E: ffi_call (in /usr/lib/x86_64-linux-gnu/libffi.so.6.0.4)
==22834==    by 0x6B2F943: _ctypes_callproc (in /usr/lib/python3.7/lib-dynload/_ctypes.cpython-37m-x86_64-linux-gnu.so)
==22834==    by 0x6B300D3: ??? (in /usr/lib/python3.7/lib-dynload/_ctypes.cpython-37m-x86_64-linux-gnu.so)
==22834==    by 0x5DBBC5: PyObject_Call (in /usr/bin/python3.7)
==22834==    by 0x622A92: ??? (in /usr/bin/python3.7)
==22834==    by 0x5D96DA: _PyObject_FastCallKeywords (in /usr/bin/python3.7)
==22834==    by 0x54AA50: ??? (in /usr/bin/python3.7)
==22834==    by 0x551C07: _PyEval_EvalFrameDefault (in /usr/bin/python3.7)
0reactions
sgilliescommented, Nov 3, 2021

Meanwhile, the work around is to use union in such a loop, or call unary_union once with a list of all source geometries, which is its intended use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory leak when using unary_union · Issue #745 - GitHub
When using unary_union it seems that some memory leaks occur. For example when running the above code the virtual memory used by the...
Read more >
Why does this union structure cause a memory leak?
Using valgrind I have determined that this data structure, as is, has a memory leak. The way to verify that this structure is...
Read more >
#830 (Possible memory leak in WKTReader with partial ...
Am I doing something wrong with the API or is this a bug with WKTReader not cleaning up in the case of an...
Read more >
QisBool Core Functions - Artwork Conversion Software
To perform Boolean operations (single threaded) such as Unary Union, ... to free this memory by calling QisBool_Release, otherwise a memory leak will...
Read more >
Changelog for GEOS 3.9.0 - ABI laboratory
... Sergey Fedoseev) - Fix crash in GEOSUnaryUnion with empty LineString (#928, Sergey Fedoseev) - Fix memory leak in SIRtree::insert (#919, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found