Is garbage collection performed after an OutOfMemoryError is thrown in java?

It would seem that it should be so. But can anyone confirm or deny this?

Related to this:

Capture java.lang.OutOfMemoryError?

Is it possible to throw an exception from memory in java?

+4
source share
4 answers

Is garbage collection performed after an OutOfMemoryError is thrown in java?

It certainly works up to which OOME throws. In fact, OOME is usually thrown away because the garbage collector discovers that it cannot return enough space to satisfy distribution request 1 .

Whether it is executed after the one that OOME has chosen depends on what the application does. If an application tries to continue, the GC will usually start the next time the application asks for more memory ... as it continues to run.

1 - In fact, you can configure the GC to throw OOME when it detects that it spends too much time collecting garbage. In this case, the JVM may have a useful amount of unallocated memory in the hand.


Aaron Digullah says the following:

So, in a carefully designed application, you can catch and process OOME, and the program will survive and continue to work.

This is true, but this is not something you usually should do for two reasons.

The first reason is that OOME can be thrown anywhere where the thread is trying to allocate memory. Regardless of what the JVM was doing at that time, it will be terminated ... until the moment when OOME is caught. For instance:

  • If the stream was in the middle of updating the general data structure (under a lock), the data structure will be left half updated.

  • If the thread was supposed to notify of some other thread, this notification will never happen, and the other thread will wait.

  • If the thread does not catch OOME, it will be terminated, and if it does not notice anything, then you will potentially remain with an application that no longer works.

The problem is that these β€œbreakdowns” are difficult to detect or predict and difficult to repair.

The second reason is that OOME usually points to one of the following things:

  • You tried to perform a calculation with insufficient heap memory. If you try to recover from OOME, chances are you will run into the same problem again.

  • Your application has a memory leak; those. some data structure in your application supports references to garbage objects and prevents them from being recovered. Most likely, if you try to recover from OOME, nothing will change, and you will encounter the same problem again, and again and again.

So, the prerequisites for a successful recovery are as follows:

  • to know that OOME cannot damage anything in the JVM, which is important, AND
  • know that you will no longer run OOME again ... because the underlying reason still exists.

These are difficult prerequisites to satisfy ... in most applications. If they are not completed, then there is a good chance that an attempt to restore from OOME will lead to a deterioration if you exit and restart the application.

+5
source

Yes. GC works right before OutOfMemoryError and continues to work.

OOME is a bug like any other: it means that the runtime may be in a problem state, but that does not stop Java. Therefore, when you catch an error, you can delete some links and the error will disappear.

The problem, of course, is that you cannot know if the memory of another code might be needed (for example, in a different thread) when you try to find links for cutting, and this code can throw another OOME and break fatally.

So, in a carefully designed application, you can catch and process OOME, and the program will survive and continue to work.

+4
source

If your question is valid, will it work after OutOfMemoryError : yes. He also works earlier. OutOfMemoryError does not interrupt the JVM; instead of blocking the selection of the object, the program continues. The JVM continues, including garbage collection.

In fact, some structures such as Tomcat for this. They allocate a bit of memory that is not in use, and in the case of OutOfMemoryError release it to have enough space to complete and orderly shutdown. This requires the GC to continue to work.

+2
source

In javadoc OOME, it says:

It is thrown when the Java virtual machine cannot allocate an object because it has lost memory and there can be no more garbage collector.

This means that it was executed before the error.

By the way, when you get such an error, it is very difficult to repair, because you do not guarantee enough memory to do anything else. If you used the last bytes of memory that you stuck. If you had this error because you tried to allocate 4Gb in one shot, you still have a chance to save your soul, but if you need these 4Gb, then you are stuck again.

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/OutOfMemoryError.html

0
source

Source: https://habr.com/ru/post/1447680/


All Articles