使用巨大的ArrayList,Java OutOfMemoryError:Java堆空间...使用db?
我有一个记忆问题,我完全理解原因,但不知道解决办法。我尝试使用 -Xmx2g 标签并使堆大小更大,但似乎存在隐藏的最大值。 (如果我使用 -Xmx512m 我同时耗尽空间)。
假设我有 2 个对象,一个区域和一个用户。我的 Area 对象保存一个用户的 ArrayList:
public class Area {
int numUsers;
ArrayList<User> userList;
}
我的 User 类保存一个朋友的 ArrayList:
public class User {
int userID;
int numFriends;
ArrayList<User> friends;
}
仅使用一个 Area,拥有 100 万个用户,平均每个用户有 200 个朋友,在创建大约 680,000 个用户后,我用完了堆空间。显然,如果我将朋友/用户的平均数量降低到接近 100,我可以将所有这些对象存储在堆中。
如果我想在一个区域模拟 200 万用户怎么办?还是数百个区域的?
有了这么多数据,数据库是使用这些信息进行模拟的唯一可行方法吗?
I am having a memory problem that I completely understand the cause, but have no idea of a fix. I have attempted to use the -Xmx2g tag and make the heap size larger, but there seems to be a hidden maximum. (if i used -Xmx512m I run out of space at the same time).
Assume I have 2 objects, an Area and a User. My Area object holds an ArrayList of users:
public class Area {
int numUsers;
ArrayList<User> userList;
}
My User class holds an ArrayList of friends:
public class User {
int userID;
int numFriends;
ArrayList<User> friends;
}
Just using a single Area, with 1 million users, and an average of 200 friends per user, I run out of heap space after about 680,000 Users are created. Obviously if I lower the average number of friends/user to something closer to 100, I can store all of these objects in the heap.
What if I want to simulate 2 million Users in an area? Or Hundreds of area's?
With this much data, is a database the only feasible way to do simulations using the information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当您能够承受 100.000 倍的随机访问性能损失时,磁盘/数据库才是一种解决方案(您可能有很多系统使用数据库)。使用专门的数据结构可以做得更好。对完全连接的子网做一些特殊的事情可能会节省大量空间。
Disk/database is only a solution if you can afford the factor 100.000 loss of random access performance (you might, there are lots of systems using a database). You can do much better with specialized data structures. Doing something special for fully connected subnetworks might save a lot of space.
当然,您可以在 64 位 java 上运行超过 2m,但这并不能解决问题。顺便说一句,对于区域,您可能需要 id (而不是 numUsers),用户/朋友的数量可以从 list.size() 获得。
数据库/磁盘存储是表示大量对象的自然解决方案,您也可以使用 cluster服务器(除了运行一个带有 500+GB 内存的巨大盒子之外)
要回答这个问题,您必须提供更多数据:区域/朋友图/等的意义是什么。
如果您可以使用 ByteBuffer 编写自己的结构(这可能不是一件容易的事),您可以通过 java.io.MappedByteBuffer、ScatteringByteChannel/GatheringByteChannel 超越 32 位限制。然而,无论如何,这都不是一个新手任务,但如果您喜欢编程,我建议您尝试一下。
祝你学业顺利。
You can, of course, run w/ more than 2m on 64bit java but that will not solve the issue. Btw, for Area, you probably need id (not numUsers), the number of users/friends can be obtained from list.size()
Database/disk storage is a natural solution for representing a lot of object, you can alternatively use cluster of servers (beside running a huge box w/ 500+GB of memory)
To answer the question you have to supply some more data: what is the point of areas/ friend graphs/etc.
If you can code your own struct(ure) using ByteBuffer (which is probably not an easy task) you can go beyond the 32bit limitations by java.io.MappedByteBuffer, ScatteringByteChannel/GatheringByteChannel. However, it's not a rookie task at any rate but if you like programming, I'd advise to try your hand at.
I wish you good luck with your studies.
要访问更多堆内存,请迁移到 64 位操作系统和 64 位 JVM。如果您遇到 -Xmx512m 问题,则您正在使用 32 位操作系统和/或 JVM。
To access more heap memory, move to a 64-bit OS and 64-bit JVM. If you are hitting problems with -Xmx512m, you are using 32-bit OS and/or JVM.