删除Java中的一部分数组以释放堆中的内存 [英] Deleting Part of An Array in Java to Free Memory on Heap

查看:782
本文介绍了删除Java中的一部分数组以释放堆中的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Java中的背包问题实现一个动态编程算法。我声明数组,然后将其大小初始化为[数量对象] [背包容量]。

I am implementing a dynamic programming algorithm for the knapsack problem in Java. I declare the array and then initialize its size to be [number of objects][capacity of knapsack].

当我的对象数量或我的容量变得太大时,由于堆空间不足而导致内存错误。我的问题是:如果我从我的double数组中删除行,Java会在我删除时释放内存吗?或者Java为我最初创建的数组的大小预留内存空间?如果是后者,有没有办法在Java中手动释放内存?

When my number of objects or my capacity gets too large, I get a memory error because I run out of space on the heap. My questions is: If I delete rows from my double array as I go along, will Java free the memory as I delete? Or does Java reserve that memory space for the size of the array I originally created? If it's the latter, is there a way to manually free the memory in Java?

感谢您的帮助!



解决方案
The short answer is "yes" - if you're using arrays like this:

$ c> private void foo(){
int [] [] bar = new int [10] [10];
bar [1] = null;
//此时,bar [1]中的数组有资格进行垃圾回收
//除非其他人有引用
}

private void foo () { int[][] bar = new int[10][10]; bar[1] = null; //at this point, the array that was in bar[1] is eligible for garbage collection //unless someone else has a reference to it }

这篇关于删除Java中的一部分数组以释放堆中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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