What is a Memory Leak?
A Memory Leak is a displacement of system resource in a computer application because of some faulty memory allocation. It occurs when an unused RAM location remains to be unreleased. Take note that a memory leak is not the same as a space leak which refers to a program that uses more RAM than needed. A memory leak in Windows 10 takes place when the memory is just not accessible even though it is not being used.
To fix Memory leak, you have to locate it first. To do so, follow these basic steps:
- Affirmation – you first have to determine if a leak has occurred.
- Finding Kernel-mode memory leak – look for a leak caused by some kernel-driver component.
- Finding User-mode memory leak – look for a leak caused by user-mode driver or program.
How is memory allocated?
Applications allocate RAM using different modes. So if the space is not freed after use, memory leak is most likely to occur irrespective of the mode of allocation. The following are some common allocation patterns:
- HealAlloc functions for heap memory allocation. For instance, the C/C++ runtime is equivalent to malloc and new.
- VirtualAlloc functions for direct allocation from operating system.
- Kernel32 APIs that holds kernel memory for the program such as CreateFile and CreateThread.
- User32 APIs and Gdi32 APIs.
How can you prevent memory leaks?
Prevention is definitely better than cure – to prevent memory leaks, here are some tips you must follow:
- Monitoring habits
You have to be mindful of any RAM usage by any individual applications or programs. To do so, you can simply go to the Windows Task Manager by tapping Ctrl + Shift + Esc then add the columns such as Handles, User Objects, GDI Objects and so on – this will help you monitor resource usage easily.
- Make use of Microsoft tools to diagnose memory leaks such as:
- Application Verifier – diagnoses heap leaks
- UMDH – a component of Windows Debugging Tools that diagnoses leaks for any individual processes by monitoring heap memory allocations.
- Trace Capture – for a systematic RAM usage analysis.
- Xperf – to track heap allocation patterns.
- CRT Debug Heap – not only tracks heap allocations but also allows coding practices to minimize leaks.
- Usage tips
- For Win32 resources and heap allocations, use kernel HANDLEs and other smart pointers.
- For automatic resource management for kernel allocations, get classes from the ATL library where C++ standard has auto_ptr for heap allocations.
- With the help of _bstr_t or _variant_t or _com_ptr_t, encapsulate COM interface pointers within smart pointers.
- For any abnormal memory usage, monitor the .NET code.
- To let allocations be free from variables in most blocks at the end of a function by avoiding multiple exit paths for functions.
- After freeing all allocations in the _finally block use the native exceptions and wrap all heap and handle allocations into smart pointers to use the C++ exceptions.
- Before reinitializing or discarding a PROPVARIANT object, you must always call the PropVariantClear function.
To troubleshoot memory leaks in Windows 10, follow the instructions below.
- Close processes and restart your PC
When you see an unnecessary process taking up too much RAM, end that process in the Task Manager and then restart your PC so that the newly freed space will be available for use by other processes and besides, if you don’t restart your PC, the memory leak problem won’t be fixed. One of the certain processes that have bugs which slow down computer is the Runtime Broker – try to disable this process.
- Memory Diagnostic Tools
- If you are working on something, save it.
- Tap Win + R to launch the Run dialog box.
- Type in exe in the field and tap Enter or click OK to open Memory Diagnostics.
- Restart your PC.
- After that, do a basic scan or go to the Advanced options such as “Test mix” or “Past count”.
- To start the test, tap F10.
Note: These are only temporary fixes.
- Check for any Driver updates
- Tap Win + R to open Run.
- Type in msc and tap Enter to open the Device Manager.
- Under Device Manager, check for any outdated drivers and update all of them.
- And if you think that you might have missed other update, check Windows Update.
- Optimize your system’s performance
When you adjust Windows performance, everything will fall into place including the processor scheduling and memory usage that will stop memory leaks. To optimize system performance, follow the steps below:
- Right click on “This PC” and select the “Advanced” settings on the left pane.
- Go to “Performance” followed by “Settings” under the Advanced tab.
- Check the “Adjust for best performance” option and click OK.
- Restart your PC and check if it fixed the problem.
You can also PC Cleaner Pro to optimize your system performance. PC Cleaner Pro is a one-click solution that does not only optimize your system’s overall performance but also fixes any system errors or corrupted registries.
- Disable programs that are running on Startup
- Go to the Task Manager.
- Then proceed to Startup.
- Disable all the startup that your PC does not need to run by default.
- Defrag your hard drive
- Go to “My Computer” or “This PC”.
- Right click on the system drive which is usually the C: drive.
- And under Properties, go to “Tools” and select “Optimize”.
- Select the hard drive you want to defrag and then select “Analyze”.
- The process may take a while. After the new fragmentation, restart your PC.
- Clear the Page file at shutdown
Here’s how you can clear the Page file at every shut down to fix memory leaks:
- Type in regedit in the Search bar to open the Registry Editor.
- Go to this path:
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerMemory Management
- Modify the value of ClearPageFileAtShutDown to 1.
- Save the changers made, close the Registry Editor.
- Restart your PC.
- Try disabling Superfetch
The Superfetch service optimizes performance by minimizing your PC’s boot time and it lets Windows manage RAM usage. Take note that your life won’t be the same after you disable this service but you must do it to fix memory leaks. To disable Superfetch, follow these steps:
- Type in msc in Search to go to the Services Manager.
- Under Services Manager, look for “Superfetch” and right click on it and go to Properties then select Stop.
- And on the drop-down menu, “Disable” the service.
- Restart your PC and check if your system’s performance has gotten better.
Note: if disabling Superfetch didn’t do anything to improved system performance, enable it back.
- Check for any malware
To check for any malware in your computer that might be causing memory leak, use SpyRemover Pro or Windows Defender.