Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce array size without creating new?

I am using array for storing a kind of object. I created an array of some fixed size

int arr[]=new int[n];

Now after processing this array i want to free upto 75% of the memory from this array(now only n/4 elements are useful). So what my question is, since n is very large and i wish not to hold larger memory than useful, How can i reduce size of array at runtime without copying to new array of size n/4(Is it even possible or not?).

like image 564
iprashant Avatar asked Oct 02 '15 09:10

iprashant


1 Answers

It's not possible. You cannot change the length of an array after you initialise it.

What you can do is create another array with suitable size and make this large array eligible for Garbage Collector.

Best is to use ArrayList if you are allowed to do that.

like image 106
Suresh Atta Avatar answered Oct 19 '22 10:10

Suresh Atta