0

If My process allocates some big memory and then deallocates, would top or gnome-system-monitor show that my memory usage of that process decreased ? or kernel will still reserve that memory for that process ?

What I see is I am deallocating memory. But I still see gnome-system-monitor displaying growing memory for my program. I don't find memory leak in my end. I want to know whether its not displaying released memory ? or there is really a memory leak at my end ?

user59088
  • 109
  • 1
  • 7
  • This depends on why the memory was allocated / what you're doing with it / whether it is actually `free()`'d -- If you're talking about a program written in Java or an interpreted language (Perl, Ruby, Python, etc.) there's no way of knowing what's going on under the hood... – voretaq7 Nov 21 '12 at 23:11

1 Answers1

1

Your question seems to be based on a confusion between physical and virtual memory. Normal memory allocations never reserve physical memory -- the kernel is always free to use physical memory where it does the most good. And since virtual memory is cheap, there is no reason to care whether it stays reserved or not.

Typically, virtual memory will stay reserved, because there's no point in going to the effort to return it. Physical memory was never reserved in the first place, so there's nothing to do.

David Schwartz
  • 31,449
  • 2
  • 55
  • 84
  • If you meant swap by virtual memory . No I am talking about Physical memory. – user59088 Nov 21 '12 at 16:26
  • 1
    No, not swap, virtual memory -- address space. But since your talking about physical memory, the answer is very simple: The kernel is *always* free to change how physical memory is used, no matter what the process does. – David Schwartz Nov 21 '12 at 16:28
  • What I see is I am deallocating memory. But I still see gnome-system-monitor displaying growing memory for my program. I don't find memory leak in my end. I want to know whether its not displaying released memory ? or there is a memory leak in my end ? – user59088 Nov 21 '12 at 16:28
  • @user59088: It's displaying virtual memory -- mere address space. The physical memory can still be moved to another process or use if necessary. The harm of a memory leak has nothing to do with wasting physical memory. So I think you're asking the wrong question. – David Schwartz Nov 21 '12 at 16:30
  • On a typical modern OS, if you allocate 1MB and never free it, all that happens is 1MB of address space in your process is reserved. If a physical page gets mapped there and you don't access it, the physical page will just get unmapped. So the harm of memory leaks is *not* that they waste physical memory. – David Schwartz Nov 21 '12 at 16:32
  • Okay fine. so should I see that virtual memory usage to decrease immediately when my process deallocates ? – user59088 Nov 21 '12 at 16:31
  • @user59088: No. Because virtual memory is effectively almost free, processes often don't bother to return it. However, if you know for a fact that you were supposed to be returning the virtual memory, *then* the lack of a reduction in virtual memory consumption would indicate a memory leak. – David Schwartz Nov 21 '12 at 16:33
  • se here when I see gnome-system-monitor's memory tab's memory usage information is not decreasing when I release memory. doesn't necessarily mean that I've a memory leak ? right ? – user59088 Nov 21 '12 at 16:34
  • Yes, that is what @DavidSchwartz has been saying multiple times. – gparent Nov 21 '12 at 16:36
  • Thanks. Is this same behaviour in windows as well ? – user59088 Nov 21 '12 at 16:36
  • Pretty much. Windows machines tend to be more virtual memory constrained than Linux machines and Windows programs tend to be more aggressive about returning address space. But really, on any modern OS, you have to look at the memory stats and think about what that means applying some knowledge of the internal structure of the program. Otherwise, go by resident set size measurements, since those measure physical memory. – David Schwartz Nov 21 '12 at 17:07