Creating native threads

789 views
Skip to first unread message

Srdjan Pantic

unread,
Nov 16, 2010, 12:37:30 AM11/16/10
to android-ndk
I have an application that heavily uses NDK. It creates seven native
threads and four of them are doing HTTP connections. For some reason
sometime sometimes thread crashes on outbound HTTP connection without
stack trace.

UI is based on WebView if you wonder. I am adding SIG handlers to
intercept this crash, but I cannot see why it would fail.

By default I was using stack size of 8 KB which was fine on some other
embedded systems. Increasing it to 32 KB helped a lot. What should be
recommended stack size for native threads?

I/native_log ( 3077): POST https://repository.pantic.info/log/collector
I/native_log ( 3077): connect()
I/WindowManager( 98): WIN DEATH: Window{462a43e0 info.pantic.client/
info.pantic.WebViewActivity paused=false}
I/ActivityManager( 98): Process info.pantic.client (pid 3077) has
died.
D/Zygote ( 64): Process 3077 terminated by signal (11)

Dianne Hackborn

unread,
Nov 16, 2010, 1:05:54 PM11/16/10
to andro...@googlegroups.com
The required stack size depends entirely on what you are doing in that thread.  Since the NDK doesn't have an HTTP stack, I can't say what is required by whatever HTTP stack you are using.

Also there is generally little benefit to giving yourself trouble by trying to keep stacks small.  The space reserved for the stack doesn't take RAM until it is actually needed; it is just reserved address space.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Srdjan Pantic

unread,
Nov 17, 2010, 1:18:18 PM11/17/10
to android-ndk
Thank you Dianne,

On Nov 16, 10:05 am, Dianne Hackborn <hack...@android.com> wrote:
> The required stack size depends entirely on what you are doing in that
> thread.  Since the NDK doesn't have an HTTP stack, I can't say what is
> required by whatever HTTP stack you are using.

I am using CURL 7.21.0 with OpenSSL 1.0. Both of course compiled as
static
libraries with my shared library loaded by Android application. Most
of connections
are HTTPS. And URLs are defined as FQDN, requiring DNS lookup.

Based on profiling it does look that it takes more memory that I
expected.

> Also there is generally little benefit to giving yourself trouble by trying
> to keep stacks small.  The space reserved for the stack doesn't take RAM
> until it is actually needed; it is just reserved address space.

Thanks, but what exactly is considered small on Android?

> On Mon, Nov 15, 2010 at 9:37 PM, Srdjan Pantic <srd...@pantic.info> wrote:
> > I have an application that heavily uses NDK. It creates seven native
> > threads and four of them are doing HTTP connections. For some reason
> > sometime sometimes thread crashes on outbound HTTP connection without
> > stack trace.
>
> > UI is based on WebView if you wonder. I am adding SIG handlers to
> > intercept this crash, but I cannot see why it would fail.
>
> > By default I was using stack size of 8 KB which was fine on some other
> > embedded systems. Increasing it to 32 KB helped a lot. What should be
> > recommended stack size for native threads?
>
> > I/native_log ( 3077): POSThttps://repository.pantic.info/log/collector
> > I/native_log ( 3077): connect()
> > I/WindowManager( 98): WIN DEATH: Window{462a43e0 info.pantic.client/
> > info.pantic.WebViewActivity paused=false}
> > I/ActivityManager( 98): Process info.pantic.client (pid 3077) has
> > died.
> > D/Zygote ( 64): Process 3077 terminated by signal (11)
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Dianne Hackborn

unread,
Nov 18, 2010, 12:57:00 AM11/18/10
to andro...@googlegroups.com
On Wed, Nov 17, 2010 at 10:18 AM, Srdjan Pantic <srd...@pantic.info> wrote:
> Also there is generally little benefit to giving yourself trouble by trying
> to keep stacks small.  The space reserved for the stack doesn't take RAM
> until it is actually needed; it is just reserved address space.
 Thanks, but what exactly is considered small on Android?

Well I would certainly consider 8KB pretty small.

I can't help you with how big your stack *needs* to be, since I don't know anything about these libraries you are using.

--
Dianne Hackborn
Android framework engineer
hac...@android.com

David Turner

unread,
Nov 18, 2010, 10:30:20 AM11/18/10
to andro...@googlegroups.com
Why don't you try to use the default stack sizes and see if it changes anything?
As Dianne said, stack RAM usage will only grow as big as your application needs it (and most native code is unable to deal with a stack overflow anyway).

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

Srdjan Pantic

unread,
Nov 18, 2010, 2:13:15 PM11/18/10
to android-ndk
You mean 1 MB? Well, as I mentioned this is porting from existing code
base that was
optimized for low memory devices and with same libraries 8 to 16 KB
where more than enough.

I raised stack size to 64 KB when I sent my question and I am not
seeing problem anymore so far.
When I was debugging issue I found that problem was when DNS
resolution was required
which surprised me.

Dianne, what your suggestion would be here? Use default stack size?

--Srdjan

On Nov 18, 7:30 am, David Turner <di...@android.com> wrote:
> Why don't you try to use the default stack sizes and see if it changes
> anything?
> As Dianne said, stack RAM usage will only grow as big as your application
> needs it (and most native code is unable to deal with a stack overflow
> anyway).
>
> On Tue, Nov 16, 2010 at 6:37 AM, Srdjan Pantic <srd...@pantic.info> wrote:
> > I have an application that heavily uses NDK. It creates seven native
> > threads and four of them are doing HTTP connections. For some reason
> > sometime sometimes thread crashes on outbound HTTP connection without
> > stack trace.
>
> > UI is based on WebView if you wonder. I am adding SIG handlers to
> > intercept this crash, but I cannot see why it would fail.
>
> > By default I was using stack size of 8 KB which was fine on some other
> > embedded systems. Increasing it to 32 KB helped a lot. What should be
> > recommended stack size for native threads?
>
> > I/native_log ( 3077): POSThttps://repository.pantic.info/log/collector
> > I/native_log ( 3077): connect()
> > I/WindowManager( 98): WIN DEATH: Window{462a43e0 info.pantic.client/
> > info.pantic.WebViewActivity paused=false}
> > I/ActivityManager( 98): Process info.pantic.client (pid 3077) has
> > died.
> > D/Zygote ( 64): Process 3077 terminated by signal (11)
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .

Dianne Hackborn

unread,
Nov 18, 2010, 3:18:28 PM11/18/10
to andro...@googlegroups.com
All you are doing by trying to tune the stack size is optimizing your process's address space usage...  which seems to me not worth doing.

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Bytes

unread,
Nov 19, 2010, 12:48:14 AM11/19/10
to android-ndk
R you using pthreads ?
> > > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Bunsubscribe@goo­glegroups.com>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.- Hide quoted text -
>
> - Show quoted text -

David Turner

unread,
Nov 19, 2010, 11:57:39 AM11/19/10
to andro...@googlegroups.com
On Thu, Nov 18, 2010 at 8:13 PM, Srdjan Pantic <srd...@pantic.info> wrote:
You mean 1 MB? Well, as I mentioned this is porting from existing code
base that was
optimized for low memory devices and with same libraries 8 to 16 KB
where more than enough.


The 1 MB is virtual address space. It doesn't use any RAM unless you stack grows.

In other words, you have "up-to to 1 MB" of address space reserved for your stacks, but the actual RAM pages are allocated on demand by the kernel.
If your core only needs 16KB for its stack needs, then it will only use 16KB of RAM (due to granularity of 4KB per page).
 
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages