Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java memory allocation de-allocation

Posted on 2004-09-27
9
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
9 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

610 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