Java 字符串数组的内存使用情况
java字符串数组占用多少空间? 更具体地说,一个看起来像这样的字符串数组需要多少空间(以字节为单位):
Longbowman
kingdom
lord
weapon1,weapon2,weapon3
armor1,armor2,armor3
我问这个是因为我要创建一个包含 5,000 多个这些数组的程序,并且想知道它需要多少空间以便我知道是否应该重新处理数据存储。
回顾一下,字符串数组(如果可量化)占用多少空间?
How much space does a java string array take up?
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.
So to recap, how much space does a string array (if its quantifiable) take up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
字符串 array 只是一个引用数组 - 大小为 N 的数组大约需要 (N * 4 + 20) 或 (N * 8 + 20) 个字节,具体取决于您的引用的大小JVM。
如果您对总存储空间感兴趣,您应该计算出您有多少个单独的 String 对象,以及您有多少个数组。如果您有 5000 个数组,但它们大多包含对相同几个字符串的引用,那么可能没问题。如果您有 5000 个数组,每个数组包含 5 个在其他地方未使用的字符串,则为 25,000 个字符串...这可能仍然不是很多(长度为 20 的字符串可能需要大约 60 个字节)。
当然,上下文在这里很重要:您的代码将在什么上运行?如果它在台式电脑上运行,那么花费几兆可能不是问题……在手机上可能会更成问题。
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.
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. If you've got 5000 arrays but they mostly contain references to the same few strings, it's likely to be fine. 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).
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.
字符串数组能取多少就取多少?您定义字符串数组的大小。
字符串[] abc = 新字符串[80]。
但我想你已经知道了......也许我没有得到你的问题。
1 个字符 = 2 个字节而不是 4 个字节,因为 Java 使用 16 位 unicode - 这是您正在寻找的吗?
Integer.MAX_VALUE 或可用堆?也许可用的最大尺寸?
String arrays can take as much as you want? you define the size of the String array.
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?
Integer.MAX_VALUE or available heap ? maybe the max size available?
这个问题并不容易回答,因为字符串是一个具有许多字段的复杂对象。这不仅仅是有多少个字符的问题。它还取决于您的系统上有多少可用内存。在 64GB RAM 服务器上这是一个问题吗?不。那么在手机上呢?是的。太多的变量可能会影响答案。
您正在通过优化来驱动代码,但这不是应该的方式。您应该按照您在功能上想要执行的操作来驱动代码。
It's not easy to answer since a String is a complex object with many fields. 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 ? 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.
准确测量内存使用情况的唯一方法是使用内存分析器。
我编写了一个简单的程序,它分配 5,000 个与您的描述相匹配的数组。然后我用 YourKit 测量了它的内存使用情况。
数组使用的内存量相差十倍:
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. I've then measured its memory usage with YourKit.
The amount of memory used by the arrays varied by a factor of ten:
Jon Skeet 是正确的(几乎总是如此),但是您可以通过使用对
enums
和EnumSets
的引用而不是“无类型”字符串表示来大大减少内存使用量您所提议的对象。使用类型化数据,尤其是
枚举
,也是很好的编码实践。Jon Skeet is correct (as virtually always), but you may be able to greatly reduce your memory usage by using references to
enums
andEnumSets
instead of the "untyped" String representations of your objects you are proposing.Using typed data, especially
enums
, is also good coding practice.