NetBeans を使用してアプリケーションを実行しており、プロジェクトのプロパティで最大 JVM ヒープ領域を 1 GiB に設定しています。
それでも、アプリケーションはメモリ不足でクラッシュします。
JVM のメモリはシステムに格納されていますか? もしそうなら、そのメモリをクリアする方法は?
NetBeans を使用してアプリケーションを実行しており、プロジェクトのプロパティで最大 JVM ヒープ領域を 1 GiB に設定しています。
それでも、アプリケーションはメモリ不足でクラッシュします。
JVM のメモリはシステムに格納されていますか? もしそうなら、そのメモリをクリアする方法は?
プロファイラーを使用してコードを分析することをお勧めします。Netbeans には優れたプロファイラーがあります。これにより、アプリケーションでメモリが占有されている場所が表示され、問題がどこにあるのかがわかります。
JVM は、メモリが不足する前に可能な限りオブジェクトをガベージ コレクションします。それか、あなたのアプリケーションは本当に多くのメモリを必要とするアプリケーションです - しかし、特にアプリケーションを長時間実行した後にのみ問題が発生する場合は、コードのバグである可能性がはるかに高いと思います.
あなたの質問のすべての詳細を完全には理解していませんが、重要な部分は理解できると思います。
OutOfMemoryError
JVM に割り当てられたメモリが、プログラムで作成されたオブジェクトに対して十分でない場合、(例外ではありません) がスローされます。あなたの場合、使用可能なヒープ領域を 1 GB 以上に増やすと役立つ場合があります。1 GByte で十分だと思われる場合は、メモリ リークが発生している可能性があります (Java では、不要になったオブジェクトへの参照があることを意味する可能性が最も高いです。おそらく何らかのキャッシュにあるのでしょうか?)。
Java reserves virtual memory for its maximum heap size on startup. As the program uses this memory, more main memory is allocated to it by the OS. Under UNIX this appear as resident memory. While Java programs can swap to disk, the Garbage Collection performs extremely badly if part of the heap is swapped and it can result in the whole machine locking up or having to reboot. If your program is not doing this, you can be sure it is entirely in main memory.
Depending on what your application does it might need 1 GB, 10 GB or 100 GB or more. If you cannot increase the maximum memory size, you can use a memory profiler to help you find ways to reduce consumption. Start with VisualVM as it is built in and free and does a decent job. If this is not enough, try a commercial profiler such as YourKit for which you can get a free evaluation license (usually works long enough to fix your problem ;)
The garbage collector automatically cleans out the memory as required and may be doing this every few seconds, or even more than once per second. If it is this could be slowing down your application, so you should consider increasing the maximum size or reducing consumption.
@camobap が述べたように、OutOfMemory の理由は、Perm Genのサイズが非常に低く設定されていたためです。これで問題は解決しました。
回答とコメントをありがとうございました。
The Java compiler doesn't allocate 1 GiB as I think you are thinking. Java dynamically allocates the needed memory and garbage collects it too, every time it allocates memory it checks whether it has enough space to do so, and if not crashes. I am guessing somewhere in your code, because it would be near impossible to write code that allocates that many variables, you have an array or ArrayList that takes up all the memory. In the case of an array you probably has a variable allocating the size of it and you did some calculation to it that made it take too much memory. In the case of an ArrayList I believe you might have a loop that goes too many iterations adding elements to it.
Check your code for the above errors and you should be good.