Solved

java memory allocation de-allocation

Posted on 2004-09-27
9
401 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

747 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

9 Experts available now in Live!

Get 1:1 Help Now