Solved

java memory allocation de-allocation

Posted on 2004-09-27
9
413 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
What's wrong with this code? 4 36
Java Inheritance super keyword use 8 40
netbeans can not run self signed app 8 14
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

730 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