Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么运行时间越长=RAM中的MB越多_Android_Multithreading_Delphi - Fatal编程技术网

Android 为什么运行时间越长=RAM中的MB越多

Android 为什么运行时间越长=RAM中的MB越多,android,multithreading,delphi,Android,Multithreading,Delphi,我的应用程序有问题。运行时间越长=RAM中的MB越多。它在我尝试使用线程后启动。即时通讯使用3倍胎面计时器。线程im使用,因为im使用的是httpget,没有它,GUI总是在启动时冻结调用httpget,下载文件大约150字节。定时器间隔可设置为5-300秒,大多数时间为5秒。因此,每次定时器时,我调用3x线程,在每个线程中,我调用httpget。但是应用程序消耗内存可能我在线程上做错了什么?也许他们会以某种方式保持“开放”状态 关闭你的线程:@NeelKamath,这是完全错误和不相关的,而且

我的应用程序有问题。运行时间越长=RAM中的MB越多。它在我尝试使用线程后启动。即时通讯使用3倍胎面计时器。线程im使用,因为im使用的是httpget,没有它,GUI总是在启动时冻结调用httpget,下载文件大约150字节。定时器间隔可设置为5-300秒,大多数时间为5秒。因此,每次定时器时,我调用3x线程,在每个线程中,我调用httpget。但是应用程序消耗内存可能我在线程上做错了什么?也许他们会以某种方式保持“开放”状态


关闭你的线程:@NeelKamath,这是完全错误和不相关的,而且由于使用了
FreeOnTerminate
而没有必要。还有一些事情正在发生。正在使用什么版本的Delphi<代码>同步()在XE7之前在Android上已中断。那么
HttpGet()
到底是什么样子呢?在计时器方法中使用try finally是错误的。这不是您的问题,但您应该删除这三个try finally块。如果在设置FreeOnTerminate时引发异常,则您可以安全地退出。在这种情况下启动线程是错误的。雷米:使用Delphi 10.2,http将添加到post中大卫:所以我会继续尝试而不最终?在尝试之前,开始会移动吗?
var
  a, b, c: string;

type
  TThread_1 = class(TThread)
    private
      FVar_1: string;

      procedure Update_1;
    protected
      procedure Execute; override;
    end;

  TThread_2 = class(TThread)
    private
      FVar_2: string;

      procedure Update_2;
    protected
      procedure Execute; override;
    end;

  TThread_3 = class(TThread)
    private
      FVar_3: string;

      procedure Update_3;
    protected
      procedure Execute; override;
    end;


procedure TThread_1.Execute;
begin
  FVar_1 := HttpGet('url', 'a');
  Synchronize(Update_1);
end;

procedure TThread_1.Update_1;
begin
  a := FVar_1;
end;

procedure TThread_2.Execute;
begin
  FVar_2 := HttpGet('url', 'b');
  Synchronize(Update_2);
end;

procedure TThread_2.Update_2;
begin
  b := FVar_2;
end;

procedure TThread_3.Execute;
begin
  FVar_3 := HttpGet('url', 'c');
  Synchronize(Update_3);
end;

procedure TThread_3.Update_3;
begin
  c := FVar_3;
end;


procedure TForm1.Timer_thread(Sender: TObject);
var
  t1 : TThread_1;
  t2 : TThread_2;
  t3 : TThread_3;
begin
  if lb_1.IsChecked = True then
    begin
      t1 := TThread_1.Create(true);
      try
        t1.FreeOnTerminate := true;
      finally
        t1.Start;
      end;
    end;

  if lb_2.IsChecked = True then
    begin
      t2 := TThread_2.Create(true);
      try
        t2.FreeOnTerminate := true;
      finally
        t2.Start;
      end;
    end;

  if lb_3.IsChecked = True then
    begin
      t3 := TThread_3.Create(true);
      try
        t3.FreeOnTerminate := true;
      finally
        t3.Start;
      end;
    end;
end;

function HttpGet(const url: string; const tip: string): string;
var
  HTTP: TIdHTTp;
begin
  HTTP := TIdHTTP.Create(nil);
  HTTP.Request.UserAgent := 'Mozilla/6.0 (compatible; Delphi)';
  try
    try
      Result:=http.Get(url);
    except
      on E : Exception do
        begin
          if Pos('Error resolving Address', E.Message) <> 0 then
            begin
              if tip = '1' then Form1.lb_1.Text:='1';
              if tip = '2' then Form1.lb_2.Text:='2';
              if tip = '3' then Form1.lb_3.Text:='2';
            end;
        end;
    end;
  finally
    FreeAndNil (http);
  end;
end;
procedure TForm1.Timer_thread(Sender: TObject);
var
  t1 : TThread;
  t2 : TThread;
  t3 : TThread;

begin
  if lb_1.IsChecked = True then
    begin
      t1:= TThread.CreateAnonymousThread(
        procedure
        var s: string;
          begin
            s := HttpGet('url_a', 'a');
            TThread.Synchronize(nil,
            procedure
              begin
                a := s;
              end);
          end);
      t1.Start;
    end;

  if lb_2.IsChecked = True then
    begin
      t2:= TThread.CreateAnonymousThread(
        procedure
        var s: string;
          begin
            s := HttpGet('url_b', 'b');
            TThread.Synchronize(nil,
            procedure
              begin
                b := s;
              end);
          end);
      t2.Start;
    end;

  if lb_3.IsChecked = True then
    begin
      t3:= TThread.CreateAnonymousThread(
        procedure
        var s: string;
          begin
            s := HttpGet('c', 'c');
            TThread.Synchronize(nil,
            procedure
              begin
                c := s;
              end);
          end);
      t3.Start;
    end;
end;