Serialization can extend the lifetime of objects, consequently preventing garbage collection of those objects. 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 whether the object's contents have changed in the interim. This table of references prevents garbage collection of the previously - written objects because the garbage collector cannot collect live references.
...
As already described, the ObjectOutputStream
maintains a cache of previously - written objects. Consequently, all SensorData
objects remain alive until the cache itself becomes garbage. This can result in an OutOfMemoryError
, because the stream remains open while new objects are being written to it.
...
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 is able to collect SensorData
instances that are no longer needed.
...
Related Guidelines
CWE ID -400, "Uncontrolled Resource Consumption (aka 'Resource Exhaustion')" | |
| CWE ID -770, "Allocation of Resources Without Limits or Throttling" |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7ee1519663140414-cc64c668-4a2a42f6-9dc287d7-908e928c1a21a98f0f6b7405"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b37db8c6fcfece14-f6591169-41324553-ae0ca478-eed826d51fc156d4da8c4c9b"><ac:plain-text-body><![CDATA[ | [[Harold 2006 | AA. Bibliography#Harold 06]] | 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="bee3f75a3ea45fc9-181d4e8a-4cfb4e77-b3659c21-5b41616a078bef731b7b171c"><ac:plain-text-body><![CDATA[ | [[Sun 2006 | AA. Bibliography#Sun 06]] | "Serialization specification" | ]]></ac:plain-text-body></ac:structured-macro> |
...