dcsimg
Controlled memory usage with collections
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   gaurav_saini
Posted On:   Saturday, April 16, 2005 12:24 AM

I have a piece of code like : func() { List obj = new ArrayList(); for (int i=0;i <100;i++) { obj.add(new Customer(name,age,salary)); } // obj.clear(); } I have select between the choices (so that the memory is released and the performance is best): 1. I can use clear() to remove all objects from the list, so that the memory is released explicitly and the list is no longer acquiring those references 2. I don't use clear() and when the function terminates the JVM has to invoke GC to look for the unused references and release the memory. I am not able to make out the best choice from response time and memory usage point o   More>>

I have a piece of code like :

			
func() {
List obj = new ArrayList();
for (int i=0;i <100;i++) {
obj.add(new Customer(name,age,salary));
}
// obj.clear();
}

I have select between the choices (so that the memory is released and the performance is best):

1. I can use clear() to remove all objects from the list, so that the memory is released explicitly and the list is no longer acquiring those references

2. I don't use clear() and when the function terminates the JVM has to invoke GC to look for the unused references and release the memory.

I am not able to make out the best choice from response time and memory usage point of view..

Plz help.

Thanks

   <<Less

Re: Controlled memory usage with collections

Posted By:   Anonymous  
Posted On:   Friday, July 15, 2005 05:18 AM

Hi Gaurav...,
I guess both java and .net would keep the memory space in both the cases that u've mentioned here, untill the GC is called to perform the clean up function.I'm not sure if java Gc also has something similar to the .net's freachable Q..if u know more on this as well as the memory mgt and mem architecture of java please help me out by providing the details or a link where i can get those details.
TIA
Jits

Re: Controlled memory usage with collections

Posted By:   Stephen_Ostermiller  
Posted On:   Sunday, May 15, 2005 09:51 PM

Both do the same thing. When the list goes out of scope (at the end of the function because it is declared as a local variable) it and everything in it becomes available for reclaimation by the garbage collector. If you clear the list, then the objects itself is released for garbage collection, but it still takes up space until the garbage collector runs.
About | Sitemap | Contact