簡體   English   中英

沒有內存泄漏

[英]No memory leaks

在我的大學里,我們被要求創建一個分配所有可用內存的程序。 因此,我認為進行無限循環並在不釋放內存的情況下分配內存必須消耗計算機的所有可用內存。 但是,由於我沒有釋放內存,因此必須有大量的內存泄漏。

所以我寫了一個簡單的程序,但是當我用valgrind檢查它時,沒有內存泄漏。 沒有任何。 無直接無間接泄漏。

請告訴我為什么。 這是我的程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int i,kb=0,mb=0;
    char *some;
    while(1) {
        for(i=0;i<1024;i++)
        {
            // Allocating memory one kilobyte a time.
            some = (char*) malloc(1024);
            if(some == NULL)
            {
                exit(EXIT_FAILURE);
            }
            kb++;
        }
        // Displaying no. of mbs that have been allocated at each mb
        mb++;
        printf("%d mb\n",mb);
    }
    return 0;
}

替換some = (char*) malloc(1024); 與此: some = new char[1024]; 如果您至少有那么多的可用內存,它將失敗於2000MB。 但是,如果在Win32環境中運行此命令,則不會分配計算機中的所有可用內存,因為每個進程都有2GB的限制,因此在這種情況下,您將需要另一種方法。

首先想到的是分配已被優化-通常是完全優化或推入堆棧存儲。 在這種情況下,最好將其完全刪除。

通常,您可以通過閱讀生成的程序集來證明或反對這一點。

當我運行它時-valgrind發現問題:

==3335==
==3335== HEAP SUMMARY:
==3335==     in use at exit: 2,271,338,496 bytes in 2,218,104 blocks
==3335==   total heap usage: 2,218,105 allocs, 0 frees, 2,271,338,496 bytes allocated
==3335== 
==3335== 
==3335==     Valgrind's memory management: out of memory:
==3335==        newSuperblock's request for 8876032 bytes failed.
==3335==        3116339200 bytes have already been allocated.
==3335==     Valgrind cannot continue.  Sorry.
==3335== 
==3335==     There are several possible reasons for this.
==3335==     - You have some kind of memory limit in place.  Look at the
==3335==       output of 'ulimit -a'.  Is there a limit on the size of
==3335==       virtual memory or address space?
==3335==     - You have run out of swap space.`

即使使用O2 O3也無法消除該錯誤。 它是完整樣本嗎?

更新

該標志不會更改輸出,但是如果我在崩潰前中斷程序,則valgrind將顯示下一個:

^C1890 mb
==3286== 
==3286== HEAP SUMMARY:
==3286==     in use at exit: 1,981,808,640 bytes in 1,935,360 blocks
==3286==   total heap usage: 1,935,360 allocs, 0 frees, 1,981,808,640 bytes allocated
==3286== 
==3286== 276,480 bytes in 270 blocks are possibly lost in loss record 2 of 3
==3286==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3286==    by 0x8048472: main (mem_test.c:13)
==3286== 
==3286== 1,981,530,112 bytes in 1,935,088 blocks are definitely lost in loss record 3 of 3
==3286==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3286==    by 0x8048472: main (mem_test.c:13)
==3286== 
==3286== LEAK SUMMARY:
==3286==    definitely lost: 1,981,530,112 bytes in 1,935,088 blocks
==3286==    indirectly lost: 0 bytes in 0 blocks
==3286==      possibly lost: 276,480 bytes in 270 blocks
==3286==    still reachable: 2,048 bytes in 2 blocks
==3286==         suppressed: 0 bytes in 0 blocks
==3286== Reachable blocks (those to which a pointer was found) are not shown.
==3286== To see them, rerun with: --leak-check=full --show-reachable=yes
==3286== 
==3286== For counts of detected and suppressed errors, rerun with: -v
==3286== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

在Linux下,內核不限制分配,而是有效使用內存。 (請參閱https://www.kernel.org/doc/Documentation/vm/overcommit-accounting

$ echo 2 > /proc/sys/vm/overcommit_memory

應該禁用此功能,否則如果用0填充分配的內存,則代碼應按預期運行:

some = (char*) malloc(1024);
if(some == NULL) {
   exit(EXIT_FAILURE);
}
memset(some, 0, 1024);
kb++;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM