A Memory Leak is a misplacement of resource in a computer program due to faulty memory allocation. It happens when a RAM location not in use remains unreleased. A memory leak is not to be confused with a space leak, which refers to a program using more RAM than necessary. A memory leak on a Windows 10/8/7 system is said to have taken place when the memory is just not accessible despite not being in use.
Memory Leaks in Windows 10
Why is it bad
It is obvious a memory leak is bad because it is a bug, a flaw in the system. But let’s find out how exactly it affects the system:
- Since the memory is not freed even when not in use, it results in memory depletion.
- Exhaustion of the memory results in software aging.
- Reduced available memory leads to increased response time and poor performance by the system.
- Unchecked memory leak can eventually lead to the crash of an application.
In order to identify a memory leak, a programmer needs to have access to the source code of the program.
Locating the leak
In order to solve a problem, we need to identify it first. The basic steps to locate a memory leak are:
- Affirmation: Determining whether a leak has occurred.
- Finding Kernel-mode memory leak: Locating a leak caused by a kernel-mode driver component.
- Finding User-mode memory leak: Locating a leak caused by user-mode driver or app.
There are different modes in which applications allocate RAM. If the space is not freed after use, the memory leak will occur irrespective of the mode of allocation. Some common allocation patterns are:
- HealAlloc function for heap memory allocation. The C/C++ runtime equivalents are malloc and new.
- VirtualAlloc function for direct allocation from OS.
- Kernel32 APIs to hold kernel memory for the app. Example, CreateFile, CreateThread.
- User32 APIs and Gdi32 APIs.
Preventing memory leaks
We all know prevention is better than cure so here are some ways to prevent a memory leak.
You need to keep an eye out for abnormal RAM usage by individual programs and applications. You can go to Windows Task Manager by pressing CTRL+SHIFT+ESC and add the columns like Handles, User Objects, GDI Objects, etc.
This will help you easily monitor resource usage.
Microsoft tools to diagnose memory leaks
Various tools diagnose memory leaks for various allocation modes:
- Application Verifier diagnoses heap leaks.
- UMDH ( a component of Windows Debugging Tools) diagnoses leaks for individual processes by monitoring heap memory allocations.
- Trace Capture for a thorough analysis of RAM usage.
- Xperf too tracks heap allocation patterns.
- CRT Debug Heap not only tracks heap allocations but also enables coding practices to minimize leaks.
- Use kernel HANDLEs and such other smart pointers for Win32 resources and heap allocations.
- Get classes for automatic resource management for kernel allocations from the ATL library. C++ standard has auto_ptr for heap allocations.
- Encapsulate COM interface pointers within “smart pointers” with the help of _com_ptr_t or _bstr_t or _variant_t.
- Monitor the .NET code for abnormal memory usage.
- Avoid multiple exit paths for functions to let allocations be freed from variables in most blocks by the end of a function.
- Use native exceptions only after freeing all allocations in the _finally block. Wrap all heap and handle allocations into smart pointers in order to use C++ exceptions.
- Always call the PropVariantClear function before reinitializing or discarding a PROPVARIANT object.
Troubleshooting memory leaks in Windows
Just like the various ways to prevent memory leaks, there are various ways to stop memory leaks.
1] Close processes and restart.
If you see an unnecessary process is taking up too much RAM, you can end the process in the Task Manager. You will need to restart the device so that the freed space is available for use by other processes. Without a Restart memory leak issue won’t be solved. One particular process that has bugs to slow down the PC is Runtime Broker. Try if disabling that alone works.
2] Memory Diagnostic Tools
To access the inbuilt Memory Diagnostic Tool for Windows:
- Save all your important work.
- Press Win + R to open the Run window.
- Type the command mdsched.exe in the Run window.
- Restart the PC.
- After a restart, perform a basic scan or go for the ‘Advanced’ options like ‘Test mix’ or ‘Pass count’.
- Press F10 to start the test.
These are still temporary fixes.
3] Check for Driver updates
Outdated drivers cause memory leaks. Keep all drivers updated:
- Press Win + R and open the Run window. Type devmgmt.msc and press Enter. It will take you to the Device Manager.
- Check for outdated drivers and update them all.
- For updates you might have missed, check in Windows Update.
This was simple.
4] Optimize performance
Adjusting Windows for performance will manage everything including processor scheduling and memory usage to stop memory leaks. Follow these steps:
- Right-click on ‘This PC’ and select ‘Advanced’ settings on the left pane.
- Under the ‘Advanced’ tab, go to ‘Performance’, followed by ‘Settings’.
- Check ‘Adjust for best performance’ and click OK.
- Restart and check if the issue is solved.
If this simple solution did not work, try the next solution.
5] Disable Programs running on Startup
Disabling the troublesome programs is the only way to get rid of the memory leak issue. Go to Task Manager and disable the troublemaking program. If you don’t know which programs are creating the trouble, do this:
- Go to ‘Task Manager’.
- Go to ‘Startup’.
- Disable the startup programs you don’t need to run by default.
6] Defrag Hard Drive
Although Windows 10 does this for you automatically, you may need to do defrag the hard drives occasionally to optimize performance:
- Go to ‘This PC’ or ‘My Computer’.
- Right-Click the system hard drive (usually C: drive).
- Go to ‘Tools‘ under ‘Properties‘ and select ‘Optimize’.
- Select the drive to defrag and select ‘Analyze’.
Restart your computer after the new fragmentation.
7] ClearPage File at shutdown
It is getting complex now – but don’t worry. Here’s how to clear the Page File at every shutdown:
- Type regedit on Search to launch the Registry Editor.
- Enter this path: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerMemory Management
- Change the value of ClearPageFileAtShutDown to ‘1’.
- Save the changes and Restart the device.
This ought to do it.
9] Disable Superfetch
This Windows service optimizes performance by minimizing boot time. It lets Windows manage RAM usage. Life after disabling Superfetch is not convenient but do it if you must. At least try this alone to isolate the problem:
- Search services.msc and go to Services Manager.
- Look for ‘Superfetch’ and right-click it to go to ‘Properties’.
- Select ‘Stop’.
- Also ‘Disable’ the service from the drop-down menu.
- Restart the PC and check if the performance has improved.
Enable Superfetch if this one didn’t work.
10] Check for malware
Use a third-party antivirus software or Windows 10’s in-house Windows Defender to scan and eliminate malware. Make sure the antivirus is updated to support your OS so that it doesn’t end up becoming the reason for the memory leak.