全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2558
推到 Plurk!
推到 Facebook!

TListItem程式的記憶體使用量越來越大?

缺席
achigo
一般會員


發表:6
回覆:15
積分:4
註冊:2005-05-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-11-18 01:36:35 IP:203.77.xxx.xxx 訂閱
透過Windows XP 工作管理員查看記憶體使用量,程式跑的越久,記憶體使用量越大
使用CodeGuard全勾也沒有發現記憶體洩漏是不是有什麼好方法可以將記憶體釋放呢?
看了一些文章也懷疑是不是資源洩漏了,也許是BCB6.0的BUG吧!

程式碼如下:


[code cpp]
//---------------------------------------------------------------------------
#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
BOOL bDone = FALSE;
INT iCount = 0;

TListItem *ListItem;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
VOID TForm1::UPDATE_LISTVIEW(INT i)
{
ListItem = ListView1->Items->Add();

ListItem->Caption = i;
ListItem->SubItems->Add(i);
ListItem->SubItems->Add("1");
if(0 == rand()%2) ListItem->SubItems->Add("0");
else ListItem->SubItems->Add("1");
ListItem->SubItems->Add(rand()0);
ListItem->SubItems->Add("100");
ListItem->MakeVisible(true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
for(int i = 0; i < 100; i )
{
UPDATE_LISTVIEW(i);
}
bDone = TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(TRUE == bDone)
{
btnStart->Click();
Label1->Caption = iCount;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnStopClick(TObject *Sender)
{
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------

[/code]


CodeGuard內容:

Functions called:
fflush (4 times)
delete (11 times)
free (7 times)
new (11 times)
SysReallocMem (30 times)
SysFreeMem (527 times)
lstrlenA (1 times)
lstrcpynA (4 times)
memset (2 times)
SysGetMem (528 times)
calloc (1 times)
strlen (69 times)
realloc (1 times)
strdup (1 times)
malloc (4 times)
memcpy (2 times)
Resource types used:
object (11 allocs, 11 max)
memory block (535 allocs, 337 max)
Modules used:
00400000 11/18/2008 01:36:32 G:\BCB\ListView\Project1.exe
==========================================

------
專注完美近乎苛求~~
編輯記錄
achigo 重新編輯於 2008-11-18 01:39:15, 註解 無‧
achigo 重新編輯於 2008-11-18 01:40:20, 註解 無‧
achigo 重新編輯於 2008-11-18 01:41:55, 註解 上傳程式碼‧
achigo 重新編輯於 2008-11-18 01:45:36, 註解 無‧
achigo 重新編輯於 2008-11-18 01:50:47, 註解 上傳圖片‧
aftcast
站務副站長


發表:81
回覆:1485
積分:1763
註冊:2002-11-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-11-18 09:20:49 IP:59.115.xxx.xxx 訂閱
我懷疑我有沒有看錯@@

你的程式碼不是這樣寫的嗎?

第一次startclick時就加了100個item,然後bDone=true,接下來你的timer事件又因為bDone=true,所以不停的又加入…

於是,難道不是時間愈久記憶用量愈大嗎? TlistView的內容愈來愈多,不是嗎?

我剛睡醒,是不是我有看錯? 因為看你寫程式的風格,感覺實力還不錯,還會用code guard…

------


蕭沖
--All ideas are worthless unless implemented--

C++ Builder Delphi Taiwan G+ 社群
http://bit.ly/cbtaiwan
編輯記錄
aftcast 重新編輯於 2008-11-18 09:23:41, 註解 無‧
achigo
一般會員


發表:6
回覆:15
積分:4
註冊:2005-05-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-11-20 18:57:22 IP:60.251.xxx.xxx 訂閱

我懷疑我有沒有看錯@@

你的程式碼不是這樣寫的嗎?

第一次startclick時就加了100個item,然後bDone=true,接下來你的timer事件又因為bDone=true,所以不停的又加入…

於是,難道不是時間愈久記憶用量愈大嗎? TlistView的內容愈來愈多,不是嗎?

我剛睡醒,是不是我有看錯? 因為看你寫程式的風格,感覺實力還不錯,還會用code guard…

===================引 用 aftcast 文 章===================

抱歉!沒有描述清楚程式在做什麼,由於我做的事情是要模擬人手動再按下btnStart這按鈕(壓力測試),
所以才會讓它一直跑,然後像您說的一樣,TListview的內容會越來越大,記憶體越吃越大,程式沒辦法
跑太久,不知道大大有沒有解決的方法?

------
專注完美近乎苛求~~
編輯記錄
achigo 重新編輯於 2008-11-20 18:58:01, 註解 無‧
achigo 重新編輯於 2008-11-20 18:58:53, 註解 無‧
achigo 重新編輯於 2008-11-20 19:02:15, 註解 無‧
achigo
一般會員


發表:6
回覆:15
積分:4
註冊:2005-05-28

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-11-23 11:25:49 IP:203.77.xxx.xxx 訂閱
此問題己解,記憶體使用量不會越來越大了,
請參考以下紅色兩行(ListItem->Free()和ListItem->Clear())

[code cpp]
//---------------------------------------------------------------------------
#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
BOOL bDone = FALSE;
INT iCount = 0;

TListItem *ListItem;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
VOID TForm1::UPDATE_LISTVIEW(INT i)
{
ListItem = ListView1->Items->Add();

ListItem->Caption = i;
ListItem->SubItems->Add(i);
ListItem->SubItems->Add("1");
if(0 == rand()%2) ListItem->SubItems->Add("0");
else ListItem->SubItems->Add("1");
ListItem->SubItems->Add(rand()0);
ListItem->SubItems->Add("100");
ListItem->MakeVisible(true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
ListItem->Free(); //使用完釋放掉
ListView1->Clear(); //使用完清除掉
for(int i = 0; i < 100; i )
{
UPDATE_LISTVIEW(i);
}
bDone = TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(TRUE == bDone)
{
btnStart->Click();
Label1->Caption = iCount;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnStopClick(TObject *Sender)
{
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------


[/code]
------
專注完美近乎苛求~~
編輯記錄
achigo 重新編輯於 2008-11-23 11:28:08, 註解 修改紅色標示‧
achigo 重新編輯於 2008-11-23 11:28:27, 註解 修改紅色標示‧
achigo 重新編輯於 2008-11-23 11:31:09, 註解 無‧
achigo 重新編輯於 2008-11-23 11:33:11, 註解 無‧
系統時間:2024-03-29 12:49:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!