Hi, I have noted this behavior when I wrote a program that has to save data on a file on a microcontroller with an Embedded Linux Operated System (ARM based, only 64RAM) and I checked also on a system with Ubuntu 10.04. To understand the problem I save about 4Mb on a file every 100ms. The memory of the process remains stable, but if I look to the total memory of the system (with vmstat 1) the free memory decrease continuously. Is it normal this behavior?
This is my code:
It can be. Where is the free memory going? Is the buffer cache increasing? The buffer cache is the page cache used by the kernel to store off disk pages so that if they are later needed, they can be loaded directly from RAM rather than accessing the physical disk (orders of magnitude slower).
When you say "Memory" are you refering to the physical RAM? Or the Disk space? Some people don't make the distiction and I wanted to make sure we are all on the same page.
If you mean the free disk space is decreasing, it's because you have fopen set to append data to the end of the file.
Normal behavior. The data you are writing (to disk) is being written to the page cache as well, and the kernel will expand the page cache as long as there is memory available (to a certain point).
Have you ever wondered why opening of firefox takes longer for the first time? Next time onwards it is fast. The reason is, even if you close the program, the image of the hard disk still persists in the RAM.
I have seen it quite often with my program. My program is a movie processor which reads a movie frame by frame from the disk. But as soon as the processing of a frame is over, it frees the memory and loads the next frame. So memory consumption should be at most one frame. However the OS continues to keep all read frame in the RAM till it is full. I am having 2GB RAM in my computer. And the kernel goes on caching till it is full. If I put 4GB, then it will go up to 4GB. I don't know where it ends. I have to test it on a workstation having 32GB RAM and running 64bit linux.
However the OS continues to keep all read frame in the RAM till it is full. I am having 2GB RAM in my computer. And the kernel goes on caching till it is full. If I put 4GB, then it will go up to 4GB. I don't know where it ends. I have to test it on a workstation having 32GB RAM and running 64bit linux.
If you don't mind I'd like to see your code in a new thread krishnendu, this shouldn't happen if you're using destructors and\or delete commands properly. A modern OS will only give an application more RAM if it hits a page fault, this occurs when the application has or is about exceed it's allocated memory. If you are destroying objects there by releasing the memory and allowing the system to designate the addresses of your variables then a page fault should never occur.
As for the "Image persisting in RAM" bit, there is a periodic process called a "Garbage Collector" that cleans this stuff up. When a thread does not have any open handles to it then the memory space for that thread is marked as avalible for the OS. The OP is seeing their issue because it is occuring at run time, the "Garbage Collector" will not release the memory because it is still in use.
I have not said that my program is using the memory. If you run 'top' in Linux, it tells you about the total available memory, memory used as a whole (i.e. both by the OS and applications) and free memory. It is the 'used memory' which keeps on increasing. The 'top' command also shows the memory used by an individual process. There I can see that the memory used by my program is not increasing. Any way try the following code.