A lot of memory consumption from an unknown source when libOpen3D.so is linked
- Dominant language
- C++
- Stars
- 14k
- Forks
- 2.6k
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 6
Description
### Checklist
- [X] I have searched for [similar issues](https://github.com/isl-org/Open3D/issues).
- [X] For Python issues, I have tested with the [latest development wheel](http://www.open3d.org/docs/latest/getting_started.html#development-version-pip).
- [X] I have checked the [release documentation](http://www.open3d.org/docs/release/) and the [latest documentation](http://www.open3d.org/docs/latest/) (for `master` branch).
### Describe the issue
I'm trying to integrate Open3D's core capabilities to my project. However, I realized a weird memory behavior which limits the library's usage in memory limited devices. Even a minimal binary which does nothing and links Open3D shared library, consumes more than a 150 MB of memory. Also, at each run of the binary, it consumes a different amount of memory.
I am compiling Open3D 0.17.0 for arm64 architecture from source with its minimal settings (only core capabilities are required). **Dockerfile**:
```
FROM arm64v8/ubuntu:bionic
# For bash-specific commands
SHELL ["/bin/bash", "-c"]
WORKDIR /root
# Prevent interactive inputs when installing packages
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=America/Los_Angeles
ENV SUDO=command
# Dependencies: basic
RUN apt-get update && apt-get install -y \
git \
wget \
nano \
curl \
build-essential \
pkg-config \
&& rm -rf /var/lib/apt/lists/*
# Dependencies: cmake
ENV CMAKE_VERSION=3.20.6
RUN wget -q https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-linux-aarch64.tar.gz \
&& tar -xf cmake-${CMAKE_VERSION}-linux-aarch64.tar.gz
# Open3D repo
# Always keep /root/Open3D as the WORKDIR
RUN git clone https://github.com/isl-org/Open3D.git \
&& cd Open3D \
&& git checkout v0.17.0
WORKDIR /root/Open3D
# Open3D C++ dependencies
RUN ./util/install_deps_ubuntu.sh assume-yes
# Build
RUN mkdir build \
&& cd build \
&& /root/cmake-${CMAKE_VERSION}-linux-aarch64/bin/cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/Open3D \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=ON \
-DWITH_OPENMP=ON \
-DBUILD_EXAMPLES=OFF \
-DBUILD_UNIT_TESTS=OFF \
-DBUILD_BENCHMARKS=OFF \
-DBUILD_PYTHON_MODULE=OFF \
-DBUILD_CUDA_MODULE=OFF \
-DBUILD_COMMON_CUDA_ARCHS=OFF \
-DENABLE_CACHED_CUDA_MANAGER=OFF \
-DBUILD_GUI=OFF \
-DWITH_IPPICV=OFF \
-DENABLE_HEADLESS_RENDERING=OFF \
-DBUILD_SYCL_MODULE=OFF \
-DENABLE_SYCL_UNIFIED_SHARED_MEMORY=OFF \
-DBUILD_WEBRTC=OFF \
-DBUILD_JUPYTER_EXTENSION=OFF \
-DBUILD_LIBREALSENSE=OFF \
-DUSE_SYSTEM_LIBREALSENSE=OFF \
-DBUILD_AZURE_KINECT=OFF \
-DBUILD_TENSORFLOW_OPS=OFF \
-DBUILD_PYTORCH_OPS=OFF \
-DBUNDLE_OPEN3D_ML=OFF \
-DDEVELOPER_BUILD=OFF \
.. \
&& make VERBOSE=1 -j$(nproc) \
&& make install -j$(nproc)
ENV LD_LIBRARY_PATH /usr/local/Open3D/lib:${LD_LIBRARY_PATH}
```
Once the docker image is built, I am running a very minimal C++ code inside the container.
**test.cpp**:
```
#include
#include
#include
#include
int main(int argc, char *argv[]) {
open3d::geometry::TriangleMesh mesh;
while (argv[1]) {
std::this_thread::sleep_for(std::chrono::duration(1000.0));
}
return 0;
}
```
**CMakeLists.txt**:
```
project(open3d-test)
cmake_minimum_required(VERSION 3.0)
find_package(Open3D REQUIRED)
include_directories(${Open3D_INCLUDE_DIRS})
add_executable(test test.cpp)
target_link_libraries(test ${Open3D_LIBRARIES})
```
This binary almost does nothing except mmap'ing some shared libraries including libOpen3D.so whose disk size is only 38 MB.
```
ls -lah /usr/local/Open3D/lib/
total 38M
drwxr-xr-x 4 root root 4.0K Oct 31 14:08 .
drwxr-xr-x 4 root root 4.0K Oct 31 14:08 ..
drwxr-xr-x 3 root root 4.0K Oct 31 14:08 cmake
-rw-r--r-- 1 root root 38M Oct 31 14:08 libOpen3D.so
drwxr-xr-x 2 root root 4.0K Oct 31 14:08 pkgconfig
```
However, when I run the test binary, I see a lot of resident memory consumption. Also, each instance of the test binary consumes a different amount memory. Here's snapshot of top while running three instances of the test binary:

`pmap -X `, shows lots of Rss heap allocations.

I've tried some memory tools like valgrind to find where this allocations originate from but could not find anything. Disclaimer: I'm not very proficient with these tools.
I looks like either Open3D itself, or one of the 3rd-party libraries allocate this heap memory during shared library load, which is probably not a desired behavior.
Can anyone point me to or help with finding the source of these allocations?
### Steps to reproduce the bug
```python
N/A
```
### Error message
_No response_
### Expected behavior
Linking libOpen3D.so to a binary should not cause such memory allocations at binary start-up.
### Open3D, Python and System information
```markdown
- Operating system: Ubuntu 18.04 64-bit
- Open3D version: 0.17.0
- System architecture: arm64
- Is this a remote workstation?: yes
- How did you install Open3D?: build from source
- Compiler version (if built from source): gcc 7.5
```
### Additional information
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.