Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
helex
Posts: 256
Registered: ‎02-14-2012
My Carrier: -
Accepted Solution

Headless Apps - Resource management - 3 MB RAM

Happy new Year! :smileyvery-happy:

 

Regarding the documentation for the Headless Apps we have some limitations for CPU usage and Memory: LINK

 

I'm currently creating my first long running headless app. It simply checks the content of a website every 6 hours and stores the value in a SQLite database in case it is connected to the correct network.

 

My UI module takes later the collected values and presents it on a monthly chart. This way the users don't have to check it manually a few times a day and get without much effort a gapless graph at the end of the month.

 

Additional Benefit: This makes Hub messages possible in case the user exceeds a threshold.

 

While dealing with the CPU usage is not a problem I have maybe a issue with the Memory. So my question, what does this sentence in the documentation exactly mean and how is it measured: "By default, headless apps can occupy only 3 MB of memory."

And what happens if my headless module exceeds this limit?  Will it get closed automatically by the system or will it simply get rejected if it happens during the testing?

 

If it means HEAP memory on the STACK I guess I have not a issue, but when I check the parameter of my app using the device monitor it uses currently 4,2MB of RAM while the UI part is closed. I would say because of all the loaded libraries.

So far it works without issues. CPU and Battery usage are zero, only a spike every 6 hours to 0.05% CPU usage, a minimal change in memory usage which goes short after back to the old 4,2MB level and 3kB Network traffic every 6 hours. Looks good so far except the parameter for the Memory.

 

I have not many variables and objects in my headless module, but I need a bunch of libraries to make it properly working.

 

I need those to make the headless part working:

#include <bb/system/InvokeManager>
#include <bb/Application>
#include <QObject>

I have those for the network usage:

#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QHostAddress>

 This one to check if it is connected to the correct network and act as soon as the connection changes:

#include <bb/AbstractBpsEventHandler>
#include <bps/bps.h>
#include <bps/netstatus.h>
#include <bps/locale.h>

This ones are for the SQLite database in which the results get stored and I get the settings from how the headless module should work: (communication between headless and UI part)

#include <QDir>
#include <QSqlRecord>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QStringList>
#include <QSqlError>

And over here we have the ones to make the HUB notifications possible:

#include <bb/platform/Notification>
#include <bb/platform/NotificationDialog>
#include <bb/platform/NotificationError>
#include <bb/platform/NotificationResult>

 

Makes in total a working headless module with 76861 Bytes binary file but it appears with 4,2MB at the device monitor while running. :smileyfrustrated:

 

Any ideas or thoughts?

 

I uploaded yesterday my first built to BlackBerry World for testing. I'm really excited how well my first headless module is working and how big the benefit is I and the users get. But on the other side I'm a little bit concerned because of the value the devices monitor shows and I really hope the measurement is more precise compared to the device monitor and looks excactly which part is eating the memory.

 

I have two more apps on schedule which would benefit strongly form a headless module. On top of it ClipMan which is also using a SQLite database. So switching to binary or textfiles for this headless app using a ugly workaround is not a option. I will run into the same issue with the next headless app.

Please use plain text.
Developer
peter9477
Posts: 6,448
Registered: ‎12-08-2010
My Carrier: none

Re: Headless Apps - Resource management - 3 MB RAM

The 3MB limit applies (apparently... details are still undocumented I guess) to HEAP, which is to say memory allocation via malloc() or the equivalent. Note that heap is not the same as stack, but I suspect if you tried allocating too much stack space for a thread then the 3MB limit would apply there too.

The app won't get closed, but will get an allocation failure (e.g. malloc returning NULL) and that may well crash your app if your code or any library routine doesn't handle this well, or handles it by exiting the process.

It does not appear that memory allocated to load libraries is covered by this limit. That's probably why you see what looks like more than 3MB allocated... some of it is not considered to be heap allocations.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
BlackBerry Development Advisor
shaque
Posts: 217
Registered: ‎07-14-2008

Re: Headless Apps - Resource management - 3 MB RAM

Peter is correct. Memoory limit is only applied to your heap, i.e. only your dynamic allocations are counted towards it. If you want to see the heap size of a running application (debug-builds only), simply open the malloc view in the IDE and select the running app in the target navigator view.
Please use plain text.
Developer
smiley
Posts: 1,360
Registered: ‎07-14-2008
My Carrier: Fido

Re: Headless Apps - Resource management - 3 MB RAM


shaque wrote:
Peter is correct. Memoory limit is only applied to your heap, i.e. only your dynamic allocations are counted towards it. If you want to see the heap size of a running application (debug-builds only), simply open the malloc view in the IDE and select the running app in the target navigator view.

