Java 递归调用时占用的内存

Java 递归调用时占用的内存,java,tree,Java,Tree,我在学校开始学习数据结构,我有一个家庭作业,我必须实现一个二进制搜索树,并告诉数据结构占用的内存 我已经创建了我的BST,我相信一切都按照预期工作,但我不知道如何计算使用的内存 这是我为数据结构创建的代码和用于插入的代码: class Node { int key, totalValue; public Node left; public Node right; public Node (int key, int totalValue) { t

我在学校开始学习数据结构,我有一个家庭作业,我必须实现一个二进制搜索树,并告诉数据结构占用的内存

我已经创建了我的BST,我相信一切都按照预期工作,但我不知道如何计算使用的内存

这是我为数据结构创建的代码和用于插入的代码:

class Node {

    int key, totalValue;
    public Node left;
    public Node right;

    public Node (int key, int totalValue) {
        this.key= key;
        this.totalValue = totalValue;
        this.left = null;
        this.right = null;
    }

    public int getkey() {
        return key;
    }

    public int getTotalValue() {
        return totalValue;
    }
}

class Tree {

    private Node root;

    public Tree() {
        this.root = null;
    }

    public Node getRoot() {
        return this.root;
    }

    public void setRoot(Node node) {
        this.root = node;
    }
}
private static void addNode(Node node, Node tempNode) {

    if (tempNode.getkey() < node.getkey()) {
        if (node.left == null) {
            node.left = tempNode;
        } else {
            addNode(node.left, tempNode);
        }
    } else if (tempNode.getkey() > node.getkey()){
        if (node.right == null) {
            node.right = tempNode;
        } else {
            addNode(node.right, tempNode);
        }
    }else{
        node.totalValue += tempNode.getTotalValue();
    }
}
这是插入的代码:

class Node {

    int key, totalValue;
    public Node left;
    public Node right;

    public Node (int key, int totalValue) {
        this.key= key;
        this.totalValue = totalValue;
        this.left = null;
        this.right = null;
    }

    public int getkey() {
        return key;
    }

    public int getTotalValue() {
        return totalValue;
    }
}

class Tree {

    private Node root;

    public Tree() {
        this.root = null;
    }

    public Node getRoot() {
        return this.root;
    }

    public void setRoot(Node node) {
        this.root = node;
    }
}
private static void addNode(Node node, Node tempNode) {

    if (tempNode.getkey() < node.getkey()) {
        if (node.left == null) {
            node.left = tempNode;
        } else {
            addNode(node.left, tempNode);
        }
    } else if (tempNode.getkey() > node.getkey()){
        if (node.right == null) {
            node.right = tempNode;
        } else {
            addNode(node.right, tempNode);
        }
    }else{
        node.totalValue += tempNode.getTotalValue();
    }
}
private static void addNode(节点节点,节点tempNode){
if(tempNode.getkey()node.getkey()){
if(node.right==null){
node.right=tempNode;
}否则{
addNode(node.right,tempNode);
}
}否则{
node.totalValue+=tempNode.getTotalValue();
}
}
我知道对于每个节点,我需要8个字节的2 int,但我不知道每个指针占用多少空间


第二个问题。假设我从10000个键中插入25000个条目。每次插入都将递归使用,直到新节点找到它的“位置”。如何计算占用的内存?

递归方法背后的基本概念是

GetMemoryUsed(node)
{

    if(node is leaf)
        return (node.localMemory)

    return(GetMemoryUsed(node.left) + GetMemoryUsed(node.right) + node.localMemory);
}

其中
node.localMemory
只是特定节点使用的内存量(不包括子节点)

就获取精确的数字而言,以下是一些您可能感兴趣的工具:


您想知道数据结构单独占用的内存量(堆使用情况)还是还需要知道用于插入的内存量(堆栈使用情况)?@MikeSamuel两者都需要。我遵循了你提供的链接,但不幸的是,我不知道每个指针将占用多少空间(8字节???),因此我可以计算每个指针的数量node@Favolas-尽管Sam I am投了非常恼人的反对票,但我绝对鼓励您查看我在下面给出的链接,以了解代码实际分配的内存。依我拙见PS:我相信大小会因平台而异:32位vs 64位vs@Favolas,引用的内存开销取决于体系结构,因为Java引用通常作为指针实现,指针的大小取决于体系结构。通常指针分别为32b或64b和“64位体系结构”,但许多其他内存模型使用不同的指针大小,并且一些指令集区分近指针和远指针。您至少可以描述这些工具的功能do@paulsm4谢谢我将对此进行研究,但我想知道,使用某种数学公式,数学上精确的答案是“视情况而定”。有人会认为你可以从简单的算术中得到一个好的、确定的答案。对你来说,也许你可以。另外,其他变量可能会影响结果。除了计算答案,您还应该根据经验查看您的实际分配。嗯…@paulsm4谢谢。会看到工具我不知道为什么会被否决。即使您知道字段在特定JVM上的布局方式,并用数学方法进行计算,对齐也是很棘手的,所以您最好根据经验检查您的计算。