# Std::bad\_alloc error

**URL:** <https://community.konduit.ai/t/std-bad-alloc-error/1736>\
**Category:** DL4J\
**Created:** [January 2, 2022, 6:35am UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736 "2022-01-02T06:35:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Purva-Chaudhari](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/purva-chaudhari/32/702_2.png) [@Purva-Chaudhari](https://community.konduit.ai/u/Purva-Chaudhari)\
**Post date:** [January 2, 2022, 6:35am UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/1 "2022-01-02T06:35:27Z")

</div>

Hi everyone, I have been trying to make the Unet model using DL4J. In the line computational\_graph.fit() I get following error

(I guess it is the memory error right? Is there any way to solve it or is it limited by the laptop configurations) Let me know if any more details are required

```nohighlight
    at org.nd4j.nativeblas.Nd4jCpu.mallocHost(Native Method)
    at org.nd4j.linalg.cpu.nativecpu.CpuMemoryManager.allocate(CpuMemoryManager.java:48)
    at org.nd4j.linalg.api.memory.abstracts.Nd4jWorkspace.alloc(Nd4jWorkspace.java:421)
    at org.nd4j.linalg.api.memory.abstracts.Nd4jWorkspace.alloc(Nd4jWorkspace.java:320)
    at org.nd4j.linalg.cpu.nativecpu.buffer.BaseCpuDataBuffer.<init>(BaseCpuDataBuffer.java:492)
    at org.nd4j.linalg.cpu.nativecpu.buffer.FloatBuffer.<init>(FloatBuffer.java:68)
    at org.nd4j.linalg.cpu.nativecpu.buffer.DefaultDataBufferFactory.create(DefaultDataBufferFactory.java:329)
    at org.nd4j.linalg.factory.Nd4j.createBuffer(Nd4j.java:1467)
    at org.nd4j.linalg.api.ndarray.BaseNDArray.<init>(BaseNDArray.java:324)
    at org.nd4j.linalg.cpu.nativecpu.NDArray.<init>(NDArray.java:191)
    at org.nd4j.linalg.cpu.nativecpu.CpuNDArrayFactory.createUninitialized(CpuNDArrayFactory.java:226)
    at org.nd4j.linalg.factory.Nd4j.createUninitialized(Nd4j.java:4364)
    at org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.preOutput(ConvolutionLayer.java:442)
    at org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.activate(ConvolutionLayer.java:505)
    at org.deeplearning4j.nn.graph.vertex.impl.LayerVertex.doForward(LayerVertex.java:110)
    at org.deeplearning4j.nn.graph.ComputationGraph.ffToLayerActivationsInWS(ComputationGraph.java:2135)
    at org.deeplearning4j.nn.graph.ComputationGraph.computeGradientAndScore(ComputationGraph.java:1372)
    at org.deeplearning4j.nn.graph.ComputationGraph.computeGradientAndScore(ComputationGraph.java:1341)
    at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:174)
    at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:61)
    at org.deeplearning4j.optimize.Solver.optimize(Solver.java:52)
    at org.deeplearning4j.nn.graph.ComputationGraph.fitHelper(ComputationGraph.java:1165)
    at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1115)
    at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1082)
    at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1018)
    at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1006)
    at activeSegmentation.deepLearning.UNet1.run(UNet1.java:111)
    at activeSegmentation.deepLearning.UNet1.main(UNet1.java:287)
```

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [January 3, 2022, 8:06am UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/2 "2022-01-03T08:06:22Z")

</div>

Unfortunately you haven’t provided the full stack trace.

In principle memory limitations are caught before an allocation error like that occurs.

Can you please share some details about the system this is running on?

---

<div class="post-metadata">

**Author:** ![Purva-Chaudhari](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/purva-chaudhari/32/702_2.png) [@Purva-Chaudhari](https://community.konduit.ai/u/Purva-Chaudhari)\
**Post date:** [January 3, 2022, 12:40pm UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/3 "2022-01-03T12:40:17Z")

</div>

Thank you for the reply,

My system configurations

|Processor|Inte - (R) Core™ i7-7500U CPU @ 2.70GHz 2.90 GHz|  
|Installed RAM - 12.0 GB|  
|System type - 64-bit operating system, x64-based processor|

I am using Intellij

Here is debug console output

```auto
18:26:15.587 [main] DEBUG oshi.util.platform.windows.WmiUtil - Query: SELECT Version,ProductType,BuildNumber,CSDVersion,SuiteMask FROM Win32_OperatingSystem 
18:26:16.055 [main] DEBUG oshi.software.os.windows.WindowsOSVersionInfoEx - Initialized OSVersionInfoEx
18:26:17.393 [main] DEBUG oshi.hardware.common.AbstractCentralProcessor - Oracle MXBean detected.
18:26:17.422 [main] DEBUG oshi.util.platform.windows.WmiUtil - Connected to ROOT\CIMV2 WMI namespace
18:26:17.422 [main] DEBUG oshi.util.platform.windows.WmiUtil - Query: SELECT ProcessorID FROM Win32_Processor 
18:26:17.461 [main] DEBUG oshi.util.platform.windows.WmiUtil - Connected to ROOT\CIMV2 WMI namespace
18:26:17.461 [main] DEBUG oshi.util.platform.windows.WmiUtil - Query: SELECT Name,PercentIdleTime,PercentPrivilegedTime,PercentUserTime,PercentInterruptTime,PercentDPCTime FROM Win32_PerfRawData_Counters_ProcessorInformation WHERE NOT Name LIKE "%_Total"
18:26:24.431 [main] DEBUG oshi.util.platform.windows.WmiUtil - Connected to ROOT\CIMV2 WMI namespace
18:26:24.431 [main] DEBUG oshi.util.platform.windows.WmiUtil - Query: SELECT PercentInterruptTime,PercentDPCTime FROM Win32_PerfRawData_Counters_ProcessorInformation WHERE Name="_Total"
18:26:24.446 [main] DEBUG oshi.hardware.platform.windows.WindowsCentralProcessor - Initialized Processor
18:26:24.687 [main] ERROR org.deeplearning4j.util.CrashReportingUtil - >>> Out of Memory Exception Detected. Memory crash dump written to: G:\ACTIVESEGMENTATION-testBranch\dl4j-memory-crash-dump-1641214573133_1.txt
18:26:24.688 [main] WARN org.deeplearning4j.util.CrashReportingUtil - Memory crash dump reporting can be disabled with CrashUtil.crashDumpsEnabled(false) or using system property -Dorg.deeplearning4j.crash.reporting.enabled=false
18:26:24.688 [main] WARN org.deeplearning4j.util.CrashReportingUtil - Memory crash dump reporting output location can be set with CrashUtil.crashDumpOutputDirectory(File) or using system property -Dorg.deeplearning4j.crash.reporting.directory=<path>
Exception in thread "main" java.lang.OutOfMemoryError: Cannot allocate new LongPointer(4): totalBytes = 560, physicalBytes = 7236M
	at org.bytedeco.javacpp.LongPointer.<init>(LongPointer.java:88)
	at org.bytedeco.javacpp.LongPointer.<init>(LongPointer.java:53)
	at org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.createShapeInfo(NativeOpExecutioner.java:2016)
	at org.nd4j.linalg.api.shape.Shape.createShapeInformation(Shape.java:3247)
	at org.nd4j.linalg.api.ndarray.BaseShapeInfoProvider.createShapeInformation(BaseShapeInfoProvider.java:68)
	at org.nd4j.linalg.api.ndarray.BaseNDArray.<init>(BaseNDArray.java:180)
	at org.nd4j.linalg.api.ndarray.BaseNDArray.<init>(BaseNDArray.java:174)
	at org.nd4j.linalg.cpu.nativecpu.NDArray.<init>(NDArray.java:78)
	at org.nd4j.linalg.cpu.nativecpu.CpuNDArrayFactory.create(CpuNDArrayFactory.java:409)
	at org.nd4j.linalg.factory.Nd4j.create(Nd4j.java:4033)
	at org.nd4j.linalg.api.shape.Shape.newShapeNoCopy(Shape.java:2123)
	at org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.preOutput(ConvolutionLayer.java:477)
	at org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.activate(ConvolutionLayer.java:505)
	at org.deeplearning4j.nn.graph.vertex.impl.LayerVertex.doForward(LayerVertex.java:110)
	at org.deeplearning4j.nn.graph.ComputationGraph.ffToLayerActivationsInWS(ComputationGraph.java:2135)
	at org.deeplearning4j.nn.graph.ComputationGraph.computeGradientAndScore(ComputationGraph.java:1372)
	at org.deeplearning4j.nn.graph.ComputationGraph.computeGradientAndScore(ComputationGraph.java:1341)
	at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:174)
	at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:61)
	at org.deeplearning4j.optimize.Solver.optimize(Solver.java:52)
	at org.deeplearning4j.nn.graph.ComputationGraph.fitHelper(ComputationGraph.java:1165)
	at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1115)
	at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1082)
	at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1018)
	at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1006)
	at activeSegmentation.deepLearning.UNet1.run(UNet1.java:111)
	at activeSegmentation.deepLearning.UNet1.main(UNet1.java:287)

Caused by: java.lang.OutOfMemoryError: Physical memory usage is too high: physicalBytes (7236M) > maxPhysicalBytes (6104M)
	at org.bytedeco.javacpp.Pointer.deallocator(Pointer.java:700)
	at org.bytedeco.javacpp.Pointer.init(Pointer.java:126)
	at org.bytedeco.javacpp.LongPointer.allocateArray(Native Method)
	at org.bytedeco.javacpp.LongPointer.<init>(LongPointer.java:80)
	... 26 more
```

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [January 3, 2022, 12:45pm UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/4 "2022-01-03T12:45:12Z")

</div>

@Purva-Chaudhari that doesn’t look related to anything related to memory. A NoSuchMethodError is a standard problem that typically comes up with java versions clashing. We would still need more information to go on on what actually causes the crash.

As I mentioned before, I think it’s related to your computer running out of memory. Generally when it comes to running out of memory you will want to limit batch sizes. std::bad\_alloc is a c++ error that says that the kernel can’t allocate anymore memory. Due to our usage of c++ code that would be my first guess.

So for now try to do anything that will reduce your memory foot print and ensure you have enough RAM to actually train your model. Monitor the output and the cpu/ram usage of your computer first.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [January 3, 2022, 12:57pm UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/5 "2022-01-03T12:57:26Z")

</div>

@Purva-Chaudhari  
The error you share here is entirely unrelated to the one you initially shared.

When sharing your error messages, please share them _fully_. If the error you show here happens in addition to the error that you’ve shared partially in your initial post, then please say so _and also add_ the full error that contains the hint that it is about memory allocation.

So as @agibsonccc said, we can’t really help you all that much with the limited information. All we can do is speculate.

---

<div class="post-metadata">

**Author:** ![Purva-Chaudhari](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/purva-chaudhari/32/702_2.png) [@Purva-Chaudhari](https://community.konduit.ai/u/Purva-Chaudhari)\
**Post date:** [January 3, 2022, 1:04pm UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/6 "2022-01-03T13:04:45Z")

</div>

Sorry I updated the output above  
It looks like my memory is running out. I had a very small dataset (5 train with ground truths and 5-validation and ground truths). So my batch size was small. I see the `Out of Memory Exception Detected.`

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [January 3, 2022, 1:09pm UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/7 "2022-01-03T13:09:18Z")

</div>

@Purva-Chaudhari read this page: [Memory - Deeplearning4j](https://deeplearning4j.konduit.ai/multi-project/explanation/configuration/memory#memory-management-for-nd4j-dl4j-how-does-it-work)

I would double check also how big each image is. Convolution memory is fairly heavy when executing (the forward and backwards pass) despite what it looks like on disk. Depending on the size of your image you may need to shrink your batch size to 3 or lower.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [January 3, 2022, 1:13pm UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/8 "2022-01-03T13:13:09Z")

</div>

The other aspect here is `maxPhysicalBytes (6104M)`.

So despite you having 12GB of memory, you only allow your JVM to use about 6GB. The link @agibsonccc pointed you to will also tell you how to increase that.

---

<div class="post-metadata">

**Author:** ![Purva-Chaudhari](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/purva-chaudhari/32/702_2.png) [@Purva-Chaudhari](https://community.konduit.ai/u/Purva-Chaudhari)\
**Post date:** [January 3, 2022, 2:24pm UTC](https://community.konduit.ai/t/std-bad-alloc-error/1736/9 "2022-01-03T14:24:42Z")

</div>

Thank you very much @treo @agibsonccc , the resource helped ! I changed my vm memory options to use more bytes for maxPhysicalBytes.
