如何在数组中存储10pow9个整数。 [英] How do I store 10pow9 number of integers in an array.

查看:77
本文介绍了如何在数组中存储10pow9个整数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须存储10pow9个整数。当我尝试将其存储在数组中时,它会产生运行时错误。那么我应该用什么而不是数组来避免运行时错误。



我尝试过:



我尝试全局声明数组,但这不起作用。

I have to store 10pow9 number of integers. When i tried storing it in array, it is giving run time error. So what should i used instead of array to avoid run time error.

What I have tried:

I tried declaring array globally, but that is not working.

推荐答案

这不是一个如何做到这一点的情况?作为你为什么要这样做?

即使是32位整数(如Dave所说),4GB的数据对于单个物体而言是一个惊人的数量。如果这是一个移动应用程序(尽管很可能,因为它主要用于Android的Java)我会说你能做的最好的事情就是查看你的应用程序以及它为什么需要随机访问那么多数据!



如果您实际上不需要随机访问它,那么您可能会考虑使用磁盘文件,并将其寻找到正确的位置而不是试图保持这一切都在记忆中。很可能数据已经从某个地方获取,并且可能直接访问某个地方而不是将其全部拉入内存并在那里进行操作。



我当然不会首先设计一个系统而不需要一个4GB阵列而不先探索其他途径!
It's not so much a case of "how do I do this?" as "why are you trying to do this?"
Even as 32 bit integers (as Dave says) that's 4GB of data which is a spectacular amount for a single object. And if this is a mobile app (as is quite possible, given it's Java which is mostly used for Android) I'd say that the best thing you can do is look at your application and why it needs random access to that much data!

If you don't actually need to access it randomly, then you might consider using a disk file instead, and seeking that to the right place instead of trying to keep all this in memory. The chances are that the data is already being sourced from somewhere and it may be possible to access the somewhere directly instead of pulling it all into memory and manipulating it there.

I certainly wouldn't start off by designing a system than needed a 4GB array without exploring other avenues first!


Java支持包含10亿个项目的数组。但是这样的数组会消耗4 GB的内存,这可能高于默认的最大堆大小,这取决于操作系统和Java版本以及可用的内存量。



您可以使用 -Xmx 选项设置最大堆大小。但它不应超过可用内存量。因此,当您的系统中安装了至少8 GB的RAM时,您可以尝试使用示例 -Xmx5g 。另请参见调整Java虚拟机(JVM) [ ^ ]。
Java supports arrays with 1 billion items. But such an array would consume 4 GB of memory which is probably above the default maximum heap size which depends on the OS and Java version and the available amount of memory.

You can set the maximum heap size with the -Xmx option. But it should not exceed the amount of available memory. So you might try for example -Xmx5g when you have at least 8 GB of RAM installed in your system. See also Tuning Java Virtual Machines (JVMs)[^].


这篇关于如何在数组中存储10pow9个整数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