# Memory consumption problem when reading png images with ImageImportExportDumpImageReadPNG

For importing png images I use often:

image = First@ImageImportExportDumpImageReadPNG[fileName]
(see: ?**PNG*)


since this is faster than:

image = Import[fileName,"PNG"]


(see for comparison here, the answer of MarcoB)

But the problem is: When the filename is varying (different images names) and I want to read in many png images with ImageImportExportDumpImageReadPNG[fileName] then the memory consumption increases endlessly.

Finally at a certain point (depending on image size and available memory) the execution of the notebook stops without any error or warning.

What could be the reason for that? Please run the code below and observe how the used memory increases.

This "problem/error" does not occur with Import[filename,"PNG"].

Example code:

SetDirectory[$TemporaryDirectory]; numberImages = 20000; Do[ imageData = RandomReal[{0, 1}, {2000, 2000}]; fileName = StringJoin[RandomChoice[{"a", "b", "c"}, 20], ".png"]; fullFileName = StringJoin[$TemporaryDirectory, "\\", fileName];

image = Image[imageData];

Export[fullFileName, image, "PNG"];

image = First@ImageImportExportDumpImageReadPNG[fullFileName];

If[Mod[i, 100] == 0, Print[i]];

, {i, 1, numberImages}

];


Im am running Mathematica 11.3 on Windows 10 Pro 64bit.

ImageImportExportDumpImageReadPNG[fileName] uses a cache. You should call ImageImportExportDumpDeleteCachePNG[]. To improve the performance of Import you can use Import["file.png", IncludeMetaInformation->None]`.