Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Question asked in my interview that what will you do if you get an out of memory exception

 
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked a question in an interview that what will you do if you get an out of memory exception. I said that first I will check in the logs of the catch block and see which line is causing this exception and then see whether it is a coding issue (e.g someone mistakenly creating objects inside infinite loop ) or whether it is not a coding issue. If not a coding issue then see  if the memory requirement itself is too much say for example for Spark in memory programs. Is this answer correct ?
Thanks
 
Saloon Keeper
Posts: 22630
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally in Java, an OOM is fatal. Because the very act of handling an OOM generally requires memory, and if you have no memory then...

Some systems pre-allocate an emergency memory pool for this purpose, but, as I said, Java isn't usually managed that way.

Of course, that's assuming that the question really meant "what should the program do?" In real life, we accept that it died, people yell at each other, then the system is re-configured with extra memory allocated and/or someone figures out what bug in the app demanded an unexpected amount of memory.
 
Marshal
Posts: 25927
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finding out what line of code triggered the OOME might be helpful, sure. But on the other hand it might be that some other code had mostly filled up the memory already and that line of code just happened to go over the limit.

The main question is "What is using up all of the memory?" So to find the answer to that, clearly it would be nice to see what kind of objects are in the memory when it's all used up. And for that, there's a tool called "heap dump" which tells you that. If it were me I wouldn't spend a lot of time looking at the code, unless it was very simple code which just happened to try allocating an array with 100 million entries. I would try to arrange for a heap dump to be produced while the code is running and then see what it says.
 
Bartender
Posts: 2697
130
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:... someone mistakenly creating objects inside infinite loop..


If I get an OOM Exception in prod, I would first suspect the data and then the code. The usual suspects are db returning a million records when it should have returned a handful or a file being read line by line contains a 10gb of data ! Basically anything out of the ordinary. The next culprit would be code. Typically, this would be when an object does not clear its data between transactions and keeps growing unchecked or a memory leak.
In an interview, I would detail out the actions I would take. This includes monitoring the total memory and then allocating more memory (temporarily of course) till I find the root cause of the problem. Sometimes a simple restart works wonders
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

Monica Shiralkar wrote:... someone mistakenly creating objects inside infinite loop..


The usual suspects are db returning a million records when it should have returned a handful or a file being read line by line contains a 10gb of data


Thanks
In that case , would one try providing more memory by specifying in the command line argument ?
 
Marshal
Posts: 70598
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . would one try providing more memory by specifying in the command line argument ?

It's an interview; you tell us whether you would or wouldn't provide more memory.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It's an interview; you tell us whether you would or wouldn't provide more memory.



Yes , I would try increasing the memory by providing as argument. But I am not sure about how much memory should I increase to.

Another example I think is Spark. If we are running spark code since it is in memory processing ,it will require lot of memory .
 
lowercase baba
Posts: 12906
63
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing I'd do is damage control and mitigation.

Does this app need to be restarted immediately?  Are there corrupt files/records that need to be cleaned?  Who needs to be notified the app is down?

If I restart it, how long do i think i might have before it crashes again - and can i do a planned restart every X days/hours?

First priority should always be mitigation of the issue - nothing else matters.  

 
Paul Clapham
Marshal
Posts: 25927
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Campbell Ritchie wrote:It's an interview; you tell us whether you would or wouldn't provide more memory.



Yes , I would try increasing the memory by providing as argument. But I am not sure about how much memory should I increase to.



If you're looking for a rule which says "Given no information at all about a program which ran out of memory, how much more memory should I give it?", there isn't any such rule.
 
Campbell Ritchie
Marshal
Posts: 70598
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . there isn't any such rule.

. . . and it won't work at all well if you move onto an old PC with less memory.
 
Paul Clapham
Marshal
Posts: 25927
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are plenty of other questions one could ask to help out the decision-making process. Remember, in real life you aren't given all of the information you need when a problem occurs. You have to search it out.

