Which is memory efficient: char [] or String?

I am developing an Android application. The main requirement of the application is that it must be memory efficient. So which one should I continue with?

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

or

 char batterylevel[]; batterylevel = Float.toString(batteryPct).toCharArray(); 
+6
source share
3 answers

In Oracle JDK, a row has four instance level fields:

  • Character array
  • Integral bias
  • Integral Character Count
  • Integrated Hash Value

This means that each line introduces an additional reference to the object (the line itself) and three integers in addition to the character array itself. (There is an offset and number of characters to allow sharing an array of characters among String instances created using the String # substring () methods, a design choice that some other Java library developers have avoided.) In addition to the additional storage costs, there is also one higher access level, not to mention border checking, with which String protects its character array.

Lines are immutable. This means that after you have created a string, if another process can flush memory, there is no way (other than reflection), you can get rid of the data before the GC hits, which means "memory loss".

With an array, you can explicitly erase the data after you are done with it: you can rewrite the array to anything.

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

+2
source

String is a class In java and it carries a char array, as you can see in the code

 private final char value[]; 

so every time you create a String object, it is supported by the char array. and also the String class has three different fields, because it takes up more memory than the char array.

but if you see usage, then String is better than a char array because it is immutable. you do not need to do any memory management for String. Performance array char array [] is faster for multiple operations (if you are executing huge data).

0
source

CString in addition to an array of characters (or wide characters) contains the size of the string, the size of the allocated buffer, and the reference count (serving additionally as a lock flag). A buffer containing an array of characters can be significantly larger than the string contained in it, which reduces the number of calls associated with time consumption. Also, when a CString is zero size, it still contains two wchar characters.

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

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

PS C ++ string comparison with char []

A source

0
source

Source: https://habr.com/ru/post/983944/


All Articles