I have compiled both apps for debug and when I view the malloc it says No Data for both apps.  what is wrong?

Please use plain text.
Developer
helex
Posts: 256
Registered: ‎02-14-2012
My Carrier: -

Re: Headless Apps - Resource management - 3 MB RAM

[ Edited ]

Thanks so far, especially to peter! :smileyhappy:

 

The first rudimentary headless app got approved for BlackBerry World in the meantime. So it seems to work fine.


smiley wrote:

I have compiled both apps for debug and when I view the malloc it says No Data for both apps.  what is wrong?


I have the same issue. But more important for me would be to know how to get the amount of used HEAP for my headless app during the runtime, is this possible to check from within the app?

 

I would like to keep some data in a buffer and write it to a SQLite database as soon as the used HEAP exceeds during the runtime a 2 MB limit, just to don't waste battery power. Any ideas?

 

Also, has a headless module enough time to write some data to the disk as soon as a device gets rebooted or will the system terminate it instantly?

Please use plain text.
BlackBerry Development Advisor
jomurray
Posts: 104
Registered: ‎09-06-2010
My Carrier: Vodafone UK

Re: Headless Apps - Resource management - 3 MB RAM

You might want to take a look at getrlimit() here: http://developer.blackberry.com/native/reference/core/com.qnx.doc.neutrino.lib_ref/topic/g/getrlimit....

 

I added it like this to the headless part of an app of mine and ran it:

 

#include <sys/resource.h>

...
    
struct rlimit rlp;

if (getrlimit(RLIMIT_DATA , &rlp) == EOK) {
	qDebug() << "getrlimit() OK" << endl;
	qDebug() << "rlim_cur: " << rlp.rlim_cur << endl;
	qDebug() << "rlim_cur_hi: " << rlp.rlim_cur_hi << endl;
	qDebug() << "rlim_max: " << rlp.rlim_max << endl;
	qDebug() << "rlim_max_hi: " << rlp.rlim_max_hi << endl;
} else {
	qDebug() << "getrlimit() FAIL " << strerror(errno) << endl;
}

 

This is the output:

 

getrlimit() OK 
rlim_cur:  3145728 
rlim_cur_hi:  0 
rlim_max:  3145728 
rlim_max_hi:  0 

 This shows that the "current" and "hard" limits on the maximum size of the process's data segment (initialized data, uninitialized data, and heap) ofmy headless app is indeed 3MB.

---------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Please use plain text.
Developer
peter9477
Posts: 6,448
Registered: ‎12-08-2010
My Carrier: none

Re: Headless Apps - Resource management - 3 MB RAM

[ Edited ]

helex wrote:

 

Also, has a headless module enough time to write some data to the disk as soon as a device gets rebooted or will the system terminate it instantly?


If your app gets the quit signal, you should certainly have enough time to flush some data to disk.

 

I haven't tested with the background service to verify that it gets a Quit on shutdown, but I expect it would. The GUI portion of an app certainly does, though I'm not aware of any way to distinguish between a regular "close app" request by a user, and system shutdown.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
BlackBerry Development Advisor
jomurray
Posts: 104
Registered: ‎09-06-2010
My Carrier: Vodafone UK

Re: Headless Apps - Resource management - 3 MB RAM

@peter9477 - yes, I've checked that you do indeed get the QCoreApplication.aboutToQuit() SIGNAL() in the background service component of a headless application. I've been persisting lifecycle information like this in QSettings() keys in my own headless app just to check :-).

---------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Please use plain text.
Developer
helex
Posts: 256
Registered: ‎02-14-2012
My Carrier: -

Re: Headless Apps - Resource management - 3 MB RAM


jomurray wrote:

You might want to take a look at getrlimit() here: http://developer.blackberry.com/native/reference/core/com.qnx.doc.neutrino.lib_ref/topic/g/getrlimit....

[...]

 This shows that the "current" and "hard" limits on the maximum size of the process's data segment (initialized data, uninitialized data, and heap) ofmy headless app is indeed 3MB.


Confirmed, same over here.

 

I read thru the document and several other parts of the documentation but I haven't found any parameter which seems to give me a reasonable indication how much of the RLIMIT_DATA my app actually uses at the moment.

 

It would be not only useful to know, in my opinion it would be the best trigger to write cached data to the disk and free up some memory.

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 651
Registered: ‎11-29-2011
My Carrier: other

Re: Headless Apps - Resource management - 3 MB RAM

I'll take a look and see if I can dig this up.  I seem to recall playing with it before.

This info is pulled by pidin by accessing /proc or similar, so there is a kernel interface for figuring it out.  If I recall, it is not very straightforward though :smileyhappy:

Please use plain text.