blob: 6d7fab6492f9ebdab1af182e252f5b9acc0e76fd [file] [log] [blame]
<!DOCTYPE html>
<html lang="en" data-content_root="" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />
<title>Memory Management &#8212; Apache Arrow v17.0.0.dev52</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "light";
</script>
<!-- Loaded before other Sphinx assets -->
<link href="../_static/styles/theme.css?digest=8d27b9dea8ad943066ae" rel="stylesheet" />
<link href="../_static/styles/bootstrap.css?digest=8d27b9dea8ad943066ae" rel="stylesheet" />
<link href="../_static/styles/pydata-sphinx-theme.css?digest=8d27b9dea8ad943066ae" rel="stylesheet" />
<link href="../_static/vendor/fontawesome/6.5.1/css/all.min.css?digest=8d27b9dea8ad943066ae" rel="stylesheet" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../_static/vendor/fontawesome/6.5.1/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../_static/vendor/fontawesome/6.5.1/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="../_static/vendor/fontawesome/6.5.1/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="../_static/design-style.1e8bd061cd6da7fc9cf755528e8ffc24.min.css" />
<link rel="stylesheet" type="text/css" href="../_static/theme_overrides.css" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=8d27b9dea8ad943066ae" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=8d27b9dea8ad943066ae" />
<script src="../_static/vendor/fontawesome/6.5.1/js/all.min.js?digest=8d27b9dea8ad943066ae"></script>
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<script src="../_static/clipboard.min.js"></script>
<script src="../_static/copybutton.js"></script>
<script src="../_static/design-tabs.js"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'cpp/memory';</script>
<script>
DOCUMENTATION_OPTIONS.theme_version = '0.15.2';
DOCUMENTATION_OPTIONS.theme_switcher_json_url = '/docs/_static/versions.json';
DOCUMENTATION_OPTIONS.theme_switcher_version_match = 'dev/';
DOCUMENTATION_OPTIONS.show_version_warning_banner = true;
</script>
<link rel="canonical" href="https://arrow.apache.org/docs/cpp/memory.html" />
<link rel="icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Arrays" href="arrays.html" />
<link rel="prev" title="High-Level Overview" href="overview.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
/* We explicitly disable cookie tracking to avoid privacy issues */
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '20']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<a id="pst-skip-link" class="skip-link" href="#main-content">Skip to main content</a>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>
Back to top
</button>
<input type="checkbox"
class="sidebar-toggle"
name="__primary"
id="__primary"/>
<label class="overlay overlay-primary" for="__primary"></label>
<input type="checkbox"
class="sidebar-toggle"
name="__secondary"
id="__secondary"/>
<label class="overlay overlay-secondary" for="__secondary"></label>
<div class="search-button__wrapper">
<div class="search-button__overlay"></div>
<div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
action="../search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
id="search-input"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar">
<div class="bd-header__inner bd-page-width">
<label class="sidebar-toggle primary-toggle" for="__primary">
<span class="fa-solid fa-bars"></span>
</label>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="../index.html">
<img src="../_static/arrow.png" class="logo__image only-light" alt="Apache Arrow v17.0.0.dev52 - Home"/>
<script>document.write(`<img src="../_static/arrow-dark.png" class="logo__image only-dark" alt="Apache Arrow v17.0.0.dev52 - Home"/>`);</script>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<nav class="navbar-nav">
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item">
<a class="nav-link nav-internal" href="../format/index.html">
Specifications
</a>
</li>
<li class="nav-item">
<a class="nav-link nav-internal" href="../developers/index.html">
Development
</a>
</li>
<li class="nav-item dropdown">
<button class="btn dropdown-toggle nav-item" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-controls="pst-nav-more-links">
Implementations
</button>
<ul id="pst-nav-more-links" class="dropdown-menu">
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../c_glib/index.html">
C/GLib
</a>
</li>
<li class="nav-item current active">
<a class="nav-link dropdown-item nav-internal" href="index.html">
C++
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://github.com/apache/arrow/blob/main/csharp/README.md">
C#
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://pkg.go.dev/github.com/apache/arrow/go/v17">
Go
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../java/index.html">
Java
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../js/index.html">
JavaScript
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/julia/">
Julia
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://github.com/apache/arrow/blob/main/matlab/README.md">
MATLAB
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/nanoarrow/">
nanoarrow
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../python/index.html">
Python
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../r/index.html">
R
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://github.com/apache/arrow/blob/main/ruby/README.md">
Ruby
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://docs.rs/crate/arrow/">
Rust
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../status.html">
Implementation Status
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/cpp/">
C++ cookbook
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/java/">
Java cookbook
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/py/">
Python cookbook
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/r/">
R cookbook
</a>
</li>
</ul>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item navbar-persistent--container">
<script>
document.write(`
<button class="btn navbar-btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
`);
</script>
</div>
<div class="navbar-item">
<script>
document.write(`
<div class="version-switcher__container dropdown">
<button id="pst-version-switcher-button-2"
type="button"
class="version-switcher__button btn btn-sm navbar-btn dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-2"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-2"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-2">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div>
`);
</script></div>
<div class="navbar-item">
<script>
document.write(`
<button class="btn btn-sm navbar-btn theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="theme-switch nav-link" data-mode="light"><i class="fa-solid fa-sun fa-lg"></i></span>
<span class="theme-switch nav-link" data-mode="dark"><i class="fa-solid fa-moon fa-lg"></i></span>
<span class="theme-switch nav-link" data-mode="auto"><i class="fa-solid fa-circle-half-stroke fa-lg"></i></span>
</button>
`);
</script></div>
<div class="navbar-item"><ul class="navbar-icon-links navbar-nav"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/apache/arrow" title="GitHub" class="nav-link" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><span><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i></span>
<span class="sr-only">GitHub</span></a>
</li>
<li class="nav-item">
<a href="https://twitter.com/ApacheArrow" title="X" class="nav-link" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><span><i class="fa-brands fa-square-x-twitter fa-lg" aria-hidden="true"></i></span>
<span class="sr-only">X</span></a>
</li>
</ul></div>
</div>
</div>
<div class="navbar-persistent--mobile">
<script>
document.write(`
<button class="btn navbar-btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
`);
</script>
</div>
<label class="sidebar-toggle secondary-toggle" for="__secondary" tabindex="0">
<span class="fa-solid fa-outdent"></span>
</label>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<div class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<nav class="navbar-nav">
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item">
<a class="nav-link nav-internal" href="../format/index.html">
Specifications
</a>
</li>
<li class="nav-item">
<a class="nav-link nav-internal" href="../developers/index.html">
Development
</a>
</li>
<li class="nav-item dropdown">
<button class="btn dropdown-toggle nav-item" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-controls="pst-nav-more-links-2">
Implementations
</button>
<ul id="pst-nav-more-links-2" class="dropdown-menu">
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../c_glib/index.html">
C/GLib
</a>
</li>
<li class="nav-item current active">
<a class="nav-link dropdown-item nav-internal" href="index.html">
C++
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://github.com/apache/arrow/blob/main/csharp/README.md">
C#
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://pkg.go.dev/github.com/apache/arrow/go/v17">
Go
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../java/index.html">
Java
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../js/index.html">
JavaScript
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/julia/">
Julia
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://github.com/apache/arrow/blob/main/matlab/README.md">
MATLAB
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/nanoarrow/">
nanoarrow
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../python/index.html">
Python
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../r/index.html">
R
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://github.com/apache/arrow/blob/main/ruby/README.md">
Ruby
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://docs.rs/crate/arrow/">
Rust
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-internal" href="../status.html">
Implementation Status
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/cpp/">
C++ cookbook
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/java/">
Java cookbook
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/py/">
Python cookbook
</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-item nav-external" href="https://arrow.apache.org/cookbook/r/">
R cookbook
</a>
</li>
</ul>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item">
<script>
document.write(`
<div class="version-switcher__container dropdown">
<button id="pst-version-switcher-button-3"
type="button"
class="version-switcher__button btn btn-sm navbar-btn dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-3"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-3"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-3">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div>
`);
</script></div>
<div class="navbar-item">
<script>
document.write(`
<button class="btn btn-sm navbar-btn theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="theme-switch nav-link" data-mode="light"><i class="fa-solid fa-sun fa-lg"></i></span>
<span class="theme-switch nav-link" data-mode="dark"><i class="fa-solid fa-moon fa-lg"></i></span>
<span class="theme-switch nav-link" data-mode="auto"><i class="fa-solid fa-circle-half-stroke fa-lg"></i></span>
</button>
`);
</script></div>
<div class="navbar-item"><ul class="navbar-icon-links navbar-nav"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/apache/arrow" title="GitHub" class="nav-link" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><span><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i></span>
<span class="sr-only">GitHub</span></a>
</li>
<li class="nav-item">
<a href="https://twitter.com/ApacheArrow" title="X" class="nav-link" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><span><i class="fa-brands fa-square-x-twitter fa-lg" aria-hidden="true"></i></span>
<span class="sr-only">X</span></a>
</li>
</ul></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
aria-label="Section Navigation">
<p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
<div class="bd-toc-item navbar-nav"><ul class="current nav bd-sidenav">
<li class="toctree-l1 has-children"><a class="reference internal" href="getting_started.html">Getting Started</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-1"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l2"><a class="reference internal" href="build_system.html">Using Arrow C++ in your own project</a></li>
<li class="toctree-l2"><a class="reference internal" href="conventions.html">Conventions</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorials/basic_arrow.html">Basic Arrow Data Structures</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorials/io_tutorial.html">Arrow File I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorials/compute_tutorial.html">Arrow Compute</a></li>
<li class="toctree-l2"><a class="reference internal" href="tutorials/datasets_tutorial.html">Arrow Datasets</a></li>
</ul>
</li>
<li class="toctree-l1 current active has-children"><a class="reference internal" href="user_guide.html">User Guide</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-2"><i class="fa-solid fa-chevron-down"></i></label><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="overview.html">High-Level Overview</a></li>
<li class="toctree-l2 current active"><a class="current reference internal" href="#">Memory Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.html">Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="datatypes.html">Data Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="tables.html">Tabular Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="compute.html">Compute Functions</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="gandiva.html">The Gandiva Expression Compiler</a><input class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-3"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="gandiva/expr_projector_filter.html">Gandiva Expression, Projector, and Filter</a></li>
<li class="toctree-l3"><a class="reference internal" href="gandiva/external_func.html">Gandiva External Functions Development Guide</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="streaming_execution.html">Acero: A C++ streaming execution engine</a><input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-4"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="acero/overview.html">Acero Overview</a></li>
<li class="toctree-l3"><a class="reference internal" href="acero/user_guide.html">Acero User’s Guide</a></li>
<li class="toctree-l3"><a class="reference internal" href="acero/substrait.html">Using Acero with Substrait</a></li>
<li class="toctree-l3"><a class="reference internal" href="acero/developer_guide.html">Developer’s Guide</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="io.html">Input / output and filesystems</a></li>
<li class="toctree-l2"><a class="reference internal" href="ipc.html">Reading and writing the Arrow IPC format</a></li>
<li class="toctree-l2"><a class="reference internal" href="orc.html">Reading and Writing ORC files</a></li>
<li class="toctree-l2"><a class="reference internal" href="parquet.html">Reading and writing Parquet files</a></li>
<li class="toctree-l2"><a class="reference internal" href="csv.html">Reading and Writing CSV files</a></li>
<li class="toctree-l2"><a class="reference internal" href="json.html">Reading JSON files</a></li>
<li class="toctree-l2"><a class="reference internal" href="dataset.html">Tabular Datasets</a></li>
<li class="toctree-l2"><a class="reference internal" href="flight.html">Arrow Flight RPC</a></li>
<li class="toctree-l2"><a class="reference internal" href="gdb.html">Debugging code using Arrow</a></li>
<li class="toctree-l2"><a class="reference internal" href="threading.html">Thread Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="opentelemetry.html">OpenTelemetry</a></li>
<li class="toctree-l2"><a class="reference internal" href="env_vars.html">Environment Variables</a></li>
</ul>
</li>
<li class="toctree-l1 has-children"><a class="reference internal" href="examples/index.html">Examples</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-5"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l2"><a class="reference internal" href="examples/cmake_minimal_build.html">Minimal build using CMake</a></li>
<li class="toctree-l2"><a class="reference internal" href="examples/compute_and_write_example.html">Compute and Write CSV Example</a></li>
<li class="toctree-l2"><a class="reference internal" href="examples/dataset_documentation_example.html">Arrow Datasets example</a></li>
<li class="toctree-l2"><a class="reference internal" href="examples/dataset_skyhook_scan_example.html">Arrow Skyhook example</a></li>
<li class="toctree-l2"><a class="reference internal" href="examples/row_columnar_conversion.html">Row to columnar conversion</a></li>
<li class="toctree-l2"><a class="reference internal" href="examples/tuple_range_conversion.html">std::tuple-like ranges to Arrow</a></li>
<li class="toctree-l2"><a class="reference internal" href="examples/converting_recordbatch_to_tensor.html">Converting RecordBatch to Tensor</a></li>
</ul>
</li>
<li class="toctree-l1 has-children"><a class="reference internal" href="api.html">API Reference</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" type="checkbox"/><label class="toctree-toggle" for="toctree-checkbox-6"><i class="fa-solid fa-chevron-down"></i></label><ul>
<li class="toctree-l2"><a class="reference internal" href="api/support.html">Programming Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/memory.html">Memory (management)</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/thread.html">Thread (management)</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/datatype.html">Data Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/array.html">Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/scalar.html">Scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/builder.html">Array Builders</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/table.html">Two-dimensional Datasets</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/c_abi.html">C Interfaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/compute.html">Compute Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/acero.html">Streaming Execution (Acero)</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/gandiva.html">Gandiva Expression Compiler</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/tensor.html">Tensors</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/utilities.html">Utilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/async.html">Asynchronous programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/io.html">Input / output</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/ipc.html">Arrow IPC</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/formats.html">File Formats</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/cuda.html">CUDA support</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/flight.html">Arrow Flight RPC</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/flightsql.html">Arrow Flight SQL</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/filesystem.html">Filesystems</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/dataset.html">Dataset</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference external" href="https://arrow.apache.org/cookbook/cpp/">C++ cookbook</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="../index.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item"><a href="index.html" class="nav-link">C++ Implementation</a></li>
<li class="breadcrumb-item"><a href="user_guide.html" class="nav-link">User Guide</a></li>
<li class="breadcrumb-item active" aria-current="page">Memory Management</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="memory-management">
<span id="cpp-memory-management"></span><h1>Memory Management<a class="headerlink" href="#memory-management" title="Permalink to this heading">#</a></h1>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="api/memory.html"><span class="doc">Memory management API reference</span></a></p>
</div>
<section id="buffers">
<h2>Buffers<a class="headerlink" href="#buffers" title="Permalink to this heading">#</a></h2>
<p>To avoid passing around raw data pointers with varying and non-obvious
lifetime rules, Arrow provides a generic abstraction called <a class="reference internal" href="api/memory.html#_CPPv4N5arrow6BufferE" title="arrow::Buffer"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::Buffer</span></code></a>.
A Buffer encapsulates a pointer and data size, and generally also ties its
lifetime to that of an underlying provider (in other words, a Buffer should
<em>always</em> point to valid memory till its destruction). Buffers are untyped:
they simply denote a physical memory area regardless of its intended meaning
or interpretation.</p>
<p>Buffers may be allocated by Arrow itself , or by third-party routines.
For example, it is possible to pass the data of a Python bytestring as a Arrow
buffer, keeping the Python object alive as necessary.</p>
<p>In addition, buffers come in various flavours: mutable or not, resizable or
not. Generally, you will hold a mutable buffer when building up a piece
of data, then it will be frozen as an immutable container such as an
<a class="reference internal" href="arrays.html"><span class="doc">array</span></a>.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Some buffers may point to non-CPU memory, such as GPU-backed memory
provided by a CUDA context. If you’re writing a GPU-aware application,
you will need to be careful not to interpret a GPU memory pointer as
a CPU-reachable pointer, or vice-versa.</p>
</div>
<section id="accessing-buffer-memory">
<h3>Accessing Buffer Memory<a class="headerlink" href="#accessing-buffer-memory" title="Permalink to this heading">#</a></h3>
<p>Buffers provide fast access to the underlying memory using the
<a class="reference internal" href="api/memory.html#_CPPv4NK5arrow6Buffer4sizeEv" title="arrow::Buffer::size"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">size()</span></code></a> and <a class="reference internal" href="api/memory.html#_CPPv4NK5arrow6Buffer4dataEv" title="arrow::Buffer::data"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">data()</span></code></a> accessors
(or <a class="reference internal" href="api/memory.html#_CPPv4N5arrow6Buffer12mutable_dataEv" title="arrow::Buffer::mutable_data"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">mutable_data()</span></code></a> for writable access to a mutable
buffer).</p>
</section>
<section id="slicing">
<h3>Slicing<a class="headerlink" href="#slicing" title="Permalink to this heading">#</a></h3>
<p>It is possible to make zero-copy slices of buffers, to obtain a buffer
referring to some contiguous subset of the underlying data. This is done
by calling the <code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::SliceBuffer()</span></code> and <code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::SliceMutableBuffer()</span></code>
functions.</p>
</section>
<section id="allocating-a-buffer">
<h3>Allocating a Buffer<a class="headerlink" href="#allocating-a-buffer" title="Permalink to this heading">#</a></h3>
<p>You can allocate a buffer yourself by calling one of the
<code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::AllocateBuffer()</span></code> or <code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::AllocateResizableBuffer()</span></code>
overloads:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">arrow</span><span class="o">::</span><span class="n">Result</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Buffer</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">maybe_buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arrow</span><span class="o">::</span><span class="n">AllocateBuffer</span><span class="p">(</span><span class="mi">4096</span><span class="p">);</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">maybe_buffer</span><span class="p">.</span><span class="n">ok</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// ... handle allocation error</span>
<span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">arrow</span><span class="o">::</span><span class="n">Buffer</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">maybe_buffer</span><span class="p">);</span>
<span class="kt">uint8_t</span><span class="o">*</span><span class="w"> </span><span class="n">buffer_data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="o">-&gt;</span><span class="n">mutable_data</span><span class="p">();</span>
<span class="n">memcpy</span><span class="p">(</span><span class="n">buffer_data</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;hello world&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">11</span><span class="p">);</span>
</pre></div>
</div>
<p>Allocating a buffer this way ensures it is 64-bytes aligned and padded
as recommended by the <a class="reference internal" href="../format/Layout.html"><span class="doc">Arrow memory specification</span></a>.</p>
</section>
<section id="building-a-buffer">
<h3>Building a Buffer<a class="headerlink" href="#building-a-buffer" title="Permalink to this heading">#</a></h3>
<p>You can also allocate <em>and</em> build a Buffer incrementally, using the
<a class="reference internal" href="api/memory.html#_CPPv4N5arrow13BufferBuilderE" title="arrow::BufferBuilder"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::BufferBuilder</span></code></a> API:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">BufferBuilder</span><span class="w"> </span><span class="n">builder</span><span class="p">;</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Resize</span><span class="p">(</span><span class="mi">11</span><span class="p">);</span><span class="w"> </span><span class="c1">// reserve enough space for 11 bytes</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="s">&quot;hello &quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">);</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="s">&quot;world&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span>
<span class="k">auto</span><span class="w"> </span><span class="n">maybe_buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">Finish</span><span class="p">();</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">maybe_buffer</span><span class="p">.</span><span class="n">ok</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// ... handle buffer allocation error</span>
<span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">arrow</span><span class="o">::</span><span class="n">Buffer</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">maybe_buffer</span><span class="p">;</span>
</pre></div>
</div>
<p>If a Buffer is meant to contain values of a given fixed-width type (for
example the 32-bit offsets of a List array), it can be more convenient to
use the template <a class="reference internal" href="api/memory.html#_CPPv4I00EN5arrow18TypedBufferBuilderE" title="arrow::TypedBufferBuilder"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::TypedBufferBuilder</span></code></a> API:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">TypedBufferBuilder</span><span class="o">&lt;</span><span class="kt">int32_t</span><span class="o">&gt;</span><span class="w"> </span><span class="n">builder</span><span class="p">;</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Reserve</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="w"> </span><span class="c1">// reserve enough space for two int32_t values</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="mh">0x12345678</span><span class="p">);</span>
<span class="n">builder</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="mh">-0x765643210</span><span class="p">);</span>
<span class="k">auto</span><span class="w"> </span><span class="n">maybe_buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">Finish</span><span class="p">();</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">maybe_buffer</span><span class="p">.</span><span class="n">ok</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// ... handle buffer allocation error</span>
<span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">arrow</span><span class="o">::</span><span class="n">Buffer</span><span class="o">&gt;</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">maybe_buffer</span><span class="p">;</span>
</pre></div>
</div>
</section>
</section>
<section id="memory-pools">
<span id="cpp-memory-pool"></span><h2>Memory Pools<a class="headerlink" href="#memory-pools" title="Permalink to this heading">#</a></h2>
<p>When allocating a Buffer using the Arrow C++ API, the buffer’s underlying
memory is allocated by a <a class="reference internal" href="api/memory.html#_CPPv4N5arrow10MemoryPoolE" title="arrow::MemoryPool"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::MemoryPool</span></code></a> instance. Usually this
will be the process-wide <em>default memory pool</em>, but many Arrow APIs allow
you to pass another MemoryPool instance for their internal allocations.</p>
<p>Memory pools are used for large long-lived data such as array buffers.
Other data, such as small C++ objects and temporary workspaces, usually
goes through the regular C++ allocators.</p>
<section id="default-memory-pool">
<h3>Default Memory Pool<a class="headerlink" href="#default-memory-pool" title="Permalink to this heading">#</a></h3>
<p>The default memory pool depends on how Arrow C++ was compiled:</p>
<ul class="simple">
<li><p>if enabled at compile time, a <a class="reference external" href="http://jemalloc.net/">jemalloc</a> heap;</p></li>
<li><p>otherwise, if enabled at compile time, a
<a class="reference external" href="https://github.com/microsoft/mimalloc">mimalloc</a> heap;</p></li>
<li><p>otherwise, the C library <code class="docutils literal notranslate"><span class="pre">malloc</span></code> heap.</p></li>
</ul>
</section>
<section id="overriding-the-default-memory-pool">
<h3>Overriding the Default Memory Pool<a class="headerlink" href="#overriding-the-default-memory-pool" title="Permalink to this heading">#</a></h3>
<p>One can override the above selection algorithm by setting the
<span class="target" id="index-0"></span><a class="reference internal" href="env_vars.html#envvar-ARROW_DEFAULT_MEMORY_POOL"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">ARROW_DEFAULT_MEMORY_POOL</span></code></a> environment variable.</p>
</section>
<section id="stl-integration">
<h3>STL Integration<a class="headerlink" href="#stl-integration" title="Permalink to this heading">#</a></h3>
<p>If you wish to use a Arrow memory pool to allocate the data of STL containers,
you can do so using the <a class="reference internal" href="api/memory.html#_CPPv4I0EN5arrow3stl9allocatorE" title="arrow::stl::allocator"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::stl::allocator</span></code></a> wrapper.</p>
<p>Conversely, you can also use a STL allocator to allocate Arrow memory,
using the <a class="reference internal" href="api/memory.html#_CPPv4I0EN5arrow3stl13STLMemoryPoolE" title="arrow::stl::STLMemoryPool"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::stl::STLMemoryPool</span></code></a> class. However, this may be less
performant, as STL allocators don’t provide a resizing operation.</p>
</section>
</section>
<section id="devices">
<h2>Devices<a class="headerlink" href="#devices" title="Permalink to this heading">#</a></h2>
<p>Many Arrow applications only access host (CPU) memory. However, in some cases
it is desirable to handle on-device memory (such as on-board memory on a GPU)
as well as host memory.</p>
<p>Arrow represents the CPU and other devices using the
<a class="reference internal" href="api/memory.html#_CPPv4N5arrow6DeviceE" title="arrow::Device"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::Device</span></code></a> abstraction. The associated class <a class="reference internal" href="api/memory.html#_CPPv4N5arrow13MemoryManagerE" title="arrow::MemoryManager"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::MemoryManager</span></code></a>
specifies how to allocate on a given device. Each device has a default memory manager, but
additional instances may be constructed (for example, wrapping a custom
<a class="reference internal" href="api/memory.html#_CPPv4N5arrow10MemoryPoolE" title="arrow::MemoryPool"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::MemoryPool</span></code></a> the CPU).
<a class="reference internal" href="api/memory.html#_CPPv4N5arrow13MemoryManagerE" title="arrow::MemoryManager"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::MemoryManager</span></code></a> instances which specify how to allocate
memory on a given device (for example, using a particular
<a class="reference internal" href="api/memory.html#_CPPv4N5arrow10MemoryPoolE" title="arrow::MemoryPool"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::MemoryPool</span></code></a> on the CPU).</p>
<section id="device-agnostic-programming">
<h3>Device-Agnostic Programming<a class="headerlink" href="#device-agnostic-programming" title="Permalink to this heading">#</a></h3>
<p>If you receive a Buffer from third-party code, you can query whether it is
CPU-readable by calling its <a class="reference internal" href="api/memory.html#_CPPv4NK5arrow6Buffer6is_cpuEv" title="arrow::Buffer::is_cpu"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">is_cpu()</span></code></a> method.</p>
<p>You can also view the Buffer on a given device, in a generic way, by calling
<a class="reference internal" href="api/memory.html#_CPPv4N5arrow6Buffer4ViewENSt10shared_ptrI6BufferEERKNSt10shared_ptrI13MemoryManagerEE" title="arrow::Buffer::View"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::Buffer::View()</span></code></a> or <a class="reference internal" href="api/memory.html#_CPPv4N5arrow6Buffer10ViewOrCopyENSt10shared_ptrI6BufferEERKNSt10shared_ptrI13MemoryManagerEE" title="arrow::Buffer::ViewOrCopy"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::Buffer::ViewOrCopy()</span></code></a>. This will
be a no-operation if the source and destination devices are identical.
Otherwise, a device-dependent mechanism will attempt to construct a memory
address for the destination device that gives access to the buffer contents.
Actual device-to-device transfer may happen lazily, when reading the buffer
contents.</p>
<p>Similarly, if you want to do I/O on a buffer without assuming a CPU-readable
buffer, you can call <a class="reference internal" href="api/memory.html#_CPPv4N5arrow6Buffer9GetReaderENSt10shared_ptrI6BufferEE" title="arrow::Buffer::GetReader"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::Buffer::GetReader()</span></code></a> and
<a class="reference internal" href="api/memory.html#_CPPv4N5arrow6Buffer9GetWriterENSt10shared_ptrI6BufferEE" title="arrow::Buffer::GetWriter"><code class="xref cpp cpp-func docutils literal notranslate"><span class="pre">arrow::Buffer::GetWriter()</span></code></a>.</p>
<p>For example, to get an on-CPU view or copy of an arbitrary buffer, you can
simply do:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">arrow</span><span class="o">::</span><span class="n">Buffer</span><span class="o">&gt;</span><span class="w"> </span><span class="n">arbitrary_buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">;</span>
<span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">arrow</span><span class="o">::</span><span class="n">Buffer</span><span class="o">&gt;</span><span class="w"> </span><span class="n">cpu_buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arrow</span><span class="o">::</span><span class="n">Buffer</span><span class="o">::</span><span class="n">ViewOrCopy</span><span class="p">(</span>
<span class="w"> </span><span class="n">arbitrary_buffer</span><span class="p">,</span><span class="w"> </span><span class="n">arrow</span><span class="o">::</span><span class="n">default_cpu_memory_manager</span><span class="p">());</span>
</pre></div>
</div>
</section>
</section>
<section id="memory-profiling">
<h2>Memory Profiling<a class="headerlink" href="#memory-profiling" title="Permalink to this heading">#</a></h2>
<p>On Linux, detailed profiles of memory allocations can be generated using
<code class="docutils literal notranslate"><span class="pre">perf</span> <span class="pre">record</span></code>, without any need to modify the binaries. These profiles can
show the traceback in addition to allocation size. This does require debug
symbols, from either a debug build or a release with debug symbols build.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If you are profiling Arrow’s tests on another platform, you can run the
following Docker container using Archery to access a Linux environment:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>archery<span class="w"> </span>docker<span class="w"> </span>run<span class="w"> </span>ubuntu-cpp<span class="w"> </span>bash
<span class="c1"># Inside the Docker container...</span>
/arrow/ci/scripts/cpp_build.sh<span class="w"> </span>/arrow<span class="w"> </span>/build
<span class="nb">cd</span><span class="w"> </span>build/cpp/debug
./arrow-array-test<span class="w"> </span><span class="c1"># Run a test</span>
apt-get<span class="w"> </span>update
apt-get<span class="w"> </span>install<span class="w"> </span>-y<span class="w"> </span>linux-tools-generic
<span class="nb">alias</span><span class="w"> </span><span class="nv">perf</span><span class="o">=</span>/usr/lib/linux-tools/&lt;version-path&gt;/perf
</pre></div>
</div>
</div>
<p>To track allocations, create probe points on each of the allocator methods used.
Collecting <code class="docutils literal notranslate"><span class="pre">$params</span></code> allows us to record the size of the allocations
requested, while collecting <code class="docutils literal notranslate"><span class="pre">$retval</span></code> allows us to record the address of
recorded allocations, so we can correlate them with the call to free/de-allocate.</p>
<div class="sd-tab-set docutils">
<input checked="checked" id="sd-tab-item-0" name="sd-tab-set-0" type="radio">
</input><label class="sd-tab-label" for="sd-tab-item-0">
jemalloc</label><div class="sd-tab-content docutils">
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>je_arrow_mallocx<span class="w"> </span><span class="s1">&#39;$params&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>je_arrow_mallocx%return<span class="w"> </span><span class="s1">&#39;$retval&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>je_arrow_rallocx<span class="w"> </span><span class="s1">&#39;$params&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>je_arrow_rallocx%return<span class="w"> </span><span class="s1">&#39;$retval&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>je_arrow_dallocx<span class="w"> </span><span class="s1">&#39;$params&#39;</span>
<span class="nv">PROBE_ARGS</span><span class="o">=</span><span class="s2">&quot;-e probe_libarrow:je_arrow_mallocx \</span>
<span class="s2"> -e probe_libarrow:je_arrow_mallocx__return \</span>
<span class="s2"> -e probe_libarrow:je_arrow_rallocx \</span>
<span class="s2"> -e probe_libarrow:je_arrow_rallocx__return \</span>
<span class="s2"> -e probe_libarrow:je_arrow_dallocx&quot;</span>
</pre></div>
</div>
</div>
<input id="sd-tab-item-1" name="sd-tab-set-0" type="radio">
</input><label class="sd-tab-label" for="sd-tab-item-1">
mimalloc</label><div class="sd-tab-content docutils">
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>mi_malloc_aligned<span class="w"> </span><span class="s1">&#39;$params&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>mi_malloc_aligned%return<span class="w"> </span><span class="s1">&#39;$retval&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>mi_realloc_aligned<span class="w"> </span><span class="s1">&#39;$params&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>mi_realloc_aligned%return<span class="w"> </span><span class="s1">&#39;$retval&#39;</span>
perf<span class="w"> </span>probe<span class="w"> </span>-x<span class="w"> </span>libarrow.so<span class="w"> </span>mi_free<span class="w"> </span><span class="s1">&#39;$params&#39;</span>
<span class="nv">PROBE_ARGS</span><span class="o">=</span><span class="s2">&quot;-e probe_libarrow:mi_malloc_aligned \</span>
<span class="s2"> -e probe_libarrow:mi_malloc_aligned__return \</span>
<span class="s2"> -e probe_libarrow:mi_realloc_aligned \</span>
<span class="s2"> -e probe_libarrow:mi_realloc_aligned__return \</span>
<span class="s2"> -e probe_libarrow:mi_free&quot;</span>
</pre></div>
</div>
</div>
</div>
<p>Once probes have been set, you can record calls with associated tracebacks using
<code class="docutils literal notranslate"><span class="pre">perf</span> <span class="pre">record</span></code>. In this example, we are running the StructArray unit tests in
Arrow:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>perf<span class="w"> </span>record<span class="w"> </span>-g<span class="w"> </span>--call-graph<span class="w"> </span>dwarf<span class="w"> </span><span class="se">\</span>
<span class="w"> </span><span class="nv">$PROBE_ARGS</span><span class="w"> </span><span class="se">\</span>
<span class="w"> </span>./arrow-array-test<span class="w"> </span>--gtest_filter<span class="o">=</span>StructArray*
</pre></div>
</div>
<p>If you want to profile a running process, you can run <code class="docutils literal notranslate"><span class="pre">perf</span> <span class="pre">record</span> <span class="pre">-p</span> <span class="pre">&lt;PID&gt;</span></code>
and it will record until you interrupt with CTRL+C. Alternatively, you can do
<code class="docutils literal notranslate"><span class="pre">perf</span> <span class="pre">record</span> <span class="pre">-P</span> <span class="pre">&lt;PID&gt;</span> <span class="pre">sleep</span> <span class="pre">10</span></code> to record for 10 seconds.</p>
<p>The resulting data can be processed with standard tools to work with perf or
<code class="docutils literal notranslate"><span class="pre">perf</span> <span class="pre">script</span></code> can be used to pipe a text format of the data to custom scripts.
The following script parses <code class="docutils literal notranslate"><span class="pre">perf</span> <span class="pre">script</span></code> output and prints the output in
new lines delimited JSON for easier processing.</p>
<div class="literal-block-wrapper docutils container" id="id1">
<div class="code-block-caption"><span class="caption-text">process_perf_events.py</span><a class="headerlink" href="#id1" title="Permalink to this code">#</a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="c1"># Example non-traceback line</span>
<span class="c1"># arrow-array-tes 14344 [003] 7501.073802: probe_libarrow:je_arrow_mallocx: (7fbcd20bb640) size=0x80 flags=6</span>
<span class="n">current</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">current_traceback</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="k">def</span> <span class="nf">new_row</span><span class="p">():</span>
<span class="k">global</span> <span class="n">current_traceback</span>
<span class="n">current</span><span class="p">[</span><span class="s1">&#39;traceback&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">current_traceback</span>
<span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">current</span><span class="p">))</span>
<span class="n">current_traceback</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="p">:</span>
<span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">:</span>
<span class="k">continue</span>
<span class="k">elif</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">:</span>
<span class="c1"># traceback line</span>
<span class="n">current_traceback</span> <span class="o">+=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">current</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">new_row</span><span class="p">()</span>
<span class="n">parts</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">&#39; +&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
<span class="n">parts</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
<span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="c1"># file</span>
<span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="c1"># &quot;14344&quot;</span>
<span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="c1"># &quot;[003]&quot;</span>
<span class="n">current</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">))</span>
<span class="n">current</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)</span>
<span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="c1"># (7fbcd20bddf0)</span>
<span class="k">if</span> <span class="n">parts</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;&lt;-&quot;</span><span class="p">:</span>
<span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="n">parts</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="n">parts</span><span class="p">:</span>
<span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">pair</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;=&quot;</span><span class="p">)</span>
<span class="n">params</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="n">current</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span>
</pre></div>
</div>
</div>
<p>Here’s an example invocation of that script, with a preview of output data:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>perf<span class="w"> </span>script<span class="w"> </span><span class="p">|</span><span class="w"> </span>python3<span class="w"> </span>/arrow/process_perf_events.py<span class="w"> </span>&gt;<span class="w"> </span>processed_events.jsonl
<span class="gp">$ </span>head<span class="w"> </span>processed_events.jsonl<span class="w"> </span><span class="p">|</span><span class="w"> </span>cut<span class="w"> </span>-c<span class="w"> </span>-120
<span class="go">{&quot;time&quot;: 14814.954378, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx&quot;, &quot;params&quot;: {&quot;flags&quot;: &quot;6&quot;, &quot;size&quot;: &quot;0x80&quot;}, &quot;traceback&quot;</span>
<span class="go">{&quot;time&quot;: 14814.95443, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx__return&quot;, &quot;params&quot;: {&quot;arg1&quot;: &quot;0x7f4a97e09000&quot;}, &quot;traceba</span>
<span class="go">{&quot;time&quot;: 14814.95448, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx&quot;, &quot;params&quot;: {&quot;flags&quot;: &quot;6&quot;, &quot;size&quot;: &quot;0x40&quot;}, &quot;traceback&quot;:</span>
<span class="go">{&quot;time&quot;: 14814.954486, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx__return&quot;, &quot;params&quot;: {&quot;arg1&quot;: &quot;0x7f4a97e0a000&quot;}, &quot;traceb</span>
<span class="go">{&quot;time&quot;: 14814.954502, &quot;event&quot;: &quot;probe_libarrow:je_arrow_rallocx&quot;, &quot;params&quot;: {&quot;flags&quot;: &quot;6&quot;, &quot;size&quot;: &quot;0x40&quot;, &quot;ptr&quot;: &quot;0x7f</span>
<span class="go">{&quot;time&quot;: 14814.954507, &quot;event&quot;: &quot;probe_libarrow:je_arrow_rallocx__return&quot;, &quot;params&quot;: {&quot;arg1&quot;: &quot;0x7f4a97e0a040&quot;}, &quot;traceb</span>
<span class="go">{&quot;time&quot;: 14814.954796, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx&quot;, &quot;params&quot;: {&quot;flags&quot;: &quot;6&quot;, &quot;size&quot;: &quot;0x40&quot;}, &quot;traceback&quot;</span>
<span class="go">{&quot;time&quot;: 14814.954805, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx__return&quot;, &quot;params&quot;: {&quot;arg1&quot;: &quot;0x7f4a97e0a080&quot;}, &quot;traceb</span>
<span class="go">{&quot;time&quot;: 14814.954817, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx&quot;, &quot;params&quot;: {&quot;flags&quot;: &quot;6&quot;, &quot;size&quot;: &quot;0x40&quot;}, &quot;traceback&quot;</span>
<span class="go">{&quot;time&quot;: 14814.95482, &quot;event&quot;: &quot;probe_libarrow:je_arrow_mallocx__return&quot;, &quot;params&quot;: {&quot;arg1&quot;: &quot;0x7f4a97e0a0c0&quot;}, &quot;traceba</span>
</pre></div>
</div>
<p>From there one can answer a number of questions. For example, the following
script will find which allocations were never freed, and print the associated
tracebacks along with the count of dangling allocations:</p>
<div class="literal-block-wrapper docutils container" id="id2">
<div class="code-block-caption"><span class="caption-text">count_tracebacks.py</span><a class="headerlink" href="#id2" title="Permalink to this code">#</a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">&#39;&#39;&#39;Find tracebacks of allocations with no corresponding free&#39;&#39;&#39;</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>
<span class="n">allocated</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:je_arrow_mallocx__return&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;arg1&#39;</span><span class="p">]</span>
<span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;traceback&#39;</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:je_arrow_rallocx&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;ptr&#39;</span><span class="p">]</span>
<span class="k">del</span> <span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:je_arrow_rallocx__return&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;arg1&#39;</span><span class="p">]</span>
<span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;traceback&#39;</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:je_arrow_dallocx&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;ptr&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">address</span> <span class="ow">in</span> <span class="n">allocated</span><span class="p">:</span>
<span class="k">del</span> <span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:mi_malloc_aligned__return&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;arg1&#39;</span><span class="p">]</span>
<span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;traceback&#39;</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:mi_realloc_aligned&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;p&#39;</span><span class="p">]</span>
<span class="k">del</span> <span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:mi_realloc_aligned__return&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;arg1&#39;</span><span class="p">]</span>
<span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;traceback&#39;</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;event&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;probe_libarrow:mi_free&quot;</span><span class="p">:</span>
<span class="n">address</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">][</span><span class="s1">&#39;p&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">address</span> <span class="ow">in</span> <span class="n">allocated</span><span class="p">:</span>
<span class="k">del</span> <span class="n">allocated</span><span class="p">[</span><span class="n">address</span><span class="p">]</span>
<span class="n">traceback_counts</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
<span class="k">for</span> <span class="n">traceback</span> <span class="ow">in</span> <span class="n">allocated</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
<span class="n">traceback_counts</span><span class="p">[</span><span class="n">traceback</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">traceback</span><span class="p">,</span> <span class="n">count</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">traceback_counts</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="o">-</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Num of dangling allocations:&quot;</span><span class="p">,</span> <span class="n">count</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">traceback</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>The script can be invoked like so:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>cat<span class="w"> </span>processed_events.jsonl<span class="w"> </span><span class="p">|</span><span class="w"> </span>python3<span class="w"> </span>/arrow/count_tracebacks.py
<span class="go">Num of dangling allocations: 1</span>
<span class="go"> 7fc945e5cfd2 arrow::(anonymous namespace)::JemallocAllocator::ReallocateAligned+0x13b (/build/cpp/debug/libarrow.so.700.0.0)</span>
<span class="go"> 7fc945e5fe4f arrow::BaseMemoryPoolImpl&lt;arrow::(anonymous namespace)::JemallocAllocator&gt;::Reallocate+0x93 (/build/cpp/debug/libarrow.so.700.0.0)</span>
<span class="go"> 7fc945e618f7 arrow::PoolBuffer::Resize+0xed (/build/cpp/debug/libarrow.so.700.0.0)</span>
<span class="go"> 55a38b163859 arrow::BufferBuilder::Resize+0x12d (/build/cpp/debug/arrow-array-test)</span>
<span class="go"> 55a38b163bbe arrow::BufferBuilder::Finish+0x48 (/build/cpp/debug/arrow-array-test)</span>
<span class="go"> 55a38b163e3a arrow::BufferBuilder::Finish+0x50 (/build/cpp/debug/arrow-array-test)</span>
<span class="go"> 55a38b163f90 arrow::BufferBuilder::FinishWithLength+0x4e (/build/cpp/debug/arrow-array-test)</span>
<span class="go"> 55a38b2c8fa7 arrow::TypedBufferBuilder&lt;int, void&gt;::FinishWithLength+0x4f (/build/cpp/debug/arrow-array-test)</span>
<span class="go"> 55a38b2bcce7 arrow::NumericBuilder&lt;arrow::Int32Type&gt;::FinishInternal+0x107 (/build/cpp/debug/arrow-array-test)</span>
<span class="go"> 7fc945c065ae arrow::ArrayBuilder::Finish+0x5a (/build/cpp/debug/libarrow.so.700.0.0)</span>
<span class="go"> 7fc94736ed41 arrow::ipc::internal::json::(anonymous namespace)::Converter::Finish+0x123 (/build/cpp/debug/libarrow.so.700.0.0)</span>
<span class="go"> 7fc94737426e arrow::ipc::internal::json::ArrayFromJSON+0x299 (/build/cpp/debug/libarrow.so.700.0.0)</span>
<span class="go"> 7fc948e98858 arrow::ArrayFromJSON+0x64 (/build/cpp/debug/libarrow_testing.so.700.0.0)</span>
<span class="go"> 55a38b6773f3 arrow::StructArray_FlattenOfSlice_Test::TestBody+0x79 (/build/cpp/debug/arrow-array-test)</span>
<span class="go"> 7fc944689633 testing::internal::HandleSehExceptionsInMethodIfSupported&lt;testing::Test, void&gt;+0x68 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc94468132a testing::internal::HandleExceptionsInMethodIfSupported&lt;testing::Test, void&gt;+0x5d (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc9446555eb testing::Test::Run+0xf1 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc94465602d testing::TestInfo::Run+0x13f (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc944656947 testing::TestSuite::Run+0x14b (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc9446663f5 testing::internal::UnitTestImpl::RunAllTests+0x433 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc94468ab61 testing::internal::HandleSehExceptionsInMethodIfSupported&lt;testing::internal::UnitTestImpl, bool&gt;+0x68 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc944682568 testing::internal::HandleExceptionsInMethodIfSupported&lt;testing::internal::UnitTestImpl, bool&gt;+0x5d (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc944664b0c testing::UnitTest::Run+0xcc (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)</span>
<span class="go"> 7fc9446d0299 RUN_ALL_TESTS+0x14 (/build/cpp/googletest_ep-prefix/lib/libgtest_maind.so.1.11.0)</span>
<span class="go"> 7fc9446d021b main+0x42 (/build/cpp/googletest_ep-prefix/lib/libgtest_maind.so.1.11.0)</span>
<span class="go"> 7fc9441e70b2 __libc_start_main+0xf2 (/usr/lib/x86_64-linux-gnu/libc-2.31.so)</span>
<span class="go"> 55a38b10a50d _start+0x2d (/build/cpp/debug/arrow-array-test)</span>
</pre></div>
</div>
</section>
</section>
</article>
<footer class="prev-next-footer">
<div class="prev-next-area">
<a class="left-prev"
href="overview.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">High-Level Overview</p>
</div>
</a>
<a class="right-next"
href="arrays.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Arrays</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>
</div>
</footer>
</div>
<div class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div
id="pst-page-navigation-heading-2"
class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> On this page
</div>
<nav class="bd-toc-nav page-toc" aria-labelledby="pst-page-navigation-heading-2">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#buffers">Buffers</a><ul class="visible nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#accessing-buffer-memory">Accessing Buffer Memory</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#slicing">Slicing</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#allocating-a-buffer">Allocating a Buffer</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#building-a-buffer">Building a Buffer</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#memory-pools">Memory Pools</a><ul class="visible nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#default-memory-pool">Default Memory Pool</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#overriding-the-default-memory-pool">Overriding the Default Memory Pool</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#stl-integration">STL Integration</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#devices">Devices</a><ul class="visible nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#device-agnostic-programming">Device-Agnostic Programming</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#memory-profiling">Memory Profiling</a></li>
</ul>
</nav></div>
<div class="sidebar-secondary-item">
<div class="tocsection editthispage">
<a href="https://github.com/apache/arrow/edit/main/docs/source/cpp/memory.rst">
<i class="fa-solid fa-pencil"></i>
Edit on GitHub
</a>
</div>
</div>
</div></div>
</div>
<footer class="bd-footer-content">
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script src="../_static/scripts/bootstrap.js?digest=8d27b9dea8ad943066ae"></script>
<script src="../_static/scripts/pydata-sphinx-theme.js?digest=8d27b9dea8ad943066ae"></script>
<footer class="bd-footer">
<div class="bd-footer__inner bd-page-width">
<div class="footer-items__start">
<div class="footer-item">
<p class="copyright">
© Copyright 2016-2024 Apache Software Foundation.
Apache Arrow, Arrow, Apache, the Apache feather logo, and the Apache Arrow project logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.
<br/>
</p>
</div>
<div class="footer-item">
<p class="sphinx-version">
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 6.2.0.
<br/>
</p>
</div>
</div>
<div class="footer-items__end">
<div class="footer-item">
<p class="theme-version">
Built with the <a href="https://pydata-sphinx-theme.readthedocs.io/en/stable/index.html">PyData Sphinx Theme</a> 0.15.2.
</p></div>
</div>
</div>
</footer>
</body>
</html>