[TIMOB-19981] iOS: FileStream Significant memory leak while reading/writing files using FileStream
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Invalid |
Resolution Date | 2015-12-07T22:48:26.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | iOS |
Labels | TCSupportTriage, ios, memory |
Reporter | Fernando Manzano |
Assignee | Pedro Enrique |
Created | 2015-01-08T22:21:19.000+0000 |
Updated | 2017-03-22T21:46:32.000+0000 |
Description
PROBLEM:
When you want to read or write a file using FileStream.read(buffer) and FileStream.write(buffer), the memory used to store the data read/written is leaked, leading to a crash after writing/reading files for a few minutes.
TEST CASE:
This is how the file is read:
while (true) {
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "image.png");
var fileStream = file.open(Ti.Filesystem.MODE_READ);
var buffer = Ti.createBuffer({
length: 1024 * 10
});
while (fileStream.read(buffer) > 0) {}
file = null;
fileStream.close();
fileStream = null;
buffer.release();
buffer = null;
}
This is how the file is written:
while (true) {
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "output.txt");
var fileStream = file.open(Ti.Filesystem.MODE_WRITE);
var buffer = Ti.createBuffer({
value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut nisi felis, ultrices nec blandit vitae, efficitur a lacus. Nunc nec neque eleifend, aliquam leo id, scelerisque dolor. Suspendisse potenti. Pellentesque feugiat volutpat dictum. Morbi et massa venenatis lectus sodales maximus. In id ornare justo. Duis a nulla tincidunt, laoreet leo nec, aliquam ex. Nunc condimentum tellus justo, non efficitur magna viverra sit amet. Sed lectus neque, placerat non posuere in, faucibus quis nisl. Aliquam lobortis est eget felis varius, a placerat ex interdum. Aenean sit amet tempor turpis. Fusce at faucibus nisi. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Phasellus lacus enim, efficitur eget egestas vitae, viverra vel neque. Donec orci lacus, consectetur sit amet placerat vel, pharetra nec odio. Quisque tempus nulla diam, id aliquet tellus pharetra et. Pellentesque id urna non nisi commodo aliquam. Praesent nec porttitor metus, ac finibus purus. Curabitur vel magna libero. Duis cursus diam sapien, eu finibus erat rutrum sed. Fusce viverra dictum erat cursus finibus. Maecenas lobortis neque libero, et porttitor tortor tincidunt eu. Mauris commodo interdum urna ac egestas. Fusce vehicula egestas risus et efficitur. Suspendisse nisl ex, sagittis nec neque vitae, malesuada vulputate justo. Vivamus aliquet lorem tellus, et convallis felis sollicitudin a. Vestibulum non lacus ut metus placerat feugiat vel sit amet nunc. Pellentesque vestibulum, turpis nec feugiat tincidunt, arcu lorem iaculis felis, eget mattis arcu nunc non mi. Phasellus eros erat, condimentum nec varius ac, eleifend non tortor. Nunc pellentesque purus non lorem ultrices rhoncus. Integer in efficitur dolor. Aenean laoreet et dolor placerat accumsan."
});
var bytesWritten = 0;
while (bytesWritten < 1024*1014) {
bytesWritten += fileStream.write(buffer);
}
file = null;
fileStream.close();
fileStream = null;
buffer.release();
buffer = null;
}
In both cases the heap grows until the app crashes. I've attached an image which shows how the heap grows.
Attachments
File | Date | Size |
---|---|---|
instruments-heap-grow.png | 2015-01-08T22:21:19.000+0000 | 280639 |
I do not see a bug here, the code is not realistic and it is creating a memory leak. Let me explain; by creating a
while(true)
you're not allowing the javascript engine to do its garbage collection, and thus creating a memory leak, infinite loop, etc. A better test case would be to create asetInterval
insteadI tested this and the allocation in instruments do not go up.
Closing ticket as invalid with reference to previous comments.