<p dir="auto">Functions like <code class="notranslate">np.zeros</code>, <code class="notranslate">np.empty</code>, <code class="notranslate">np.ones</code> and <code class="notranslate">np.full</code> can be used to allocate large arrays easily. However, they always allocate memory, even if the array contains no data because one of the shape elements is 0. They, in particular, seem to allocate memory ignoring the 0s in the shape (<code class="notranslate">np.product([x for x in shape if x != 0])</code>).</p>
<p dir="auto">If any of the shape elements in zero, there's no possibility of any memory accesses ever, so the allocation seems unnecessary?</p>
<p dir="auto">If the shape has a zero, one can get an equivalent array using <code class="notranslate">broadcast_to</code>, which won't allocate.</p>
<h3 dir="auto">Reproducing code example:</h3>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import tracemalloc as tm

def test(f):
    tm.start()
    arr = f((0, 100, 0, 200, 0, 300, 0), dtype=np.uint8)
    mem, _ = tm.get_traced_memory()
    tm.stop()
    print(f&quot;{f.__name__}: allocated memory: {mem} bytes, array size: {arr.size}&quot;)

def full(shape, dtype):
    return np.full(shape, 123, dtype=dtype)

test(np.zeros)
test(np.empty)
test(np.ones)
test(full)

# workaround, use broadcasting
def broadcast(shape, dtype):
    return np.broadcast_to(dtype(), shape)
test(broadcast)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">tracemalloc</span> <span class="pl-k">as</span> <span class="pl-s1">tm</span>

<span class="pl-k">def</span> <span class="pl-en">test</span>(<span class="pl-s1">f</span>):
    <span class="pl-s1">tm</span>.<span class="pl-en">start</span>()
    <span class="pl-s1">arr</span> <span class="pl-c1">=</span> <span class="pl-en">f</span>((<span class="pl-c1">0</span>, <span class="pl-c1">100</span>, <span class="pl-c1">0</span>, <span class="pl-c1">200</span>, <span class="pl-c1">0</span>, <span class="pl-c1">300</span>, <span class="pl-c1">0</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">uint8</span>)
    <span class="pl-s1">mem</span>, <span class="pl-s1">_</span> <span class="pl-c1">=</span> <span class="pl-s1">tm</span>.<span class="pl-en">get_traced_memory</span>()
    <span class="pl-s1">tm</span>.<span class="pl-en">stop</span>()
    <span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">f</span>.<span class="pl-s1">__name__</span><span class="pl-kos">}</span></span>: allocated memory: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">mem</span><span class="pl-kos">}</span></span> bytes, array size: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">arr</span>.<span class="pl-s1">size</span><span class="pl-kos">}</span></span>"</span>)

