简体   繁体   English

如何估计Java中数据结构/变量的内存大小?

[英]How to estimate the memory size of data structures/variables in Java?

I'm doing a project for a class where I'm supposed to implement the algorithms described in a research paper and run the experiments described in the paper. 我正在为一个班级做一个项目,我应该在该类中实现研究论文中描述的算法并运行论文中描述的实验。 The paper is related to shortest-path queries, so I'm using the JUNG library; 本文与最短路径查询有关,因此我正在使用JUNG库。 the paper and the datasets used for the experiments are found here . 纸和用于实验的数据集被发现在这里

Anyway part of the experiments involves comparing the memory required for the data structures used by the algorithm (a tree derived from the original graph and some info on the shortest paths) to that used by data structures in other algorithms when they're run on the same graph. 无论如何,实验的一部分涉及将算法所使用的数据结构(从原始图形派生的树和最短路径上的一些信息)所需要的内存与在其上运行的其他算法中的数据结构所使用的内存进行比较。同一张图。 The paper's author wrote the code in C++, but my prof let us choose which language to use for this project so I chose Java as I've used it a lot more and therefore code faster in it... But now I'm not sure how to figure out this memory usage thing. 该论文的作者使用C ++编写了代码,但是我的教授让我们选择了用于该项目的语言,所以我选择了Java,因为我已经使用了更多的语言,因此在其中编写代码的速度更快...但是现在我还没有确定如何弄清楚内存使用情况。

I found a few questions asking similar questions but several were old (4 or 5 years) and others didn't seem to be asking quite the same question; 我发现有几个问题在问类似的问题,但有几个是古老的(4或5年),而另一些似乎并没有问完全相同的问题。 they were calculating the size of structures that wouldn't change. 他们正在计算不会改变的结构的大小。 So I'm hoping someone can point me to an algorithm, method, or even better a library that would give a good estimate for this. 因此,我希望有人可以为我提供一种算法,方法,甚至更好的库,可以对此提供一个很好的估计。 I don't think it needs to be exact, but I need some sort of estimate at least. 我不认为它是准确的,但至少我需要某种估计。

You want to look at various SizeOf implementations: 您要查看各种SizeOf实现:

However, this question seems to have a better answer. 但是, 这个问题似乎有更好的答案。

I think that depends of implementation of Java Virtual Machine. 我认为这取决于Java虚拟机的实现。 In the document http://java.sun.com/docs/books/jni/download/jni.pdf or in http://en.wikipedia.org/wiki/Java_Native_Interface the types mapping to C++ to Java. 在文档http://java.sun.com/docs/books/jni/download/jni.pdfhttp://en.wikipedia.org/wiki/Java_Native_Interface中 ,类型映射到C ++到Java。

By example, 举例来说,

a java boolean type is unsigned char jboolean unsigned 8 bits java boolean类型为unsigned char jboolean unsigned 8位

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

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