A Memory Leak is a misplacement of resources 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 or high memory usage, which refers to a program using more RAM than necessary. A memory leak on a Windows 11/10/8/7 system is said to have occurred when the memory is not accessible despite not being in use.
Memory Leaks in Windows 11/10
You should know that a Memory leak is an operating system or software issue to debug – for example, in Java, JavaScript, C/C++, Windows, etc. It is not to be confused with a space leak or high memory usage. Physically replacing RAM or hard disk isn’t required.
Why is a memory leak bad
A memory leak is bad because it is a bug, a flaw in the operating system or software. 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.
- An 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.
How to locate the memory leak
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 a user-mode driver or app.
How does Memory allocation occur
There are different modes in which applications allocate RAM. If 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.
How to prevent memory leaks in Windows
We all know prevention is better than cure, so it is important to monitor your system.
You need to watch for abnormal RAM usage by individual programs and applications. You can go to the 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.
Read: What are the Types of Memory in a computer?
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) analyzes 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.
- JavaScript Memory Leak Detector debugs memory leaks in codes.
Usage Tips
- Use kernel HANDLEs and 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.
How to fix a Memory Leak in Windows 11/10
Just like the various ways to prevent memory leaks, there are various ways to stop memory leaks. But before you begin, remember to create a system restore point first.
1] Close processes and restart
If you see an unnecessary process 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. Runtime Broker is one process that is known to slow down the PC. See if disabling that one 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. Follow one of the methods below to update your device drivers:
- You may check for Driver Updates via Windows Optional Updates to update your drivers
- You may visit the manufacturer’s site to download the drivers.
- Use a free driver update software
- If you have the INF driver file already on your computer then:
- Open Device Manager.
- Click the driver category to expand the menu.
- Then choose the relevant driver and right-click on it.
- Select Update Driver.
- Follow the on-screen wizard to finish updating your audio drivers.
Updating your operating system and the installed programs and Microsoft Store apps could help too.
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 does 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 11/10 does this for you automatically, you may need to 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. Do remember that your shutdown process will take a little more time.
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 doesn’t work.
10] Check for malware
Use a third-party antivirus software or Windows in-house Microsoft Defender to scan and eliminate malware. Ensure the antivirus is updated to support your OS so that it doesn’t become the reason for the memory leak.
Every solution you might need to find or prevent memory leaks is here. You can read more about memory leaks on MSDN and Microsoft.
Read: Memory leak issue in Remote Registry Service causes Windows to hang.