Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Serialization can extend the lifetime of objects, preventing their garbage collection. The ObjectOutputStream ensures that each object is written to the stream only once by retaining a reference (or handle) to each object written to the stream. When a previously written object is subsequently written to the stream again, it is replaced with a reference to the originally written data in the stream. Note that this substitution takes place without regard to regardless whether the object's contents have changed in the interim. This It requires a table of references to be maintained to keep track of previously serialized objects. This table of references prevents garbage collection of the previously serialized objects because the garbage collector cannot collect object instances referred to by live references.

...

This rule is a specific instance of the more general rule MSC05-J. Do not exhaust heap space.

...

As already described, the ObjectOutputStream maintains a cache of previously written objects. Consequently, all SensorData objects remain alive until the cache itself becomes garbage-collected. This can result in an OutOfMemoryError An OutOfMemoryError can occure because the stream remains open while new objects are being written to it.

Code Block
bgColor#FFcccc

class SensorData implements Serializable {
  // 1 MB of data per instance!
  ... 
  public static SensorData readSensorData() {...}
  public static boolean isAvailable() {...}
}

class SerializeSensorData {
  public static void main(String[] args) throws IOException {
    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream(
          new BufferedOutputStream(new FileOutputStream("ser.dat")));
      while (SensorData.isAvailable()) {
        // noteNote that each SensorData object is 1 MB in size
        SensorData sd = SensorData.readSensorData();
        out.writeObject(sd);
      }
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }
}

...

This compliant solution takes advantage of the known properties of the sensor data by resetting the output stream after each write. The reset clears the output stream's internal object cache; consequently, the cache no longer maintains references to previously written SensorData objects. The garbage collector can collect SensorData instances that are no longer needed.

Code Block
bgColor#ccccff

class SerializeSensorData {
  public static void main(String[] args) throws IOException {
    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream(
          new BufferedOutputStream(new FileOutputStream("ser.dat")));
      while (SensorData.isAvailable()) {
        // noteNote that each SensorData object is 1 MB in size
        SensorData sd = SensorData.readSensorData();
        out.writeObject(sd);
        out.reset(); // resetReset the stream
      }
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }
}

...

Memory and resource leaks during serialization can result in a resource exhaustion attack or can crash the JVMJava Virtual Machine.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

SER10-J

low

Low

unlikely

Unlikely

low

Low

P3

L3

Automated Detection

Detecting code that should be considered privileged or sensitive requires programmer assistance. Given identified privileged code as a starting point, automated tools could compute the closure of all code that can be invoked from that point. Such a tool could plausibly determine whether all code in that closure exists within a single package. A further check of whether the package is sealed is feasible.

ToolVersionCheckerDescription
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

JAVA.ALLOC.LEAK.NOTSTORED
JAVA.CLASS.UI

Closeable Not Stored (Java)
Inefficient Instantiation (Java)

Related Guidelines

MITRE CWE

CWE-400

.

, Uncontrolled

resource consumption

Resource Consumption (aka "

resource exhaustion

Resource Exhaustion")

 

CWE-770

.

, Allocation of

resources

Resources without

limits

Limits or

throttling

Throttling

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f4f937c3-b573-4b5f-ae41-0358b1c5d578"><ac:plain-text-body><!

[

CDATA[

[[API 2006

AA. References#API 06]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c3457fe4-ca46-45ca-87e0-bf30d950d69f"><ac:plain-text-body><![CDATA[

[[Harold 2006

AA. References#Harold 06]]

API 2014]


[Harold 2006]

Section 13.4,

Performance

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c364ac15-9c2c-4ec2-ad97-4771e9fece70"><ac:plain-text-body><![CDATA[

[[Sun 2006

AA. References#Sun 06]]

Serialization Specification

]]></ac:plain-text-body></ac:structured-macro>

"Performance"

[Sun 2006]

Serialization Specification


...

Image Added Image Added Image Removed      13. Serialization (SER)      Image Modified