I worked on my school project. I used malloc quite badly without free. Since then my computer has been much slower and I am afraid that memory leaks could be the reason for it. I use Ubuntu 20.04.
I tried to restart it but it is still slow many days after it.
Can I somehow check if memory leaks are the problem and solve it?
Your computer might slow down if your program is still running and it has some big memory leaks, but I don't believe that it would affect your computer when the program is shut down and it ends the execution.
When the program exits and finishes the execution, all the memory is freed by the kernel implicitly. That mempory will be used next time your computer needs to allocate new memory.
In our project we are loading Lua as a dll in a Windows service (32 bit process on X64 Windows servers) and running scripts in parallel on different threads. It all works fine most of the times however, in cases where the script is doing memory intensive task (such as loading a large file and iterating through it while doing other processing) the script throws "not enough memory" error.
As per my understanding of the Windows OS's memory model, if the "not enough memory" error is being thrown it is because the process has crossed its memory quota (2 GB or if large memory aware linker option is set then 4GB) as there is no thread specific memory quota in Windows. But there are few points that I am not able to understand:
If there are multiple threads running Lua scripts and all are sharing the same address space of the process then why the "not enough memory" coming only on the thread which is doing heavy memory operation, it could come on any other script also, but it doesn't happen in practice?
Could this issue be related to lua Stack that is used to interact with c code?
Is there any internal memory limit that lua maintains (I couldn't find any reference of this btw).
Few points about our env:
It is a legacy product and uses Lua 5.1
There is no custom allocation function so the code use realloc for memory allocation
Any pointer in the direction why this could happen would be helpful.
Thanks in advance!
Just in case someone else hits this : there is no memory limit in lua itself, the issue was happening because the process's address space was getting filled up completely because we were loading huge maps in memory. We fixed it by moving in memory data to disc (local db).
I'm working with embedded linux development, and we're currently having some trouble with some memory page allocation faults, which led me to believe we have a leak somewhere.
Currently, I am trying to cross compile valgrind to use on our system, but I'm losing my faith on this solution because of the sheer amount of memory valgrind will use up (we have serious memory restrictions).
This has made me wonder: is there any way of hunting for a memory leak without valgrind or with a valgrind-like application with minimal memory usage? Creating wrappers for malloc() and free() is out of question.
Also, the test that caused the allocation failures was a simple stress test of copying a file n times and checking its md5sum, in case anyone is curious.
I'm using the Linaro toolchain for cross compiling, glibc 2.15, and the system is set up without a swap partition. The system has around 64MB of RAM, making valgrind, or any other memory intensive application a tad difficult to use.
Regards,
Guilherme
Since you are using glibc, you should have its built-in memory-tracing support available to you. Your program would enable this by calling mtrace(3) at or near startup. mtrace() installs hook functions into the memory allocator to log allocations and deallocations, under runtime control via environment variable MALLOC_TRACE.
You probably also want to be aware of mtrace(1), a Perl script for interpreting log files produced by the mtrace facility.
This facility traces only allocations and deallocations, which is much less than Valgrind does. Nevertheless, those are the main items of interest when you are looking for a memory leak.
There are many memory leak bug in our company's codes and normally our solution is "Reading the Codes" though we have tools to found memory leak's position. So I am wondering is memory leak unavoidable in C or it is not worth to do garbage collection to sacrifice system's performance.
It is always possible to avoid memory leaks, it's just that it can be difficult to do so when doing manual memory management. As programs grow complex it becomes harder to do memory management correctly. That is why you see many larger project implement some kind of automatic or semi automatic memory management. For instance GCC has a garbage collector, as has open source web browsers like Firefox and Chrome (I'm sure the closed source web browsers has it as well but it's not so easy to tell).
It is important to not that automatic memory management does not remove all memory leaks. Data can still be retained unnecessarily. But automatic memory management makes things easier and helps avoid errors like freeing memory twice or referencing already freed memory.
So, I've recently noticed that our development server has a steady ~300MB out of 4GB ram left after the finished development of a certain project. Assuming this was due to memory leaks during the development phase, will that memory eventually free itself up or will it require a server restart. Are there any tools that can be used to prevent this in the future (aside from the obvious, 'don't write code that produces memory leaks')? Sometimes they go unseen for a little while and over time I guess they add up as you continue testing your app.
What operating system are you running? Most operating systems these days will clean up leaked memory for a process when the process exits. It is possible that the memory you are seeing in use is actually being used for the filesystem cache. This is nothing to worry about -- the OS will reclaim this memory if necessary.
From: http://learnlinux.tsf.org.za/courses/build/internals/ch05.html
The amount of free memory indicated by
the free command includes the current
size of the buffer cache in its
calculation. This is misleading, as
the amount of free memory indicated
will often be very low, as the buffer
cache soon fills most of user memory.
Don't' panic. Applications are
probably not crowding your RAM; it is
merely the buffer cache that is taking
up all available space. The buffer
cache counts as memory space available
for application use (remembering that
it will be shrunk as required), so
subtract the size of the buffer cache
to see the real amount of free memory
available for application use
It's best to fight them during development, because then it's easier to identify the revision that introduces the leak. As you probably see now, doing it after the fact is very, very hard. Expect a lot of reports when running the tools I recommend below:
http://valgrind.org/
http://www.ibm.com/software/awdtools/purify/
http://directory.fsf.org/project/ElectricFence/
I'd suggest you to run this tools, suppress most warnings about leaks, and then fix them one by one, removing the suppresions.
And then, make sure you regularly run these tools and quickly fix any regressions!
Of course the obvious answer is "Don't write code that produces memory leaks" and it's a valid one, because they can be extremely hard to fix if you have reference counting issues, or complex code in which it's hard to track the lifetime of memory.
To address your current situation you might consider using a tool such as DevPartner for Windows, or Valgrind for Linux/Unix, both of which I've found to be very effective for tracking down memory leaks (as well as other issues such as performance bottlenecks).
Another thing you may wish to consider is to look at your use of pointers and slowly replace them with smart pointers if you can, which should help manage your pointer lifetimes.
And no, I doubt that memory is going to be recovered without restarting the process in which your code is running.
Run the program using the exceptional valgrind on Linux x86 boxes.
A commerical equivilant, Purify, is available on Windows.
These runtime analysis of your program will report memory leaks and other errors such as buffer overflows and unitialised variables.
Static code analysis - Lint and Coverity for example - can also uncover memory leaks and more serious errors.
Lets be specific about what memory leaks cause and how they harm your program:
If you 'leak' memory during operation of your program there is a risk that your application will eventually exhaust RAM and swap, or the address space of available to your program (which can be less than physical RAM) and cause the next allocation to fail. The vast majority of programs will fail to catch this error, as error checking is harder than it seems. The majority of programs will either fail by dereferencing a null pointer or will exit.
If this is on Linux, check the output of 'free' and specifically check the amount of 'cached' ram. If your development work includes a lot of disk I/O, it'll use it for caching files, and you'll see very little 'available' but it's still there if it's needed. For all practical purposes, consider free+cached as available.
The 'free' output is distilled from /proc/meminfo, and you can get more detailed information on the running process in /proc/$pid/{maps,smaps}
In theory when your process exits, any memory it had is released. Is your process exiting?
Don't assume anything, run a memory profiler over it and see what it's doing.
When I was at college we used the Borland C++ Builder 6 IDE
It included CodeGuard, which checks for memory leaks and other memory related issues.
I am not sure if this option is still available on newer versions, but it would be weird for a new version to have less features.
On linux, as mentioned before, valgrind is a good memory leak debugger.