4

I believe explaining the context surrounding this problem will only muddle things up, so I will cut straight to the chase:

If an Android application allots a small amount of native memory through JNI, and then looses the reference to that memory (i.e., the variable references to the objects that manage that native memory become null and will throw a null pointer exception if one attempts to use them):

  1. Is it really a big deal if this occurs once and only once during the entire life cycle of the app?

  2. Won't the memory eventually be overwritten and thus reclaimed anyway?

I understand it is a bad practice to allow memory leakage. Frankly, I fully understand this. However, I also know that there are rare occasions when a rule should be broken (to paraphrase Joshua Bloch's words from Effective Java). Respectfully, I don't want to get into a discussion about why I am doing this, I only seek an answer to this specific problem.

Thank You!!! Chris

orb
  • 1,263
  • 1
  • 10
  • 16
  • *"...a small amount of native memory..."*: *how* small? Are we talking about a couple of kilobytes? Tens of kilobytes? Megabytes? – thkala Feb 27 '13 at 16:48

1 Answers1

2

Is it really a big deal if this occurs once and only once during the entire life cycle of the app?

For most people, it would depend on what "a small amount of native memory" means for you. If we are talking about a couple of KB then I would not worry about it when it's lost once. If, on the other hand, we are talking about a couple of MB, things are probably different...

That said, having a known memory leak is a sign of potential design problems that may also cause other issues. It may also become more of an issue if a future refactoring changes the architecture of the application.

Won't the memory eventually be overwritten and thus reclaimed anyway?

Sure... eventually. When the process terminates, the kernel will free all allocated resources. I am not really familiar with the inner workings of Android, but it is my understanding that processes tend to be more long-lived than on a desktop Linux system. For embedded devices with little memory and no swap space, that might be a problem - especially if all application writers start leaving memory leaks all over the place.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • I am under the understanding that Native memory allocated through JNI is allocated to the same process as the VM. Thus, when the App is destroyed, its VM shuts down, that VM's process terminates, and the kernel will then release any and all memory allocated to that process. However, as you were saying, if many developers begin to let one memory leak go there could be significant problems. Also, to let the memory leak slide is probably just plain sloppy, even if solving the leak would require hours of digging into code to solve the problem. – orb Feb 27 '13 at 21:20