<span class="pl-k">def</span> <span class="pl-en">full</span>(<span class="pl-s1">shape</span>, <span class="pl-s1">dtype</span>):
    <span class="pl-k">return</span> <span class="pl-s1">np</span>.<span class="pl-en">full</span>(<span class="pl-s1">shape</span>, <span class="pl-c1">123</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">dtype</span>)

<span class="pl-en">test</span>(<span class="pl-s1">np</span>.<span class="pl-s1">zeros</span>)
<span class="pl-en">test</span>(<span class="pl-s1">np</span>.<span class="pl-s1">empty</span>)
<span class="pl-en">test</span>(<span class="pl-s1">np</span>.<span class="pl-s1">ones</span>)
<span class="pl-en">test</span>(<span class="pl-s1">full</span>)

<span class="pl-c"># workaround, use broadcasting</span>
<span class="pl-k">def</span> <span class="pl-en">broadcast</span>(<span class="pl-s1">shape</span>, <span class="pl-s1">dtype</span>):
    <span class="pl-k">return</span> <span class="pl-s1">np</span>.<span class="pl-en">broadcast_to</span>(<span class="pl-en">dtype</span>(), <span class="pl-s1">shape</span>)
<span class="pl-en">test</span>(<span class="pl-s1">broadcast</span>)</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="zeros: allocated memory: 6000248 bytes, array size: 0
empty: allocated memory: 6000080 bytes, array size: 0
ones: allocated memory: 6000080 bytes, array size: 0
full: allocated memory: 6000080 bytes, array size: 0
broadcast: allocated memory: 3680 bytes, array size: 0"><pre class="notranslate"><code class="notranslate">zeros: allocated memory: 6000248 bytes, array size: 0
empty: allocated memory: 6000080 bytes, array size: 0
ones: allocated memory: 6000080 bytes, array size: 0
full: allocated memory: 6000080 bytes, array size: 0
broadcast: allocated memory: 3680 bytes, array size: 0
</code></pre></div>
<h3 dir="auto">Numpy/Python version information:</h3>
<ul dir="auto">
<li><code class="notranslate">numpy.__version__</code>: 1.18.4</li>
<li><code class="notranslate">sys.version</code>: 3.6.9 (default, Jul 10 2019, 12:25:55) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)]</li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; a = np.array([], dtype=np.int64).reshape((6, 10, 6, 10, 0, 5, 1, 9, 10, 2, 6, 2, 6, 0, 10, 9, 5, 1, 5, 1, 10, 3))
&gt;&gt;&gt; np.testing.assert_equal(a, a[()])
python(73392,0x113ae6dc0) malloc: can't allocate region
*** mach_vm_map(size=15746400002048) failed (error code=3)
python(73392,0x113ae6dc0) malloc: *** set a breakpoint in malloc_error_break to debug
python(73392,0x113ae6dc0) malloc: can't allocate region
*** mach_vm_map(size=15746400002048) failed (error code=3)
python(73392,0x113ae6dc0) malloc: *** set a breakpoint in malloc_error_break to debug
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 349, in assert_equal
    return assert_array_equal(actual, desired, err_msg, verbose)
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 936, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 770, in assert_array_compare
    flagged = func_assert_same_pos(x, y, func=isnan, hasval='nan')
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 746, in func_assert_same_pos
    raise AssertionError(msg)
AssertionError:
Arrays are not equal

x and y nan location mismatch:
 x: array([], shape=(6, 10, 6, 10, 0, 5, 1, 9, 10, 2, 6, 2, 6, 0, 10, 9, 5, 1, 5, 1, 10, 3),
      dtype=int64)
 y: array([], shape=(6, 10, 6, 10, 0, 5, 1, 9, 10, 2, 6, 2, 6, 0, 10, 9, 5, 1, 5, 1, 10, 3),
      dtype=int64)"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">int64</span>).<span class="pl-en">reshape</span>((<span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">0</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">10</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-c1">9</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">10</span>, <span class="pl-c1">3</span>))
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-s1">testing</span>.<span class="pl-en">assert_equal</span>(<span class="pl-s1">a</span>, <span class="pl-s1">a</span>[()])
<span class="pl-en">python</span>(<span class="pl-c1">73392</span>,<span class="pl-c1">0x113ae6dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-s1">can</span>'<span class="pl-s1">t</span> <span class="pl-s1">allocate</span> <span class="pl-s1">region</span>
<span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-en">mach_vm_map</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">15746400002048</span>) <span class="pl-s1">failed</span> (<span class="pl-s1">error</span> <span class="pl-s1">code</span><span class="pl-c1">=</span><span class="pl-c1">3</span>)
<span class="pl-s1">python</span>(<span class="pl-c1">73392</span>,<span class="pl-c1">0x113ae6dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-s1">set</span> <span class="pl-s1">a</span> <span class="pl-s1">breakpoint</span> <span class="pl-c1">in</span> <span class="pl-s1">malloc_error_break</span> <span class="pl-s1">to</span> <span class="pl-s1">debug</span>
<span class="pl-en">python</span>(<span class="pl-c1">73392</span>,<span class="pl-c1">0x113ae6dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-s1">can</span>'<span class="pl-s1">t</span> <span class="pl-s1">allocate</span> <span class="pl-s1">region</span>
<span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-en">mach_vm_map</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">15746400002048</span>) <span class="pl-en">failed</span> (<span class="pl-s1">error</span> <span class="pl-s1">code</span><span class="pl-c1">=</span><span class="pl-c1">3</span>)
<span class="pl-en">python</span>(<span class="pl-c1">73392</span>,<span class="pl-c1">0x113ae6dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-s1">set</span> <span class="pl-s1">a</span> <span class="pl-s1">breakpoint</span> <span class="pl-c1">in</span> <span class="pl-s1">malloc_error_break</span> <span class="pl-s1">to</span> <span class="pl-s1">debug</span>
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-c1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">349</span>, <span class="pl-c1">in</span> <span class="pl-s1">assert_equal</span>
    <span class="pl-s1">return</span> <span class="pl-en">assert_array_equal</span>(<span class="pl-s1">actual</span>, <span class="pl-s1">desired</span>, <span class="pl-s1">err_msg</span>, <span class="pl-s1">verbose</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">936</span>, <span class="pl-c1">in</span> <span class="pl-s1">assert_array_equal</span>
    <span class="pl-s1">verbose</span><span class="pl-c1">=</span><span class="pl-s1">verbose</span>, <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-s">'Arrays are not equal'</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">770</span>, <span class="pl-c1">in</span> <span class="pl-s1">assert_array_compare</span>
    <span class="pl-s1">flagged</span> <span class="pl-c1">=</span> <span class="pl-en">func_assert_same_pos</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">func</span><span class="pl-c1">=</span><span class="pl-s1">isnan</span>, <span class="pl-s1">hasval</span><span class="pl-c1">=</span><span class="pl-s">'nan'</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">746</span>, <span class="pl-c1">in</span> <span class="pl-s1">func_assert_same_pos</span>
    <span class="pl-s1">raise</span> <span class="pl-v">AssertionError</span>(<span class="pl-s1">msg</span>)
<span class="pl-v">AssertionError</span>:
<span class="pl-v">Arrays</span> <span class="pl-s1">are</span> <span class="pl-c1">not</span> <span class="pl-s1">equal</span>

<span class="pl-s1">x</span> <span class="pl-c1">and</span> <span class="pl-s1">y</span> <span class="pl-s1">nan</span> <span class="pl-s1">location</span> <span class="pl-s1">mismatch</span>:
 <span class="pl-s1">x</span>: <span class="pl-en">array</span>([], <span class="pl-s1">shape</span><span class="pl-c1">=</span>(<span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">0</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">10</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-c1">9</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">10</span>, <span class="pl-c1">3</span>),
      <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int64</span>)
 <span class="pl-s1">y</span>: <span class="pl-en">array</span>([], <span class="pl-s1">shape</span><span class="pl-c1">=</span>(<span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">0</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">10</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-c1">9</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">10</span>, <span class="pl-c1">3</span>),
      <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">int64</span>)</pre></div>
<p dir="auto">I also sometimes get MemoryError from similar examples, for instance, with <code class="notranslate">shape=[7, 2, 6, 10, 6, 10, 0, 5, 1, 9, 10, 2, 6, 2, 6, 0, 10, 9, 5, 1, 5, 1, 10, 3]</code>, and even <code class="notranslate">np.all</code> gives warnings from malloc.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; a = np.array([], dtype=np.int64).reshape([7, 2, 6, 10, 6, 10, 0, 5, 1, 9, 10, 2, 6, 2, 6, 0, 10, 9, 5, 1, 5, 1, 10, 3])
&gt;&gt;&gt; np.all(a == a[()])
python(77502,0x10cb79dc0) malloc: can't allocate region
*** mach_vm_map(size=220449600000000) failed (error code=3)
python(77502,0x10cb79dc0) malloc: *** set a breakpoint in malloc_error_break to debug
python(77502,0x10cb79dc0) malloc: can't allocate region
*** mach_vm_map(size=220449600000000) failed (error code=3)
python(77502,0x10cb79dc0) malloc: *** set a breakpoint in malloc_error_break to debug
False"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">int64</span>).<span class="pl-en">reshape</span>([<span class="pl-c1">7</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">0</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">10</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-c1">9</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">10</span>, <span class="pl-c1">3</span>])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">all</span>(<span class="pl-s1">a</span> <span class="pl-c1">==</span> <span class="pl-s1">a</span>[()])
<span class="pl-en">python</span>(<span class="pl-c1">77502</span>,<span class="pl-c1">0x10cb79dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-s1">can</span>'<span class="pl-s1">t</span> <span class="pl-s1">allocate</span> <span class="pl-s1">region</span>
<span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-en">mach_vm_map</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">220449600000000</span>) <span class="pl-s1">failed</span> (<span class="pl-s1">error</span> <span class="pl-s1">code</span><span class="pl-c1">=</span><span class="pl-c1">3</span>)
<span class="pl-s1">python</span>(<span class="pl-c1">77502</span>,<span class="pl-c1">0x10cb79dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-s1">set</span> <span class="pl-s1">a</span> <span class="pl-s1">breakpoint</span> <span class="pl-c1">in</span> <span class="pl-s1">malloc_error_break</span> <span class="pl-s1">to</span> <span class="pl-s1">debug</span>
<span class="pl-en">python</span>(<span class="pl-c1">77502</span>,<span class="pl-c1">0x10cb79dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-s1">can</span>'<span class="pl-s1">t</span> <span class="pl-s1">allocate</span> <span class="pl-s1">region</span>
<span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-en">mach_vm_map</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">220449600000000</span>) <span class="pl-en">failed</span> (<span class="pl-s1">error</span> <span class="pl-s1">code</span><span class="pl-c1">=</span><span class="pl-c1">3</span>)
<span class="pl-en">python</span>(<span class="pl-c1">77502</span>,<span class="pl-c1">0x10cb79dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-s1">set</span> <span class="pl-s1">a</span> <span class="pl-s1">breakpoint</span> <span class="pl-c1">in</span> <span class="pl-s1">malloc_error_break</span> <span class="pl-s1">to</span> <span class="pl-s1">debug</span>
<span class="pl-v">False</span></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.testing.assert_equal(a, a[()])
python(77502,0x10cb79dc0) malloc: can't allocate region
*** mach_vm_map(size=220449600000000) failed (error code=3)
python(77502,0x10cb79dc0) malloc: *** set a breakpoint in malloc_error_break to debug
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 349, in assert_equal
    return assert_array_equal(actual, desired, err_msg, verbose)
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 936, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 770, in assert_array_compare
    flagged = func_assert_same_pos(x, y, func=isnan, hasval='nan')
  File &quot;/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py&quot;, line 728, in func_assert_same_pos
    x_id = func(x)
MemoryError: Unable to allocate 0 bytes for an array with shape (7, 2, 6, 10, 6, 10, 0, 5, 1, 9, 10, 2, 6, 2, 6, 0, 10, 9, 5, 1, 5, 1, 10, 3) and data type bool"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-s1">testing</span>.<span class="pl-en">assert_equal</span>(<span class="pl-s1">a</span>, <span class="pl-s1">a</span>[()])
<span class="pl-en">python</span>(<span class="pl-c1">77502</span>,<span class="pl-c1">0x10cb79dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-s1">can</span>'<span class="pl-s1">t</span> <span class="pl-s1">allocate</span> <span class="pl-s1">region</span>
<span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-en">mach_vm_map</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">220449600000000</span>) <span class="pl-s1">failed</span> (<span class="pl-s1">error</span> <span class="pl-s1">code</span><span class="pl-c1">=</span><span class="pl-c1">3</span>)
<span class="pl-s1">python</span>(<span class="pl-c1">77502</span>,<span class="pl-c1">0x10cb79dc0</span>) <span class="pl-s1">malloc</span>: <span class="pl-c1">**</span><span class="pl-c1">*</span> <span class="pl-s1">set</span> <span class="pl-s1">a</span> <span class="pl-s1">breakpoint</span> <span class="pl-c1">in</span> <span class="pl-s1">malloc_error_break</span> <span class="pl-s1">to</span> <span class="pl-s1">debug</span>
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-c1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">349</span>, <span class="pl-c1">in</span> <span class="pl-s1">assert_equal</span>
    <span class="pl-s1">return</span> <span class="pl-en">assert_array_equal</span>(<span class="pl-s1">actual</span>, <span class="pl-s1">desired</span>, <span class="pl-s1">err_msg</span>, <span class="pl-s1">verbose</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">936</span>, <span class="pl-c1">in</span> <span class="pl-s1">assert_array_equal</span>
    <span class="pl-s1">verbose</span><span class="pl-c1">=</span><span class="pl-s1">verbose</span>, <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-s">'Arrays are not equal'</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">770</span>, <span class="pl-c1">in</span> <span class="pl-s1">assert_array_compare</span>
    <span class="pl-s1">flagged</span> <span class="pl-c1">=</span> <span class="pl-en">func_assert_same_pos</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">func</span><span class="pl-c1">=</span><span class="pl-s1">isnan</span>, <span class="pl-s1">hasval</span><span class="pl-c1">=</span><span class="pl-s">'nan'</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/Users/aaronmeurer/anaconda3/lib/python3.7/site-packages/numpy/testing/_private/utils.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">728</span>, <span class="pl-c1">in</span> <span class="pl-s1">func_assert_same_pos</span>
    <span class="pl-s1">x_id</span> <span class="pl-c1">=</span> <span class="pl-en">func</span>(<span class="pl-s1">x</span>)
<span class="pl-v">MemoryError</span>: <span class="pl-v">Unable</span> <span class="pl-s1">to</span> <span class="pl-s1">allocate</span> <span class="pl-c1">0</span> <span class="pl-s1">bytes</span> <span class="pl-k">for</span> <span class="pl-s1">an</span> <span class="pl-s1">array</span> <span class="pl-k">with</span> <span class="pl-en">shape</span> (<span class="pl-c1">7</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">0</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">10</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-c1">9</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">10</span>, <span class="pl-c1">3</span>) <span class="pl-c1">and</span> <span class="pl-s1">data</span> <span class="pl-s1">type</span> <span class="pl-s1">bool</span></pre></div>
<p dir="auto">Note:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; shape = [7, 2, 6, 10, 6, 10, 0, 5, 1, 9, 10, 2, 6, 2, 6, 0, 10, 9, 5, 1, 5, 1, 10, 3]
&gt;&gt;&gt; np.prod([i for i in shape if i])
220449600000000"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">shape</span> <span class="pl-c1">=</span> [<span class="pl-c1">7</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">6</span>, <span class="pl-c1">10</span>, <span class="pl-c1">0</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">9</span>, <span class="pl-c1">10</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">2</span>, <span class="pl-c1">6</span>, <span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-c1">9</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">10</span>, <span class="pl-c1">3</span>]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">prod</span>([<span class="pl-s1">i</span> <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-s1">shape</span> <span class="pl-k">if</span> <span class="pl-s1">i</span>])
<span class="pl-c1">220449600000000</span></pre></div>
<p dir="auto">It isn't clear to me if this is the sort of thing that should work. I would expect empty arrays to effectively ignore the nonzero entries in the shape except for broadcasting purposes. But maybe there are technical reasons why this cannot happen?</p>
<h3 dir="auto">Numpy/Python version information:</h3>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import sys, numpy; print(numpy.__version__, sys.version)
1.18.1 3.7.3 | packaged by conda-forge | (default, Mar 27 2019, 15:43:19)
[Clang 4.0.1 (tags/RELEASE_401/final)]"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">sys</span>, <span class="pl-s1">numpy</span>; <span class="pl-en">print</span>(<span class="pl-s1">numpy</span>.<span class="pl-s1">__version__</span>, <span class="pl-s1">sys</span>.<span class="pl-s1">version</span>)
<span class="pl-c1">1.18</span>.<span class="pl-c1">1</span> <span class="pl-c1">3.7</span>.<span class="pl-c1">3</span> <span class="pl-c1">|</span> <span class="pl-s1">packaged</span> <span class="pl-s1">by</span> <span class="pl-s1">conda</span><span class="pl-c1">-</span><span class="pl-s1">forge</span> <span class="pl-c1">|</span> (<span class="pl-s1">default</span>, <span class="pl-v">Mar</span> <span class="pl-c1">27</span> <span class="pl-c1">2019</span>, <span class="pl-c1">15</span>:<span class="pl-c1">43</span>:<span class="pl-c1">19</span>)
[<span class="pl-v">Clang</span> <span class="pl-c1">4.0</span><span class="pl-c1">.1</span> (<span class="pl-s1">tags</span><span class="pl-c1">/</span><span class="pl-v">RELEASE_401</span><span class="pl-c1">/</span><span class="pl-s1">final</span>)]</pre></div>