A Java OutofMemoryError
occurs if the program attempts to use more heap space than what is available. Among other causes, this error may result from
- a memory leak. See MSC06-J. Avoid memory leaks for information on preventing memory leaks.
- an infinite loop
- limited amounts of default heap memory available
- incorrect implementation of common data structures (hash tables, vectors and so on)
- unbound deserialization.
- writing a large number of objects to an
ObjectOutputStream
. For more information, see SER12-J. Avoid memory and resource leaks during serialization. - creating a large number of threads
- uncompressing a file. See IDS22-J. Limit the size of files passed to ZipInputStream for example.
Some of these causes are platform-dependent, and difficult to anticipate. Others are fairly easy to anticipate, such as reading data from a file. Therefore, programs shall not accept untrusted input in a manner that can cause the program to exhaust memory.
Noncompliant Code Example (readLine()
)
...