6

I am developing an Android application. The primary requirement of the application is that it should be efficient in terms of memory. So, which of these should I proceed with?

String batterylevel;
batterylevel = Float.toString(batteryPct);

or

char batterylevel[];
batterylevel = Float.toString(batteryPct).toCharArray();
5
  • How often are you checking the battery level, such that ~16 bytes per check makes a noticeable difference? Commented Mar 18, 2015 at 3:58
  • 1
    There is a refresh button to refresh the battery level. So, it totally depends on the user. Though, no noticeable difference. Commented Mar 18, 2015 at 4:00
  • The Android system is probably allocating more memory than that just to make the button orange when the user clicks on it. How concerned do you want to be about memory use? (And when an Android application has memory problems, it's usually from loading megabytes of images) Commented Mar 18, 2015 at 4:02
  • Well, there are just 20 Strings that I am concerned about. I think the instance variable is also allocated whenever an object of String class is created. Will 20 such cases make a huge difference? Commented Mar 18, 2015 at 4:06
  • 2
    3 extra int fields (32bit) + 1 reference (64bit) per string = 12+8 = 20. 20 * 20 = 400 bytes extra. You would save the incredible amount of 0.0004 megabytes. I think you are looking to optimize something that's just not worth it at all. Also notice that the toCharArray call creates a copy of the original String inner buffer. This means that as long as the garbage collector won't free up the memory you are actually using more memory (read: double the memory). I suggest you just use the String approach. Commented Mar 18, 2015 at 6:54

3 Answers 3

2

In Oracle's JDK a String has four instance-level fields:

  • A character array
  • An integral offset
  • An integral character count
  • An integral hash value

That means that each String introduces an extra object reference (the String itself), and three integers in addition to the character array itself. (The offset and character count are there to allow sharing of the character array among String instances produced through the String#substring() methods, a design choice that some other Java library implementers have eschewed.) Beyond the extra storage cost, there's also one more level of access indirection, not to mention the bounds checking with which the String guards its character array.

Strings are immutable. That means once you have created the string, if another process can dump memory, there's no way (aside from reflection) you can get rid of the data before GC kicks in which means waste of memory.

With an array, you can explicitly wipe the data after you're done with it: you can overwrite the array with anything you like.

So as far as I can conclude is char[] is better in terms of memory for your case.

Sign up to request clarification or add additional context in comments.

Comments

0

String is class In java and it wraps char array as you can see in code

private final char value[];

so every time you create a String object it is backed by char array. and also String class is having three different fields because of that it is taking more memory than char array.

but if you see usage then String is better than char array because its immutable. you don't have to do any memory management for String. Performance wise char array[] is faster for few operations (if you are performing on huge data).

Comments

0

CString in addition to array of chars (or wide chars) contains string size, allocated buffer size, and reference counter (serving additionally as a lock flag). The buffer containing the array of chars may be significantly larger than the string it contains -- it allows to reduce the number of time-costly allocation calls. In addition, when the CString is set to be zero-sized, it still contains two wchar characters.

Naturally, when you compare the size of CString with the size of corresponding C-style array, the array will be smaller. However, if you want to manipulate your string as extensively as CString allows, you will eventually define your own variables for string size, buffer size and sometimes refcounter and/or guard flags. Indeed, you need to store your string size to avoid calling strlen each time you need it. You need to store separately your buffer size if you allow your buffer to be larger than the string length, and avoid calling reallocs each time you add to or subtract from the string. And so on -- you trade some small size increase for significant increases in speed, safety and functionality.

So, the answer depends on what you are going to do with the string. Suppose you want a string to store the name of your class for logging -- there a C-style string (const and static) will do fine. If you need a string to manipulate and use it extensively with MFC or ATL-related classes, use CString family types. If you need to manipulate string in the "engine" parts of your application that are isolated from its interface, and may be converted to other platforms, use std::string or write your own string type to suit your particular needs (this can be really useful when you write the "glue" code to place between the interface and the engine, otherwise std::string is preferable).

P.S Comparing String of C++ with char[]

Source

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.