Framework is eating memory on iOS

Hi


I have found that the aurelius framework is eating memory on the iOS platform. Simply querying the database with the code below will result in growing memory consumption of the application. In real-life apps this will eventually result in a "low-memory" warning of the OS and because you cannot explicitly free or clear something this will end up in iOS ending your app. 

You can see in instruments that the app will gradually eat more and more memory. But it does not report them as leaks, so my guess is the connection or some global class still has a reference to it. The classes in the code below are the same that the framework uses internally for finding and updating objects. So every DB intense app will eventually be destroyed by iOS because it uses to much memory.

procedure TForm4.Button3Click(Sender: TObject);
var
  man: TObjectManager;
  I: Integer;
//  SQL: string;
  stat: IDBStatement;
  res: IDBResultSet;
  found: boolean;
begin
  man := CreateObjectManager;
  try
    for I := 0 to 10000 do
    begin
      stat := man.Connection.CreateStatement;
      stat.SetSQLCommand('Select * from opdrachtbonnen');
      res := stat.ExecuteQuery;
      found := res.Next;
      stat := nil;
      res := nil;
    end;
  finally
    man.DisposeOf;
  end;
end;

We have done extensive tests with the code your provided (and variations). I cannot say Aurelius is not eating memory, since you claim it is. But the code you provided doesn't seem to be the case. In fact, we have confirmed that when using Instruments with the Allocation instrument, it keeps reporting that heap allocations are increasing. But when using the Activity Monitor (which shows memory usage), the code above does not increase the application memory.


Can you please confirm exactly how are you detecting memory increase using Instruments? Can you also please confirm the exact source code that causes such problem? 

We have done our tests using Delphi Berlin 10.1 Update 1, XCode 8, iPhone 5 with iOS 10, Aurelius 3.3.

Wagner,


I am no expert in Instruments, but it is my understanding that the profile template "Leaks" is used for monitoring the memory usage. When I start this with a test project then you can see the memory usage (Persistent Bytes) rise. At about 413Mb Memory Instruments signals a LowMemory message of the OS. This is repeated at about 460, then at 560, and in the end at 590Mb the OS terminates the app. So even if this statistic in instruments is wrong, as you say, it still seems to be the number iOS is looking at to send LowMemory and terminate the app.

The test project does nothing, it queries an empty table, and it accumulates memory (I query only 10.000 times) at a staggering rate. resulting in a whopping 590MB!!!!! 

You can guess that in a real-live app that uses lots of memory itself, this results in quite a lot of crashes.
I have been having this problem since the first release of our app. I was looking at leaks from delphi and my own code, until I finally broke it down to this simpel test. I have tested this in Delphi Berlin update1, Xcode 7.3.1. iOS 9.3.4, using Aurelius 3.1 and 3.3. but my guess is that this has been the issue all the time, so back to Aurelius 2.x with Delphi XE8 and Xcode 6x.

To be sure, I will test on Xcode 8 with iOS10. 

I will mail you my simpel testproject, so you can test it yourself. 


Hi Kick, please if possible include in your e-mail the exact steps to follow up the issue with Instruments. Yes, I used the Leaks options - Instruments doesn't report any leak. It does report an increase in memory heap allocation, but my application never crashes, I waited for some time for it. That's when I was suspicious about the report. Note also that I reduced the example to a much more simple example which is NOT related to Aurelius: 


while True do
begin
  SomeList := TObjectList<TObject>.Create;
  SomeList.DisposeOf;
end;

even that code above would cause memory heap allocation to increase constantly (can you check?).
But then, when using Activity Monitor, Instruments show you a list of all your iPhone applications and how much memory they are using. In that monitor, memory keeps constant, no increase. I did another test application where there is a deliberate leak of memory, and Instruments does report a memory increase, so it should display when the application is really eating memory.


Just send you the email. Just start instruments, use the "Leaks" template. select the device and the app (not in debug mode from inside IDE!) and start. You see the "Persistent Bytes" accumulate, the little gray flags up in the ruller will signal when you receive a Lowmemory warning (at lease that is my understanding). in my case (iPad air) at about 430Mb these warnings start appearing. eventually at about 590Mb the app is terminated.


Just tested your code:

while True do
begin
  SomeList := TObjectList<TObject>.Create;
  SomeList.DisposeOf;
end;

In my test, this showed NO increase in the "Persistent Bytes" column of instruments. the "Total Bytes" yes, but it is my understanding that this is the total amount of allocated memory (so including the already freed), not the current number of bytes in use. That is indicated by the "Persistent Bytes" column. But again, I am no expert, maybe somebody can confirm my assumptions?




Tested it with Xcode 8.0 and iOS 10.0.1 on a iPad Mini2. Same thing happens. Only now the LowMem messages come more rapidly after each other at around 430MB and crash happens at 463Mb. 


The new release 3.4.1. fixes the problem. 


Thanks a lot Wagner for the quick response and timely solution.