How to find and fix Memory Leaks in Windows 10/8/7

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

Before you begin, you should know that Memory leak is a software issue to debug – for example in Java, JavaScript, C/C++, Windows, etc. Physically replacing RAM or hard disk isn’t required.

Memory Leak 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:

  1. Since the memory is not freed even when not in use, it results in memory depletion.
  2. Exhaustion of the memory results in software aging.
  3. Reduced available memory leads to increased response time and poor performance by the system.
  4. 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:

  1. Affirmation: Determining whether a leak has occurred.
  2. Finding Kernel-mode memory leak: Locating a leak caused by a kernel-mode driver component.
  3. Finding User-mode memory leak: Locating a leak caused by user-mode driver or app.

Memory allocation

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:

  1. HealAlloc function for heap memory allocation. The C/C++ runtime equivalents are malloc and new.
  2. VirtualAlloc function for direct allocation from OS.
  3. Kernel32 APIs to hold kernel memory for the app. Example, CreateFile, CreateThread.
  4. 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.

Monitoring habits

Memory Leaks in Windows 10

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:

  1. Application Verifier diagnoses heap leaks.
  2. UMDH ( a component of Windows Debugging Tools) diagnoses leaks for individual processes by monitoring heap memory allocations.
  3. Trace Capture for a thorough analysis of RAM usage.
  4. Xperf too tracks heap allocation patterns.
  5. CRT Debug Heap not only tracks heap allocations but also enables coding practices to minimize leaks.
  6. JavaScript Memory Leak Detector debugs memory leaks in codes.

Usage Tips

  1. Use kernel HANDLEs and such other smart pointers for Win32 resources and heap allocations.
  2. Get classes for automatic resource management for kernel allocations from the ATL library. C++ standard has auto_ptr for heap allocations.
  3. Encapsulate COM interface pointers within “smart pointers” with the help of _com_ptr_t or _bstr_t or _variant_t.
  4. Monitor the .NET code for abnormal memory usage.
  5. Avoid multiple exit paths for functions to let allocations be freed from variables in most blocks by the end of a function.
  6. 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.
  7. 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

Windows Memory Diagnostics

To access the inbuilt Memory Diagnostic Tool for Windows:

  1. Save all your important work.
  2. Press Win + R to open the Run window.
  3. Type the command mdsched.exe in the Run window.
  4. Restart the PC.
  5. After a restart, perform a basic scan or go for the ‘Advanced’ options like ‘Test mix’ or ‘Pass count’.
  6. Press F10 to start the test.

These are still temporary fixes.

3] Check for Driver updates

Update Driver Software

Outdated drivers cause memory leaks. Keep all drivers updated:

  1. Press Win + R and open the Run window. Type devmgmt.msc and press Enter. It will take you to the Device Manager.
  2. Check for outdated drivers and update them all.
  3. For updates you might have missed, check in Windows Update.

This was simple.

4] Optimize performance

Adjust for best performance

Adjusting Windows for performance will manage everything including processor scheduling and memory usage to stop memory leaks. Follow these steps:

  1. Right-click on ‘This PC’ and select ‘Advanced’ settings on the left pane.
  2. Under the ‘Advanced’ tab, go to ‘Performance’, followed by ‘Settings’.
  3. Check ‘Adjust for best performance’ and click OK.
  4. 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

Startup programs

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:

  1. Go to ‘Task Manager’.
  2. Go to ‘Startup’.
  3. Disable the startup programs you don’t need to run by default.

6] Defrag Hard Drive

Disk Defragmentation

Although Windows 10 does this for you automatically, you may need to do defrag the hard drives occasionally to optimize performance:

  1. Go to ‘This PC’ or ‘My Computer’.
  2. Right-Click the system hard drive (usually C: drive).
  3. Go to ‘Tools‘ under ‘Properties‘ and select ‘Optimize’.
  4. 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:

  1. Type regedit on Search to launch the Registry Editor.
  2. Enter this path: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerMemory Management
  3. Change the value of ClearPageFileAtShutDown to ‘1’.
  4. 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:

  1. Search services.msc and go to Services Manager.
  2. Look for ‘Superfetch’ and right-click it to go to ‘Properties’.
  3. Select ‘Stop’.
  4. Also ‘Disable’ the service from the drop-down menu.
  5. 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.

Every solution you might ever need to find or prevent memory leaks is right here. You can read more about memory leaks on MSDN and Microsoft.

Posted by on , in Category Windows with Tags

Karan Khanna is a passionate Windows 10 user who loves troubleshooting Windows problems in specific and writing about Microsoft technologies in general.