question

SanjeevGupta-1515 avatar image
0 Votes"
SanjeevGupta-1515 asked JeanineZhang-MSFT edited

Observing memory leak on Windows Server 2016 with a 32-bit C++ application

Hello,

We have a win32 C++ application. We started observing memory leak issue with it when running this application on Windows Server 2016.

We have a hierarchy of exception classes and the base exception class handles the stack trace generation and string conversion for logging.
• When the exceptions are thrown frequently, we see that with the increase in number of exceptions thrown and time, the memory consumption of the service that throws these exceptions increases when the application is running on Windows Server 2016.
E.g. if we keep the service running for 2+ days (regularly throwing exceptions) the consumption reaches 190 MB and continues to increase

• We have traced and listed down the Microsoft APIs that are called when this exception is thrown:
1. _wgetenv()
2. GetCurrentThread()
3. GetCurrentProcess()
4. RtlCaptureContext( &ctxt )
5. StackWalk64( IMAGE_FILE_MACHINE_I386, process, thread, &frame, &ctxt, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL )
6. SymSetOptions( SYMOPT_CASE_INSENSITIVE | SYMOPT_LOAD_LINES )
7. GetModuleFileNameA()
8. SymInitialize()
9. SymGetSymFromAddr()
10. SymGetLineFromAddr()
11. EnumProcessModules()
12. GetModuleInformation()
13. GetModuleFileNameA()
14. GetFileAttributesA()
15. SymLoadModule64()
16. GetModuleFileNameW()
17. SymCleanup(process)

• And below Structures are used:
1. ::STACKFRAME64 frame
2. ::CONTEXT ctxt

• All these APIs are called from a DLL whose methods are invoked when the exception is thrown by the service
• The base exception class definition is also present in the DLL code, whereas the actual derived exception class is defined in the exe code.

We have checked and observed below –
• The issue does not occur on 2012 and 2019 server; hence, the issue is specific to windows server 2016
• We also tried to check if there is any difference between 2012 and 2016 server in terms of allocation and deallocation of resources by these APIs on Microsoft’s official documentation but could not find anything conclusive

Please let me know if more details are required.
Thanks in advance,

Sanjeev

windows-server-2016
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please explain how you are observing memory leaks. Are you using the task manager for that? Is there a problem other than your observations?

0 Votes 0 ·

We are using resource monitor tool available with the windows task manager.
There is no other problem apart from the memory consumption keeps on increasing on Windows Server 2016.
This issue is not observed on Windows server 2012 or 2019.

0 Votes 0 ·

1 Answer

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered SanjeevGupta-1515 commented

Hello @SanjeevGupta-1515

I would like to suggest you to check below Microsoft article discussing the same however its for Windows 7 and 2008 R2 but may help in your situation.
https://docs.microsoft.com/en-us/windows/win32/win7appqual/preventing-memory-leaks-in-windows-applications

If the reply was helpful, please don’t forget to upvote or accept as answer.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The suggested answer in the article mainly focuses on best practices part for preventing memory leakage. Some observations -
• - HeapAlloc / Malloc / VirtualAlloc is not called anywhere in the code
• - PROPVARIANT is also not used anywhere in the code
• - new operator is used for allocations, but the deallocations are handled (otherwise the issue would have occurred in windows server 2012 and 2019 as well, which is not the case)
• - The same argument goes for other best practices mentioned in the documentation. The issue is specific to windows server 2016 and is not observed on either of 2012 and 2019 windows servers, hence not sure if best practices checklist plays a role here.

A real-world scenario of this issue is that the process reaches up to a consumption of 2 GB when exceptions are thrown for an extended period of time and since the application is 32-bit, the process crashes, halting everything.
And again, this is specific to windows server 2016.

0 Votes 0 ·