6

I am designing a function (Java method) which will be executed 40-80 times per second on a mobile device.

I want to avoid producing a ton of dead variables which get collected by GC, as the function runs (possibly throughout the life of the app).

In C I might use volatile for example, to prevent the memory allocation of my variables in each execution of the function... I want to do something similar in Java but don't know how.

The function stores data in

  • 1 string
  • 4 integers
  • 2 1-dimensional String arrays

In general, in Java, what is the preferred method for using the above variables but not re-allocating them every time my function is executed (40+ times per second)?

Member variables would "work" but is this the best solution?

Thanks! Brad

jmj
  • 225,392
  • 41
  • 383
  • 426
Brad Hein
  • 10,689
  • 11
  • 48
  • 71
  • does it have to be re-entrant? – Nim Oct 12 '10 at 15:29
  • 2
    Curious what you are doing 40+ times a second on a mobile device? – Aaron McIver Oct 12 '10 at 15:32
  • 2
    @Brad: "variables" aren't GC'd: objects are. It's important to know the difference. – Mark Peters Oct 12 '10 at 15:47
  • 1
    80hz = 12.5 ms / invocation, so I suppose your time budget is about 1 to 5 ms. I would try to just code it cleanly, and then run it flat out in a long loop. While running it, I would use the random-pause technique to see what parts of it could be improved. Not sure what you're doing, but 1ms is a pretty healthy amount of time. – Mike Dunlavey Oct 12 '10 at 16:30
  • The function won't be called concurrently so it doesn't have to be re-entrant. – Brad Hein Oct 12 '10 at 17:00
  • @Mike - +1 for random pause recommendation, can you please explain or provide a link? – Brad Hein Oct 12 '10 at 17:03
  • @org.life.java why did you add tag Java-ME this is not Java ME. – Brad Hein Oct 12 '10 at 17:05
  • @Brad: Thx. You want the basic explanation (http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux/378024#378024) or the blow-by-blow (http://stackoverflow.com/questions/926266/performance-optimization-strategies-of-last-resort/927773#927773) or the general flame (http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343) ? :-) – Mike Dunlavey Oct 12 '10 at 19:05
  • Would 'volatile' really have that effect in C? Don't you mean 'register'? – user207421 Oct 12 '10 at 23:24

5 Answers5

3
  • Wrap those fields in a class {Java loves to see the Object} and allocate it once and use it.
  • keep string pool concept in mind as you have String array
jmj
  • 225,392
  • 41
  • 383
  • 426
  • -1. Using a static field has absolutely nothing to do with how often something is allocated. The ONLY relevant thing is how often objects are created (e.g. how often the `new` operator is used) – Mark Peters Oct 12 '10 at 15:45
  • @Mark that is equally true.check my answer – jmj Oct 12 '10 at 15:49
  • Static means it won't change. The problem is, the variables are calculated during each call to the function. – Brad Hein Oct 12 '10 at 16:54
  • @Brad ..check now.static also solves the purpose , but here it is better suited as Object – jmj Oct 12 '10 at 16:55
  • OK I like the idea of bundling the variables into a class - keeps things clean, and allows for future growth (such as multithreadding support) And I'll read up on string pools. Thanks. – Brad Hein Oct 12 '10 at 17:17
1

Static member vars, they won't get unloaded until the class is unloaded. Keep in mind that if all references to the class are lost it is possible that it can be GC'ed. I doubt that will be an issue in your case however it is worth noting. In addition if you are creating new instances of the class containing the static members vars you will be in the same boat from an allocation stance.

Aaron McIver
  • 23,797
  • 5
  • 53
  • 83
0

I totally agree with that answer.

Every call to your function would allocate more memory if your creating the variables on the fly as Java's GC doesn't fully clean up until destroy is called when the class is being disposed.

But if your going to be calling the function multiple times then just making them member variables of the class would also work.

daynier
  • 99
  • 1
  • 3
0

You could use static variables for this, but that assumes that these variables are constants or that changes to them to not badly influence other threads currently calling the same function.

If your code must be reentrant and static variables are not an option, you can create a simple data-holder object which holds these variables and pass it as argument to your function. Your calling environment can decide whether to share these objects or not.

rsp
  • 22,242
  • 6
  • 51
  • 64
0

Use either static class fields, or if you are going to create only one instance of your class normal member variables will work.

If you need to change the contents of the String consider using a StringBuilder instead of immutable String instances that will be created/gc'ed.

int's are primitives, so they are not a problem.

Your String arrays will be okay, but think about what you are putting into them. Are you constructing new String objects and letting old ones gc?

Darron
  • 20,463
  • 5
  • 47
  • 53