Solved

java memory allocation de-allocation

Posted on 2004-09-27
9
406 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 103
Java Timer (static) 9 43
object oriented programming comparison 5 69
expectj telnet failing 5 35
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

785 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