Solved

Memory allocation in java programatically

Posted on 2006-06-22
14
370 Views
Last Modified: 2012-08-14
Hi All,

Any clues on how to allocate memory in java at runtime?
I mean suppose in my main method if I want to say that "this program should run with memory of 512MB" is there any way programatically
We use command like

java -Xms512M -Xmx512M abc

Is there anyway to implement it in your java program?

Thanks.

Rgds,
Arpit
0
Comment
Question by:arpit080399
[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
  • 4
  • 2
  • +2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16958249
No
0
 
LVL 92

Expert Comment

by:objects
ID: 16958250
no, you need to do it from the command line as you already are.
0
 

Author Comment

by:arpit080399
ID: 16958268
ok let me explain the problem I have suppose you guys can give me a better solution

I am facing problem while parsing large XML file. filesize is around 35MB and it gives me error of "outofMemoryError" while executing DocumentBuilder.parse command.

now the problem is this particular program is a part of sequence on a larger program, hence cannot be executed standalone as indicated above.
Any ideas on how to tackle this kind of memory allocation errors?

Thanks
0
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!

 
LVL 92

Expert Comment

by:objects
ID: 16958282
if it is ruunninmg in the same vm as the larger program then you need to specify the required memory size when you start that program.
You wouldn't need to change mx, just mx.

If its in a different vm , then specify the size when you start the vm
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16958283
Can you use SAX? - if so it will save a lot of resources
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16958466
You should use the SAX parser for files which are big in size and cannot be loaded in memory at one instant - that is what it is meant for and traditionally that is one of its advantages over DOM (and this is one of DOM's disadvantages).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16958470
>> You wouldn't need to change mx, just mx.

:-) ?
0
 

Author Comment

by:arpit080399
ID: 16958683
SAX cannot be used as we have to modify and manipulate the XML file.
ok, so only while starting the program we need to allocate the mx parameter .
Is there any other better way? because i require such high memory only for this one program and dont want to allocate this memory forever as some other applications are also running on this machine.
large program is a timer, hence will always be executed after every 30 mins
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 16958739
> >> You wouldn't need to change mx, just mx.

should have been:

>> You wouldn't need to change mx, just ms.

mx only specified the max required, it will only get allocated when needed.
0
 

Author Comment

by:arpit080399
ID: 16959117
ok objects, you mean that
I should increase my ms parameter to the value i require and mx will be allocated by JVM?
does it mean that if i say
java -Xms2056M abc then 2056MB will always be reserved by JVM whether it is being utilised or no?
0
 
LVL 92

Expert Comment

by:objects
ID: 16959243
no, I mean increase mx to the most you need.
and set ms to the amount your app will typically need.
0
 
LVL 1

Expert Comment

by:sharath_kulal
ID: 16960219
You could use JDOM.
JDOM uses best of DOM and SAX. JDOM supports document modification with less footprint(i.e. doesn't load the whole document in memory)
http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html

hope this helps
0
 

Author Comment

by:arpit080399
ID: 16961016
Thanks objects, Infact what i want to ask is
that suppose if i give command like
java -Xms 1024M -Xmx 2048M abc
then how much memory will JVM always reserve for this program? will it always keep 1024 or 2048 reserved?
I mean to say how does JVM treat ms and mx parameters?
0
 
LVL 92

Expert Comment

by:objects
ID: 16964444
ms specifies the initial size
mx the maximum size

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html
0

Featured Post

Industry Leaders: 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
eclipse buid path vs tomcat lib path 10 37
Cisco ASA: Java web start no go, asdm launcher no go 3 57
Netbeans and org.apache.commons.lang3 issue 3 22
Html split(text) 2 27
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…
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 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 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:

735 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