297 views
in GUI Development by

Hi,

We have interface functions in Device Class, within which we are allocating memory for strings using EwNewStringUtf8/EwNewString functions.

e.g.

/* 'C' function for method : 'Device::DeviceClass.GetAppName()' */
XString DeviceDeviceClass_GetAppName( DeviceDeviceClass _this )
{
  XString appName;

  /* Dummy expressions to avoid the 'C' warning 'unused argument'. */
  EW_UNUSED_ARG( _this );

  appName = 0;
  {
    TCHAR* pString = EwGetAppName();
    UNSIGNED16 stringLen = TCHAR_TO_BYTES(OSstrnlen_s(pString, MAX_STRING_BUFFER_SIZE));
    if(pString != NULL && stringLen > 0 && stringLen < TCHAR_TO_BYTES(MAX_STRING_BUFFER_SIZE))
    {
    #ifndef _UNICODE
          appName = EwNewStringUtf8(pString, stringLen);
    #else
          appName = EwNewString(pString);
    #endif
        }

  }
  return appName;

This appName string is only used in a screen and its expected that its memory should get freed once screen is changed, but it seems that Device Class autoobject is not finalized until the shutdown of Embedded Wizard task. 

I am wondering if each time user moves to the screen which calls this function, the new memory gets allocated but might not be freed until the end of EW task.

Is there a way to check in garbage collector to see if the memory allocated for local variables in Device Class gets freed ?

 

Thanks,

Vikas

1 Answer

+1 vote
by
 
Best answer

Hello Vikas,

in your implementation you create a new string by invoking the function EwNewString() or EwNewStringUtf8(). First at all, this is the right approach. The disposal of the memory occupied by the strings is performed during the garbage collection. This operation is triggered at the end of the screen update (see step 7 in chapter Platform Integration Aspects: Main Loop). Consequently, when you create a string and switch the screen, the string continues occupying the memory until the garbage collection is performed.

The garbage collection (as its name indicates) reclaims only 'garbage'. The string is considered as garbage when there is no variable, property, etc. in the GUI application referring the string. When the string returned by the method GetAppName() is assigned to e.g. a Text view and the view belongs to an actually used object, the string can't be discarded - it is actively used - at least from the garbage collection's point of view.

I am wondering if each time user moves to the screen which calls this function, the new memory gets allocated but might not be freed until the end of EW task.

Possibly your application creates more and more objects and connects them together so all the objects are in use? To test it I would recommend following approaches:

1. When you prototype your application in Embedded Wizard Studio, you can track the RAM usage. See: Embedded Wizard Studio: Memory (RAM) usage window. Please note the explanation in the referenced chapter concerning the frequency when the garbage collection is started during the prototyping. Misunderstanding this aspect may lead to wrong memory usage interpretation. With this debug window you can observe how many objects exist and whether the number increases.

2. In the target system you can protocol the memory usage. For this purpose the function EwPrintProfilerStatistic() is available. Each time you invoke the function a short overview of the actual memory usage, the number of objects and number strings is printed on the console. Depending on your target system, it may be sufficient to rebuilt your application with the defined macro EW_PRINT_MEMORY_USAGE. See also the section Monitoring the Data Memory (RAM) usage. Again, the outputs help you to track whether the number of objects/string increases or not.

I hope it helps you further.

Best regards

Paul Banach

by

Hi Paul,

Thanks!

Right, the memory disposal of strings looks to be correctly done during garbage collection.

Verified this with function EwPrintProfilerStatistic() . checked the actual memory usage, the number of objects and number strings and garbage collection is working fine with the device class strings. 

Regards,

Vikas

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...