In C++ and C# when new not able to allocate enought memory it throws exception.
I couldn't find any information about new's behavior in Java. So what will happen if new fails in Java (not enough memory)?
What happens when new fails? Explanation: While creating new objects, the new operator may fail because of memory errors or due to permissions. At that moment the new operator returns zero or it may throw an exception. The exception can be handled as usual.
By default, when the new operator is used to attempt to allocate memory and the handling function is unable to do so, a bad_alloc exception is thrown. But when nothrow is used as an argument for new, and it returns a null pointer instead.
The ordinary form of new will never return NULL ; if allocation fails, a std::bad_alloc exception will be thrown (the new (nothrow) form does not throw exceptions, and will return NULL if allocation fails).
Answer: When we allocate memory from heap dynamically in a C++ program using new operator, the program crashes when memory is not available, or the system is not able to allocate memory to a program, as it throws an exception.
Assuming you specifically mean failure of the memory allocation, then it should throw OutOfMemoryError
Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
Like all subclasses of Error
, it's usually a non-recoverable condition, even if technically you can catch it:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.
A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.
When Java cannot get enough memory to allocate an object you'll get an OutOfMemoryError.
In practice the exception can take quite a while to be actually thrown by the JVM. When faced to memory issue, the JVM will first try to garbage as much memory as possible. Depending on the JVM configuration (GC parameters and max heap memory), a GC cycle can take several seconds to several minutes with Xmx set to several gigabytes. Even worse, depending on the memory needed, the JVM can perform several GC cycles before throwing the exception.
When the exception is throw, it is processed as any uncaught exception. As such it will propagate to the top of the calling stack of the thread where the exception was raised. As the exception is uncaught, the thread will display a stacktrace on System.err
before dying. That's all. In a mono-threaded program this'll cause the program to exit. On a multi-threaded program, this thread death can free enough memory for the program to keep on running in an unstable configuration.
My recommendation if you're concerned about memory issue is that you should register and UncaughtExceptionHandler to kill your program when a memory issue arises as it is certainly better to stop your program than letting it working in an undefined state without anyone knowing.
You can read the following articles from Heinz Kabutz on the subject:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With