Solved

java memory allocation de-allocation

Posted on 2004-09-27
9
404 Views
Last Modified: 2010-03-31
I was wondering how to do memory de-allocation in java. Here is the problem that i ran into. I have array of vectors of size around 5000. I store words in these vectors. Each  vector has around 5000 words. so as i keep readin the values in to vectors, i get out of memory excepton at array size around 700.... i have the set the virtual memory size to the max... to be specific -Xmx1570m I was wondering if i could deallocate the memory of the vectors  every 768 times . Please lemme know if i am not clear or need to give additional info.

thanks in advance
0
Comment
Question by:rajiv11
  • 5
9 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 50 total points
ID: 12162871
To deallocate memory (=heap objects) just stop referencing them, put their reference on null
or forget them if referenced from an exited method.
Whenever you are in need for memory, JAVA looks which objects are no longer "looked at".

It proves better than "deleting" them (in C++).
;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12162972
Btw., -Xmx1570 is about the maximum presently.
You might calculate if you really need it as a peek or your performance will suffer.

   Runtime rt = Runtime.getRuntime();  // Our runtime administration.

   long memoryInUse = rt.totalMemory();

Might help to monitor memory use on the fly.

;JOOP!
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12166356
To dereference entire arrays, just call
   array.clear();

To dereference the ith item in the array,
   array.set(i, null);

To suggest that the VM do a garbage collection, just do
  System.gc();
This is just a suggestion, the VM may choose to NOT garbage collect.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 24

Expert Comment

by:sciuriware
ID: 12166750
Never do System.gc() unless you know that the garbage collector will obey.

Just, like I said, release references.
By the way:

    Vector huge = new Vector();   // If this one consumes a lot memory

          huge = null;     // Will release everything, unless referenced also from elsewhere.

    Object[] alot = new Object[1000000];  // The same for arrays ...

         alot = null;        // Will ... just the same.
;JOOP!
0
 
LVL 1

Expert Comment

by:KartikShah
ID: 12168708
>    Vector huge = new Vector();   // If this one consumes a lot memory
>    huge = null;     // Will release everything, unless referenced also from elsewhere.

Before referenceing the vector to null, do call the clear method on the vector, this will release the references held by the vector. This is a precautionary step to  avoid cyclic reference. so before:

huge = null;

make the following call:

huge.clear();

>    Object[] alot = new Object[1000000];  // The same for arrays ...
>    alot = null;        // Will ... just the same.

If that does not help enough, you can also try to remove the references assigned to each of the array indexes.

Hpe it helps

cheers,

Kartik
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12169184
KartikShah, please first read an article about the garbage collector before you start
to write nonsense about cyclic references.

The present gc is smart enough to delete to objects referencing only each other.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12402691
Honored.

;JOOP!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 88
Checkbox and ListView in Android Layout 4 56
couple of eclipse 5 25
MySqlDump not dumping triggers 1 17
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now