77

java.lang.OutOfMemoryError. Consider increasing the value of $(JavaMaximumHeapSize). Java ran out of memory while executing 'java.exe'

I am getting out of memory exception in my visualstudio Xamarin Project Please help me how can i resolve this issue.

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
Srinivas Ch
  • 1,176
  • 2
  • 17
  • 30
  • 2
    I would like to recommend not only to set LargeHeap to true. Instead you should try to figure out what is causing that OOM and fix that. Otherwise you might have bought you some time until it will hit you again. – tequila slammer Apr 25 '16 at 13:31

9 Answers9

125

I would increase your build heap size.

Right click your Android Project > Properties > Android Options > Advanced > Java Max Heap Size.

enter image description here

Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Iain Smith
  • 7,855
  • 4
  • 42
  • 57
21

The selected answer lead me in the right direction, but for Visual Studio 2017, the screen looks like:

VS 2017 Updated Screens

VS 2017 Updated Screens

mrogunlana
  • 679
  • 7
  • 9
18

If you want to increase Heap size of your application (not the build process) you can set this in your AndroidManifest.xml:

<application android:largeHeap="true"></application>
Matt
  • 4,283
  • 1
  • 20
  • 42
12

For visual studio 2015 right click Project --> Properties --> Android Options --> Advanced and heap size there.

DavidM
  • 121
  • 5
  • Make that: left-click .Droid --> Double-click 'Properties' --> Click 'Android Options' on the left --> Click 'Advanced' tab --> "Java Max Heap Size" – LionelGoulet Nov 04 '16 at 16:42
7

As of Xamarin studio version 6.3 and visual studio 7.0.1 the option to increase heap size can be found here

Right click on the android project then choose options and choose, Build/Android Build, then Advanced tab and set 1G (or something) in Java heap size

enter image description here

Durai Amuthan.H
  • 28,889
  • 6
  • 148
  • 223
4

When you run out of 1G heap size, start to optimize resources.

First check if you are not duplicating images instances, keeping same image in memory several times. For that use ffimageloading library (https://github.com/luberda-molinet/FFImageLoading) get it on nuget fast and easy.

Then optimize images you are using. For large images, other than icons, try converting everything to jpeg avoiding png with transparency, that once saved me for constant ooms.

Then try using thumbnails instead of images when you might have large uploaded images on remote. On your server pre-create thumbnails for different sizes, mini, small, medium, normal , large, xlarge etc, and load only images of size you really need. That saves a lot of memory and speed.

Another one is memory leaks. You might have cells or other elements still reside im memory if they didn't unsubscribe from messages, eventhandlers and some other. Imagine you could have several copies of a list full of images still in memory when you are sure that couldn't happen. More info here:

https://forums.xamarin.com/discussion/123876/should-we-unsubscribe-all-events-for-memory-management

https://forums.xamarin.com/discussion/87206/messaging-center-unsubscribe-from-all-messages-when-broadcast-stops

Nick Kovalsky
  • 3,015
  • 15
  • 35
2

In my experience, memory issues ONLY happen on the android version of the Xamarin.Forms app. And it always have to deal with large-size images. I've dealt with this problem by using FFImageLoading(https://github.com/luberda-molinet/FFImageLoading)

Replace your

<Image Source="waterfront.jpg" />

With

<ffimageloading:CachedImage Source="waterfront.jpg"
DownsampleToViewSize = true,
BitmapOptimizations = true/>

You will see a drastic improvements in performance, and it will also solve your memory problems.

Dylan Liu
  • 123
  • 7
1

Increasing java max heap size works for me

enter image description here

fulgen
  • 181
  • 2
  • 2
  • 12
-1

I had fixed adding below in application tag of AndroidManifiest.xml

<application  android:largeHeap="true" android:label="sample"></application>
Chitu
  • 193
  • 2
  • 14