title: Android activity lifecycle and jni memory management
body: <p>I am trying to make an android application, which uses some jni code.<br>
  This jni code allocates some memory, which has to be freed. Let`s<br>
  suppose I have this class containing this code:</p>
  
  <pre><code>class MyJniClass{
  
  public native void jni_init(); //this allocates some memory
  public native void do_something(); //this also allocates some memory,  
  which is linked to the memory allocated in jni_init()
  public native void jni_destroy(); //this frees all the allocated  
  memory in two previous functions
  
  ... // some other java code
  
  }
  </code></pre>
  
  <p>My trouble is, where in activity lifecycle to call <code>jni_init</code> and <code>jni_destroy</code>. So I started some digging how exactly the activity lifecycle works and I am a little bit confused about the results. I suppose that this is caused by the fact, that I am trying to<br>
  understand quite deeply to the level of linux processes. But I am using jni, so it makes some sense.</p>
  
  <p>I created a simple activity, implemented all the lifecycle callbacks and put some logging into them. I did the same with the constructor and I also added jni code, which logs the <code>pid</code> of the process.</p>
  
  <p>So, what I found
   - the activity can be destroyed even if onDestroy is not called (this is clear, I found lots of explanations for this) and the process is still runnig (verified by apps like Usage Timelines, ps command and the getpid jni call). The process is running, so it seems logical to keep the memory allocated.</p>
  
  <p>The problem is, where to allocate the memory. I cannnot use <code>onCreate</code>, because it can called several times during the process lifetime, so memory leaks can happen. I also don't want to use onStart for allocation and onStop for freeing, because it unnecessary work to do. I tried the singleton design, it seems to work, but I don't want to use it, because I need MyJniClass to be reentrant (I want to use it also in a service). So my question is - is there some recommended way how to do memory management in jni apps?</p>
  
  <p>Edit:
  I see I didn`t describe the most confusing part of this clearly enough. Here is the sequens of events:</p>
  
  <p>1) I start the app using the icon in launcher</p>
  
  <p>2) new activity is created (constructed)</p>
  
  <p>3) onCreate is called</p>
  
  <p>4) onStart is called</p>
  
  <p>5) onResume is called</p>
  
  <p>6) I press the home button</p>
  
  <p>7) onSaveInstanceState is called</p>
  
  <p>8) onPause is called</p>
  
  <p>9) onStop is called</p>
  
  <p>10) I start the app using the icon in launcher (as in step 1)</p>
  
  <p>11) new activity is created (constructed)</p>
  
  <p>12) onCreate is called</p>
  
  <p>13) onStart is called</p>
  
  <p>14) onResume is called</p>
  
  <p>The confusing steps are 11) and 12) because the process (pid) is still the same, but as I understand from documentation, the previous process should be killed if 11) and 12) happen.</p>
  
  <p>Thanks and sorry for all the grammar mistakes.</p>