Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to (if it possible) limit amount of allowed memory for code block?

Status
Not open for further replies.

alebu

Programmer
Sep 7, 2002
46
Hi all, is it possible to limit somehow allowed memory for some code block (method for example)? For example, I am going to run some code but it is possible that it can try to use too much memory (heap or stack). The practical usage of such limitation can be: preventing some plugin to suck all possible for JVM memory or stack memory and this way preventing the situation where custom code on oput of memory will kill a whole application. I mean, I am interested in something like (just free typing now):
//set limitation for heap and stack memory
try{
// run some code with applied limitations
}catch( some_special_exception ex){
// well, do something...
}
As I know it is not possible for now but maybe there are some strategies which may help at least somehow.
 
I don't think you can control that: heap is common for all JVM so maybe the buggie code gets all the memory and some piece of healthy code throws the exception.

May advice: code it OK and you won't consume all the memory.

Cheers,
Dian
 
Heh, it was actually more theorical question. Imagine, today we have a huge zoo of application servers and any custom component can actually kill a whole app-server with simple endless loop. The same goes to any IoC container. A simple, 100 of years know bug with endless loop can kill any application. Thats bad I think espesially for modular applications, like different web frameworks, CMS systems, app-servers, etc. I wonder if such a functionality will be implemented in the future? We have classloding, so why not to have memory boxes or something like that. Maybe even some different process running (maybe from pool of processes)may be used with IPC. Just wondering ...
 
Simple sollution: don't do endless loops.

Anyway, application servers have a lot of ways to control performance. A single thread cannot block them.

Cheers,
Dian
 
If you plan to use Java to do a task that needs very huge amount processing and it may take several days to finish the task, you may divide the big task into several smaller subtasks.

The results saved on harddisk from subtasks may help you a lot because a huge task may create runtimeexception if there is lot s of memory leak in running your program.

If I have 4 subtasks A, B, C, D. It is easier to trace the problem occurs in which part. You may reboot the computer if it is a pc and Windows platform after finishing each subtask to make sure the computer is at the best performance.
 
The results saved on harddisk from subtasks can save you time.

Assume my subtasks are in this sequence: A->B->C->D
If there is no runtimeexception, I can get the result from D.
If there is runtimeexception in C, I can use the result from B after I have solved the problem in C.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top