﻿# Closing a FITS handle need not release every memory-mapped data reference

Closing a FITS handle need not release every memory-mapped data reference. A focused scientific-computing guide with the documented mechanism, a reproducible validation scenario, evidence and operational limits.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/science-astropy-fits-memmap-borrowed-data-lifetime/
Language: en

Closing an Astropy FITS HDUList does not necessarily release every operating-system handle associated with memory-mapped array data. A retained array reference can keep the mapping alive. A scientific AI loader that interprets the end of a file context as the end of all data ownership can accumulate resources or hand asynchronous work a lifetime contract it never documented.

## The scientific interface

The FITS guide explains that accessing memmapped HDU data opens an additional underlying handle that can remain after hdul.close while array references exist. This permits mapped data access but means file-list closure is not a universal resource-release barrier. A regular copied array has a different ownership and memory-cost profile. The effective route can also depend on the file and selected reading options, so an application should observe its actual mapped-versus-copied path rather than infer it solely from the requested flag.

## Implementation details

Define whether a pipeline stage borrows mapped data or receives an explicit owned copy. Preserve the required owner until all consumers finish, or detach intentionally with a reviewed copy and memory budget. Release all retained mapped references under the actual object lifecycle rather than assuming one close call controls every alias. Bound prefetching so many queued scientific images cannot retain an unbounded number of mappings. Keep process-specific handle observations separate from physical memory measurements, since address-space mapping, resident pages and copied arrays describe different resources.

## A constructed validation case

Use a controlled FITS file and retain a data reference beyond HDUList closure under the documented mapped route. Observe access and resource ownership, then release the references according to the chosen lifecycle. Compare with an explicitly copied-data path and an asynchronous consumer. Acceptance requires truthful ownership and bounded resource retention, not a promise that every platform reports identical handles or permits identical file operations while mappings exist. Preserve actual reading options and file characteristics so the fixture exercises the intended path rather than an accidental eager copy.

## Numerical and data limits

This is a data-ownership check, not a storage durability or astronomical-content validation. Keep it before GPU transfer and model batching so mapping lifetime problems are not mislabeled as image corruption, inference instability or unexplained memory requirements of the scientific model.

## Primary references

[Astropy FITS memory-mapped data](https://docs.astropy.org/en/stable/io/fits/index.html); [Astropy FITS file opening and lifecycle](https://docs.astropy.org/en/stable/io/fits/api/files.html).

This is a proposed scientific-interface validation, not an executed Retrivium benchmark. Preserve the implementation version and unresolved outcomes; interface documentation does not establish the accuracy or performance of a scientific model.

## Related reading

[Validate safetensors weight artifacts separately from repository code](/journal/safetensors-artifact-validation/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
