?
Solved

Memory allocation in java programatically

Posted on 2006-06-22
14
Medium Priority
?
378 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 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