1

我一直在使用gabr 的 OmniThreadLibrary来构建一个 ThreadPool。

在多线程方面我是个新手,这就是为什么我发现 OTL 如此有趣。

在演示应用程序“app_11_ThreadPool.exe”之后,我在我的应用程序中设置了一个线程池。我的应用程序的目的是获取 URL 列表,并尝试连接到它们并下载自动返回的任何文件(我正在测试潜在的恶意软件站点的有效负载,以便我可以阻止它们)。

通常我会有一个包含 500 - 1000 个站点的列表。我有一个 TMemo 在我的主窗体上保存 URL。这是我的相关代码。请注意,此代码是从我的“开始”按钮单击事件执行的。

  iNumTasks := memoSiteList.Lines.Count - 1;
  GlobalOmniThreadPool.MaxQueued := 16;
  GlobalOmniThreadPool.MonitorWith(OmniEventMonitor1);
  GlobalOmniThreadPool.MaxExecuting := 16;
  GlobalOmniThreadPool.MaxQueued := 0;

    for iTask := 1 to iNumTasks + 1 do
    begin

      if iTask mod 4 = 0 then
        Application.ProcessMessages;

      CreateTask(
        TSiteQuery.Create(
        url,
        full_url,
        sProxyServer,
        sProxyPort,
        sSaveLocation,
        bVerboseHeaders)
        ).MonitorWith(OmniEventMonitor1).Schedule;
     end;

这是 TSiteQuery:

type
  TSiteQuery = class(TOmniWorker)
  strict private
    { Private declarations }
    FTaskID: int64;
    furl: string;
    f_full_url: string;
    fsProxyServer: string;
    fsProxyPort: string;
    fbVerboseHeaders: boolean;
    fsSaveLocation: string;
  private // [..]

现在,一切都很好。运行 1000 个 URL 会将我的应用程序的内存使用量从 10mb 增加到 ~130mb。这没什么大不了的,我明白这一点。

但问题是每次单击“开始”时,应用程序的内存使用量都会增加约 130mb。也许我只是不知道自己在做什么,但我的印象是使用线程池意味着我不必每次运行都创建新线程。

我希望池中先前创建的线程将在后续执行中重用。我期待我的应用程序的内存使用量将保持在 ~130 mb 左右,无论我点击“开始”1 次还是随后 10 次。

有什么建议么?

问候

4

2 回答 2

2

Is this a threading issue or a memory leak issue? Try using the debugger's Thread Status window to determine whether or not your number of allocated threads is actually increasing. If not, then you're probably creating objects inside the threads and not freeing them.

于 2009-08-07T23:54:10.360 回答
1

Looks more like an memory leak issue than a threading issue...

于 2009-08-08T00:47:05.043 回答