# How to compile vtk for memory profiling

**URL:** https://discourse.vtk.org/t/how-to-compile-vtk-for-memory-profiling/15321
**Category:** Development
**Created:** [February 26, 2025, 3:11pm UTC](https://discourse.vtk.org/t/how-to-compile-vtk-for-memory-profiling/15321 "2025-02-26T15:11:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kit](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@kit](https://discourse.vtk.org/u/kit)
#### Post date: [February 26, 2025, 3:11pm UTC](https://discourse.vtk.org/t/how-to-compile-vtk-for-memory-profiling/15321/1 "2025-02-26T15:11:23Z")

</div>

I am trying to memory profile a C++ library written on top of VTK. For profiling C executables adding the compile option

` -DCMAKE_CXX_FLAGS="-fsanitize=address -g3"`

works well (I am using gcc). However, I run into errors when trying to import vtk modules withing python, for example:

```auto
LD_PRELOAD=$(gcc -print-file-name=libasan.so) python3 -c "from vtk import vtkImageData"

```

I get errors like this

```auto
Indirect leak of 120 byte(s) in 1 object(s) allocated from:
    #0 0x7fd3be0ffb37 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x575303 (/usr/bin/python3.12+0x575303) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #2 0x5dc07c in _PyEval_EvalFrameDefault (/usr/bin/python3.12+0x5dc07c) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #3 0x549ae6 (/usr/bin/python3.12+0x549ae6) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #4 0x54b372 in PyObject_CallMethodObjArgs (/usr/bin/python3.12+0x54b372) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #5 0x5fda34 in PyImport_ImportModuleLevelObject (/usr/bin/python3.12+0x5fda34) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #6 0x5dc40e in _PyEval_EvalFrameDefault (/usr/bin/python3.12+0x5dc40e) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #7 0x5d560a in PyEval_EvalCode (/usr/bin/python3.12+0x5d560a) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #8 0x5d31ab (/usr/bin/python3.12+0x5d31ab) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #9 0x581e4c (/usr/bin/python3.12+0x581e4c) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #10 0x5db335 in _PyEval_EvalFrameDefault (/usr/bin/python3.12+0x5db335) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #11 0x549ae6 (/usr/bin/python3.12+0x549ae6) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
......

```

What should I add to the build configuration to ensure the wrapped python modules are profiled correctly?

Thanks

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 28, 2025, 1:21pm UTC](https://discourse.vtk.org/t/how-to-compile-vtk-for-memory-profiling/15321/2 "2025-02-28T13:21:28Z")

</div>

Hello,

The message is telling you a memory leak was detected in your program and where in your source code. It is also useful to post the entire message log. I think that message is unlikely the only one. Normally the profiler (LeakSanitizer?) outputs a lot of messages.

best,

PC

---

<div class="post-metadata">

### Author: ![kit](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@kit](https://discourse.vtk.org/u/kit)
#### Post date: [February 28, 2025, 2:37pm UTC](https://discourse.vtk.org/t/how-to-compile-vtk-for-memory-profiling/15321/3 "2025-02-28T14:37:47Z")

</div>

Yes you are correct I did not post the full output. There was alot and it was very repetative.

As it stands I did a bit more investigation and I think this is a python problem rather that a VTK problem. For example I got very similar (possibly the same) error messages if I did somethign like this:

```auto
PYTHONMALLOC=malloc \
LD_PRELOAD=$(gcc -print-file-name=libasan.so) \
python3 -c "pass"

```

My understanding is that python’s memory allocation does not play nicely with memory profiling tools like LeakSanitizer. I was able to get rid of the spurious error messages by adding the following suppression file (suppressions.txt)

```auto
  leak:_PyObject_Malloc
  leak:PyMem_Malloc
  leak:PyMem_RawMalloc
  leak:malloc
  leak:calloc
  leak:realloc
  leak:_dl_open
  leak:_dl_map_object

```

Then with the following command things seem to work

```auto
PYTHONMALLOC=malloc \
LD_PRELOAD=$(gcc -print-file-name=libasan.so)\
LSAN_OPTIONS=suppressions=./suppressions.txt \
python3 -c "from vtk import vtkImageData"

```

Hopefully this does not supress any genuine issues…

If there is any guidance or best practices for memory profiling VTK applications, advice will be gratefully received.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 28, 2025, 5:14pm UTC](https://discourse.vtk.org/t/how-to-compile-vtk-for-memory-profiling/15321/4 "2025-02-28T17:14:00Z")

</div>

That’s good to know. I was about to suggest you to compile Python with about the same configuration you used to build VTK and your program.
