多线程应用程序在高内存使用率时崩溃,错误R6016或0xC0000005 [英] Multithreaded application crashes with errors R6016 or 0xC0000005 at high memory usage

查看:301
本文介绍了多线程应用程序在高内存使用率时崩溃,错误R6016或0xC0000005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是基于多线程控制台的应用程序,在Windows XP中使用MSVC2010在C ++中开发。该应用程序消耗大量的内存。但是,当内存使用量达到2GB(Windows 32位内存限制)时,它会随机出现崩溃,出现以下错误:



  1. R6016-线程数据空间不足

  2. 0xC0000005:访问冲突读取位置0x02030000


伴随着这一点,还有一个字符串出现在控制台上的运行时错误多次。但不知道它从哪里来(我的代码不打印它)。



不幸的是,没有确定的堆栈跟踪或位置可用的地方崩溃。它只是崩溃的任何地方,特别是当内存使用大约2GB。
我希望CRT在无法分配内存时返回错误或失败。在这种情况下,我确保应用程序运行顺利。但是为什么在这个地球它
显示消息框与这些错误,导致退出应用程序。



我在这方面苦苦了很多天,现在真的难以忍受。我尝试:



1)通过调试器知道代码中崩溃的位置。但它只是崩溃在任何地方。例如,主要在新的内存分配调用类似于:



char * ptr = new(std :: nothrow)char [1024];



正如我们所看到的,没有任何应用程序可以在此处崩溃



2)缩小代码路径以找到它崩溃的确切情况:我也无法成功。



以为没有触摸内存使用我的应用程序到2GB。
但是一个问题出现在我的脑海里,什么是保证即使在低内存使用也不会崩溃。这可能只是时间问题?



此外,没有明确的方法可以提前知道可分配多少确切的内存



我真的用尽了这个错误,迫切需要帮助/指导。



UPDATE:



我缩小了这是 QueueUserWorkItem (Windows函数将工作项排队到线程池中的工作线程)。当调用是在没有内存情况下,它导致线程内的内存分配失败与运行时错误不幸的是,我不能在这里示例代码再现此崩溃,因为我通过第三方库调用该函数,它是非常很难提取出来的代码演示。



但是为了解释,我调用函数(带有标志 WT_EXECUTELONGFUNCTION ),当它完成时调用回调



作为一种解决方法我试图检查应用程序的内存使用情况(私人字节),而不是让它接触2GB。希望它应该工作。

解决方案

有几个组件:



<
  • 如果您接近2GB可寻址的用户空间,那么您实际上可能会出现内存不足的问题...

  • 您获得的例外不是一个通用的内存不足问题。

  • 内存管理可能依赖于线程本地存储。

  • 根据我发现的内容( http://social.msdn.microsoft.com/Forums/en-US/7b63a615-b906-42dc-87cb-da2638ed03ac/r6016-not -enough-space-for-thread-data?forum = vcgeneral ):


    一个线程使用CreateThread或任何其他不使用_beginthtread / _beginthreadex的函数。确切地说,当线程被创建时,你不会得到这个错误,以后当你尝试访问一些需要每线程数据结构的CRT功能时,你会得到这个错误。



    This is multi-threaded console based application being developed in C++ using MSVC2010 on Windows XP. The application consumes high amount of memory. But when memory usage reaches around 2GB (Windows 32bit memory limit) it abruptly crashes at random places with either of these errors:

    1. R6016- not enough space for thread data
    2. 0xC0000005: Access violation reading location 0x02030000

    Accompanied to this, there is also a string appears on console "runtime error" many many times. But not sure from where is it coming (My code does not print it).

    Unfortunately there are no definitive stack traces or locations available where it crashes. It just crashes anywhere especially when memory usage goes around 2GB. I would expect CRT to return error or failure when it cannot allocate memory. In that case I've made sure the application would run smooth. But why on this earth does it display message boxes with these errors which led to quit the application.

    I am struggling for many days on this and its really unbearable now. I tried:

    1) To know the location of crash in code through debugger. But it just crashes anywhere. For e.g. Mostly at new memory allocation calls similar to:

    char* ptr = new (std::nothrow) char[1024];

    As we can see there is no reason for any application to crash here.

    2) To narrow down the code path to find exact scenario under which it crashes: I couldn't succeeded in this also.

    As a workaround I thought to not to touch memory usage of my application to 2GB. But then a question came to my mind, "What is the guarantee that it will not crash even at low memory usage. It could be just matter of time?"

    Also, there is no definitive way to know in advance how much exact memory is available to allocate.

    I am really exhausted out with this bug and desperately need help/guidance.

    UPDATE:

    I narrowed down that this is problem with QueueUserWorkItem (Windows function that queues a work item to a worker thread in the thread pool). When the call is made under no memory situation, it causes memory allocations inside thread to fail with "runtime error" Unfortunately, I am unable give here sample code reproducing this crash, because I call that function through a third party library and it is very difficult to extract code out of it for demo.

    But to explain, I call the function (with flag WT_EXECUTELONGFUNCTION) and when it finishes it invokes the callback from where I call it again (so that it could process next work item).

    As a workaround I am trying to put check on memory usage of the application ("Private Bytes") and not letting it to touch 2GB. Hopefully it should work.

    解决方案

    There are several components:

    1. If you're close to the 2GB addressable user space, then you can actually get out of memory issues...
    2. The exception that you get isn't a generic "out of memory" problem. Actually it's a "problem when allocating thread local storage".
    3. Memory management may rely on thread local storage.

    From what I've found (http://social.msdn.microsoft.com/Forums/en-US/7b63a615-b906-42dc-87cb-da2638ed03ac/r6016-not-enough-space-for-thread-data?forum=vcgeneral):

    This error can also happen when you create a thread by using CreateThread or any other function which doesn't use _beginthtread/_beginthreadex. To be precise, you won't get this error when the thread is created, you'll get this error later when you try to access some CRT functionality which requires a per thread data structure.

    这篇关于多线程应用程序在高内存使用率时崩溃,错误R6016或0xC0000005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