Solved

Memory allocation in java programatically

Posted on 2006-06-22
14
372 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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