简体   繁体   English

Java字符串数组的内存使用情况

[英]Memory usage of a Java String Array

How much space does a java string array take up? java字符串数组占用多少空间? More specifically, how much space (in bytes) does a string array that looks something like this: 更具体地说,字符串数组看起来像这样的空间(以字节为单位):

Longbowman 
kingdom   
lord    
weapon1,weapon2,weapon3    
armor1,armor2,armor3

I ask this because I'm going to create a program that has 5,000+ of these arrays and want to know how much space it'll take up so I'll know if I should rework the data storage. 我问这个是因为我要创建一个包含5,000多个这些数组的程序,并想知道它将占用多少空间,所以我知道是否应该重做数据存储。

So to recap, how much space does a string array (if its quantifiable) take up? 那么回顾一下,字符串数组(如果可量化的话)占用了多少空间?

The string array is just an array of references - an array of size N will take approximately (N * 4 + 20) or (N * 8 + 20) bytes depending on the size of a reference in your JVM. 字符串数组只是一个引用数组 - 大小为N的数组将占用大约(N * 4 + 20)或(N * 8 + 20)个字节,具体取决于JVM中引用的大小。

If you're interested in your total storage, you should work out how many separate String objects you've got, and also how many arrays you've got. 如果你有兴趣在你的总存储,你应该工作,你有多少独立的String对象了, 而且你有多少阵列得到。 If you've got 5000 arrays but they mostly contain references to the same few strings, it's likely to be fine. 如果你有5000个数组,但它们大多包含对相同几个字符串的引用,那么它很可能会很好。 If you've got 5000 arrays each of which contains 5 strings which aren't used anywhere else, that's 25,000 strings... which still probably isn't very much (a string of length 20 will probably take about 60 bytes). 如果你有5000个数组,每个数组包含5个字符串,这些字符串在其他任何地方都没有使用,那就是25,000个字符串......这可能也不是很多(长度为20的字符串可能需要大约60个字节)。

Of course, context matters here: what's your code going to run on? 当然,上下文在这里很重要:你的代码会运行什么? If it's running on a desktop PC, than taking a few megs probably isn't a problem... it might be more of an issue on a phone. 如果它在台式电脑上运行,那么取一些兆可能不是问题......这可能是手机上的一个问题。

String arrays can take as much as you want? 字符串数组可以随意使用吗? you define the size of the String array. 您定义String数组的大小。 String[] abc = new String [80]. String [] abc = new String [80]。 But i suppose you already know this.. maybe i did not get your question. 但我想你已经知道了......也许我没有得到你的问题。

1 char = 2 bytes not 4 bytes since Java uses 16 bit unicode - is this what you are looking for? 1 char = 2个字节而不是4个字节,因为Java使用16位unicode - 这就是你要找的东西吗?

Integer.MAX_VALUE or available heap ? Integer.MAX_VALUE还是可用堆? maybe the max size available? 也许最大尺寸可用?

It's not easy to answer since a String is a complex object with many fields. 回答并不容易,因为String是一个包含许多字段的复杂对象。 It's not only a matter of how many chars there are. 这不仅仅是有多少个字符的问题。 And it also depends on how much memory is available on your system. 它还取决于系统上可用的内存量。 On a 64GB RAM server is it a problem ? 在64GB RAM服务器上是一个问题吗? No. And on a mobile phone ? 不,在手机上? Yes. 是。 Too many variables may influence the answer. 变量太多可能会影响答案。

You are driving your code by optimization and that's not the way it should be. 您正在通过优化来驱动您的代码,而这不是应该的方式。 You should drive your code by what you functionally want to do. 您应该根据您的功能来驱动您的代码。

The only way to accurately measure memory usage is to use a memory profiler. 准确测量内存使用情况的唯一方法是使用内存分析器。

I've written a simple program that allocates 5,000 arrays that match your description. 我写了一个简单的程序,分配5,000个符合你描述的数组。 I've then measured its memory usage with YourKit. 然后我用YourKit测量了它的内存使用情况。

The amount of memory used by the arrays varied by a factor of ten: 阵列使用的内存量相差十倍:

  1. If all arrays use the same string literals, in total they take up about 200KB of RAM. 如果所有数组使用相同的字符串文字,则总共占用大约200KB的RAM。
  2. If each array contains unique randomly-generated strings (of the same lengths as in the first case), they take up about 2MB of RAM. 如果每个数组包含独特的随机生成的字符串(长度与第一种情况相同),则它们占用大约2MB的RAM。

Jon Skeet is correct (as virtually always), but you may be able to greatly reduce your memory usage by using references to enums and EnumSets instead of the "untyped" String representations of your objects you are proposing. Jon Skeet是正确的(几乎总是如此),但您可以通过使用对enumsEnumSets引用而不是您提议的对象的“无类型”字符串表示来大大减少内存使用量。

Using typed data, especially enums , is also good coding practice. 使用类型化数据,特别是enums ,也是很好的编码实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM