Thursday, December 21, 2006

Memory usage of axapta task

On a job client we had some troubles with memory wastage.
The reason was an external application with which we communicate across an OCX.
On each call the application needs more and more memory.
Our experience shows, on about 100MB Axapta crashed.
To solve this problem, our idea was, to monitor the used memory and if axapta reaches an defined limit, to shut down the axapta client.
With windows task manager it should be restartet automatic.

Now, after approximately three weeks in production system, we can say it works without problems, it works marvelously.


Solution:
Three new WinAPI-functions:


getProcessMemoryInfo
Retrieves information about the memory usage of the specified process.
Link to ms...


static container getProcessMemoryInfo(int _processHandle)
{
#define.structSize(4*10)
DLLFunction _getProcessMemoryInfo ;
Binary struct = new Binary(#structSize);
DLL _dLL = new DLL(#PSAPIDLL);
_getProcessMemoryInfo = new DLLFunction(_DLL, 'GetProcessMemoryInfo');

_getProcessMemoryInfo.returns(ExtTypes::DWord);

_getProcessMemoryInfo.arg(ExtTypes::DWord,
ExtTypes::Pointer, ExtTypes::DWord);

struct.dWord (#offset0, 0 );
struct.dWord (#offset4, #structSize );
struct.dWord (#offset8, 0 );
struct.dWord (#offset12, 0 );
struct.dWord (#offset16, 0 );
struct.dWord (#offset20, 0 );
struct.dWord (#offset24, 0 );
struct.dWord (#offset28, 0 );
struct.dWord (#offset32, 0 );
struct.dWord (#offset36, 0 );

if (_getProcessMemoryInfo.call(_processHandle, struct, #structSize))
{
return [struct.dWord(#offset0),
struct.dWord(#offset4),
struct.dWord(#offset8),

struct.dWord(#offset12),
struct.dWord(#offset16),
struct.dWord(#offset20),
struct.dWord(#offset24),
struct.dWord(#offset28),
struct.dWord(#offset32),
struct.dWord(#offset36)];
}

return conNull();

}


getCurrentProcessId
Retrieves the process identifier of the calling process.
Link to ms...

client server static int getCurrentProcessId()
{
//The return value is the process identifier of the calling process.

DLL winApiDLL = new DLL(#KERNELDLL);
DLLFunction getCurrentProcessId = new DLLFunction(winApiDLL, 'GetCurrentProcessId');

getCurrentProcessId.returns(ExtTypes::DWord);
return getCurrentProcessId.call();
}

OpenProcess
Opens an existing local process object.
Link to ms...

static int openProcess(boolean _openForQuery = true, int _processId = winapi::getCurrentProcessId())
{
//If the function succeeds, the return value is an open handle of the specified process.

#define.PROCESS_QUERY_INFORMATION (1024) //&h400
#define.PROCESS_VM_READ (16) //&h10
#define.PROCESS_ALL_ACCESS (2035711) //&H1F0FFF
DLL _winApiDLL = new DLL('KERNEL32');
DLLFunction _openProcess = new DLLFunction(_winApiDLL, 'OpenProcess');
int call = _openForQuery ? #PROCESS_QUERY_INFORMATION : #PROCESS_ALL_ACCESS;
;
_openProcess.returns(ExtTypes::DWord);
_openProcess.arg(ExtTypes::DWord, ExtTypes::DWord, ExtTypes::DWord);
return _openProcess.call(call, 0, _processId);
}

Usage:
Simple job:
static int getCurrentMemoryUsage(boolean _kiloByte = true)
{
hwnd curhwnd = winapi::openProcess();
container memInfo;
;
memInfo = winapi::getProcessMemoryInfo(curhwnd);
winapi::closeHandle(curhwnd);
if(_kiloByte)
return conpeek(memInfo, 4) / 1024;
else
return conpeek(memInfo, 4);
}

3 comments:

Arijit Basu said...

Hi,
Nice blog. Can I link to my Blog
http://daxguy.blogspot.com/

Anonymous said...

# The Best SAP Training Video You Ever Seen...

Visit WWW.SAPVISUAL.COM

We Guarantee...!!! You CAN be a certified SAP consultant fast...

axapta development said...

As we know that Axapta development is mainly for the organizations who are using ERP services for their production system so your post for controlling & managing the memory system for Axapta is very impressive & helpful. Because there are many versions for Axapta so they need a careful attention so that they can be handled according to their requirement & the latest version of Axapta is Dynamics AX 2012 R2 which is having lots of features which are more effective than others.