For example: Has this application run out of memory before? If so, how often? Does it happen regularly? Frequently? What action was taken when it happened? Or if not, what new things has it been asked to do? (You probably won't get an answer to that if the actions come from external users, but if they came from internal changes then you might.)
 
Rancher
Posts: 212
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I encountered this problem in an application I was working on, while building a syntax tree to store language details.

What I did was first analyze the problem and discover what is taking the most memory. You can do this in many different ways, by either code analysis or memory benchmarking (you can get the current amount of memory used by Java). Then you need adjust the design based on what you find. The way I did that was changing all of my objects from immutable to mutable, because I found that when I needed to change a value, it had to copy everything and basically took up 2x the required memory.

You can look for tricks to minimize memory usage for specific places as well. If most of your data are Strings, maybe you want to cache the Strings and represent them as a small String like {$STR_1} or something.

You can increase allowed memory usage of Java by passing in -Xmx1g or something similar. Of course remember that this sometimes only covers up a problem rather than fixes it.

 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I once worked on a multi threaded application which was giving out of memory exception. This was solved by increasing the heap memory by providing in the command line arguments. But I had just increased little and checked , then again increase little and tried .I had no idea how much should I actually be increasing it.All I knew was that increasing it would resolve the issue .
 
fred rosenberger
lowercase baba
Posts: 12906
63
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:All I knew was that increasing it would resolve the issue .


but that may only be a temporary solution...if a pipe is leaking, and the bucket that catches the drips is replaced with a bigger bucket, you still have a leaky pipe.

If you have a memory leak, you will still eventually run out, no matter how much more memory you give the JVM.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Monica Shiralkar wrote:All I knew was that increasing it would resolve the issue .


but that may only be a temporary solution...if a pipe is leaking, and the bucket that catches the drips is replaced with a bigger bucket, you still have a leaky pipe.

If you have a memory leak, you will still eventually run out, no matter how much more memory you give the JVM.



But this leaky pipe example will be only for the case where the out of memory exception is because of memory leak.What if memory leaks are not there and despite that we get out of memory error ?
 
Paul Clapham
Marshal
Posts: 25927
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, again, you have to investigate whether the memory error was a fluke or whether it was more systematic. In any case you need to find out the cause of the problem.
 
Master Rancher
Posts: 4698
49
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:
But this leaky pipe example will be only for the case where the out of memory exception is because of memory leak.What if memory leaks are not there and despite that we get out of memory error ?



One of my clients, many years ago, had an issue with their app occasional hitting an Out of Memory error.
They processed XML documents, and sometimes they would get a document that was a bit larger than the norm, and as time went on this occurred more frequently. Essentially the average size of docs they had to process was increasing over time as more people used their system.

Now, they had increased memory to get over this a couple of times, but it would still creep up on them a few months later.

In this case the solution was not to keep adding memory, but to spend a couple of days looking at how the documents were processed (yes, they were being loaded into memory and then processed), and change the processing to a stream, so the whole thing was not stuck in memory.

So, this was not a memory leak, and yes they could have solved it by increasing memory each time they hit the wall, but the better solution was to simply modify the way stuff was handled so it never hit the wall in the first place.
 
fred rosenberger
lowercase baba
Posts: 12906
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:But this leaky pipe example will be only for the case where the out of memory exception is because of memory leak.What if memory leaks are not there and despite that we get out of memory error ?


Ok...different example.  If you have a trash can that gets filled up, you can replace it with a bigger trash can, but that too will eventually fill up if you don't empty it.

If you have a dinner table and you keep putting dishes of food on it, never clearing off the old ones, eventually the table will be full no matter how big the table is.

If you have ANY kind of size limit on anything, and your input is greater than your output, eventually you will run out.

My point is that simply increasing the size is quite probably NOT a long term solution.  You need to look and see WHY you are running out of memory.  You're holding onto stuff you don't need, or the size of the data is growing (like in Dave's example), or data is coming in faster than it used to...but until you take the time to investigate WHY, you're just kicking the can down the road.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote: You're holding onto stuff you don't need,



But wouldn't that be taken care of during automatic garbage collection ?
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote: the size of the data is growing (like in Dave's example), or data is coming in faster than it used to...but until you take the time to investigate WHY, you're just kicking the can down the road.



And what can be done in case of these cases ?
 
Dave Tolls
Master Rancher
Posts: 4698
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:
And what can be done in case of these cases ?



Depends on the result of any analysis done on the issue.
And yes, sometimes you simply need more stuff (memory, disk space, processors etc etc).
 
salvin francis
Bartender
Posts: 2697
130
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are still a few more cases to consider that's not discussed in this thread. e.g. a third party library having a memory leak after some odd set of transactions, etc...
One can keep adding more cases to this hypothetical situation. There is no single rule on what to do in this situation.

As with any production issue, the more you dig into the logs and metrics of the system, the more clues you get to solve the particular issue.

As an interviewer, If I ask this question, my aim would be to get a conversation about two items:
1. If the candidate has faced this issue in the past, how has he solved it
2. How will he use this experience to solve this issue in the future.

 
reply
    Bookmark Topic Watch Topic
  • New Topic