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

x
?
Solved

Memory allocation in java programatically

Posted on 2006-06-22
14
Medium Priority
?
376 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
Independent Software Vendors: 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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
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