Cancel
Showing results for 
Search instead for 
Did you mean: 

Femap API / Output / PutScalarAtNode / memory leak?

Experimenter
Experimenter

 

Hello,

I use DELPHI (Pascal) together with the FEMAP 11.2 type library to create user functions for FEMAP. At our current project we have to create numerous new outputsets and vectors for a  FEM model with about 170000 nodes. But after writing about 900 outputvectors we got the OLE Error <8007000E> which means insufficent memory for that process. Searching for memory leaks we found that each API command  Output.PutScalarAtNode reduces the available memory significantly by about 12 Bytes for each node (4 byte ID, 8 byte Value), until we ran out of memory. At each PutScalarAtNode command FEMAP seems to create an new copy of the IDList and ValueList array without freeing the former array. The following program fragment (despite senseless) shall show the problem. After 940 loops we ran out of memory with the OLE Error.  Has anybody made similar experience or a workaround for this problem? 

 

P.S.

1) Assigning values directly with output.value(x) for each node needs no additional memory, but is much to slow. 2) This error dont occur at the Femap build in Basic Interpreter.

 

Best regards

Michael Breuer

 

This is our test program:

//variables

var:

   Femap : Femap_TLB.model; 

   Vector:Femap_TLB.Output;

   NoNodes : longint;

   IDList : OleVariant;

   ValueList : OleVariant;

  ...

//program

begin

  Femap:=GetActiveOleObject('femap.model') as model;

  Vector:=Femap.feOutput;

  Vector.GetFromSet(1,14);   //Load Data from Outputset 1, Vector 14
  Vector.GetScalarAtNode(NoNodes,IDList,ValueList);

  for I:=100 to 1100 do

  begin

     Vector.ID:=I;

     Vector.PutScalarAtNode(NoNodes,IDList,ValueList;  //memory leak?

     Vector.Put(Vector.ID);

  end;

6 REPLIES

Re: Femap API / Output / PutScalarAtNode / memory leak?

Legend
Legend

Dear Michael,

 

 

I've tried to reproduce your problem. I did a tool (but on VB.net, don't work with Delphi). I used model with 250.000 nodes. I was able to create 1000 vectors:

 

program.png 

All vectors were created. I checked usage of ram memory and it was not more than 9MB. In the end it was 6:

ram.png

 

In visual basic .net your application works fine. Maybe Delphi works with memory differently and you have to control it manually?

 

Can you check in task manager how much ram memory your application consumes.

 

Also here is some tips how to measure memory used by your app:

http://stackoverflow.com/questions/437683/how-to-get-the-memory-used-by-a-delphi-program

 

Yarko

 

 

 

 

Re: Femap API / Output / PutScalarAtNode / memory leak?

Experimenter
Experimenter

Thank you four investigation an your hints,Yarko!

 

Indeed this errors seems to occur only at DELPHI, but I dont understand the different behaviour of DELPHI and Visual Basic Net. The different usage of array type types (Delphi uses variant array, VB not) may be the difference?

 

I made further checks of the memory usage a by creating an array of 150000 nodes and writing data by PutScalarAtNodes in a loop. Each loop consists of the following steps:

- 1st step creates an array of 150000 integer values (ID.

- 2nd step adds an array of 150000 double values (VALUE).

- 3rd step is the PutScalarAtNode API command

- 4th step is freeing the VALUE array

- 5th step is freeing the ID array

 

These are the results of my testprogram:

After 5th step the memory usage should be the same as before step 1, but there remains a memory leak of 150000x12 bytes. The command PutScalarAtNode commands in step 3 doubles the memory usage (copies ID and Values to another position?). The original ID and VALUE arrays are deleted at step 4 and 5 with the expected relative memory reduction. I have no chance of deleting the additional memory used by the PutScalarToNode command. So which each loop the required memory increases by about 150000x12 bytes, until the OLE Error 800700E occurs.

 

Memory usage of the testprogram:

1. loop

Total memory =12103680  
Delta memory =0
Delta memory after VarArrayCreate(ID) = 602112            (array of 150000 nodes x 4 bytes = 600000 Bytes)
Delta memory after VarArrayCreate(Value) = 1802240     (add array of 150000 nodes x 8 bytes = 600000 + 1200000 bytes)
PutScalarAtNode command
Delta memory after PutScalarAtNode = 3608576   (doubles the required memory !)
Delta memory after Clear(Value) = 2408448 (expected reduction)
Delta Memory after Clear(ID) = 1806336       (expected reduction)
----------------------------------------------------------

2. loop
Total memory =13910016 (increases at every loop)
Delta memory =0
Delta memory after VarArrayCreate(ID) = 602112
Delta memory after VarArrayCreate(Value) = 1802240
PutScalarAtNode command
Delta memory after PutScalarAtNode = 3608576
Delta memory after Clear(Value) = 2408448
Delta Memory after Clear(ID) = 1806336
----------------------------------------------------------

3. loop
Total memory =15716352 (increases at every loop)
Delta memory =0
Delta memory after VarArrayCreate(ID) = 602112
Delta memory after VarArrayCreate(Value) = 1802240
PutScalarAtNode command
Delta memory after PutScalarAtNode = 3608576
Delta memory after Clear(Value) = 2408448
Delta Memory after Clear(ID) = 1806336

.....

 

Interestingly a program memory optimization with <SetProcessWorkingSetSize> reduces the program memory usage, but the OLE errors occurs at the exact same loop as before. So not the absolute memory usage of the program seems to be the problem, but the additional memory used by the PutScalarAtNode command. In my opinion the knowledge of the source code of the PutScalarToNode command is essential to solve that problem. Meanwhile I try a workaround by writing all my output data to a femap neutral file and reading this file afterwords.

 

Best regards

Michael Breuer

Re: Femap API / Output / PutScalarAtNode / memory leak?

Legend
Legend

Hello Michael,

 

 

Visual Basic .Net is more advanced than simple Visual Basic. It has automatic tool (Garbage collector) which controls memory automatically. That's why I don't have problems.

 

What you can try: free memory manually. In the end of the loop add

FreeAndNil(IDList)

FreeAndNil(ValueList)

info about freeandnil function

http://www.delphibasics.co.uk/RTL.asp?Name=FreeAndNil

 

Or you can try FreeMem function:

http://www.delphibasics.co.uk/RTL.asp?Name=FreeMem

 

If these functions won't help, what you can try to set your application to x64 (64 bit) configuration. If application is run under x32 it has access only to 2GB or RAM shared with other 32 bits applications. If you use x64 your app can use all RAM memory of your computer.

Maybe this gives you extra capacity.

 

 

Yarko

 

Re: Femap API / Output / PutScalarAtNode / memory leak?

Experimenter
Experimenter

Hello,

The arrays IDList and ValueList are explicit deleted in my program with the following commands:

- VarClear(ValueList)    and

- VarClear(IDList)

The required amount of memory is subsequently reduced by the expected values. These are the step 4 and 5 of my program, described in my last post. Without explicit deleting the Delphi Garbage collector will do it automaticly, when the arrays get out of sight. But what I really don't understand is, why the API command PutScalarAtNodes doubles the required memory and how to flush that buffered data. Meanwhile i tried to flush the buffered data by writing both the outputvector and outputset with the PUT command, but these commands have no influence.

 

Best regards

Michael Breuer

 

 

 

 

 

 

 

Re: Femap API / Output / PutScalarAtNode / memory leak?

Legend
Legend

Dear Michael,

 

 

I did one more experiment with your app. I tracked how much Femap consume RAM memory.

When model is opened Femap consumes 900 MB.

After I run app with 1000 loops it consime extra 1GB.

For 2000 loops it takes 2GB.

 

So indeed problem is that after PutScalarAtNodes call it use memory and not free it afterwards. 

Probably only Femap developers can suggest you a function which can help.

 

Last small question: You're working with Femap 64bit?

 

Yarko

Re: Femap API / Output / PutScalarAtNode / memory leak?

Experimenter
Experimenter

Dear Yarko,

 

yes we work with the 64 bit Version of FEMAP 11.2.2.

 

Michael Breuer