When i run this code i get System.OutOfMemoryException: ‘Out of memory.’ when it tries to load a new form. This is the error i get An unhandled exception of type ‘System.OutOfMemoryException’ occurred in System.Drawing.dll
I tried to fix the issue on my own several times but i get the same error every time. In debugger it says that its using 37MB of memory. This is the code of all relevant functions
public static async Task<List<Meni>> RetrieveAll()
{
using (var handler = new LoggingHttpClientHandler())
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromSeconds(5);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Console.WriteLine("Sending request");
var response = await client.GetAsync($"https://apex.oracle.com/pls/apex/student12345/Meni/Get");
Console.WriteLine("Request sent. Awaiting response...");
if (Convert.ToInt32(response.StatusCode) == 200)
{
JObject menijson = JObject.Parse(await response.Content.ReadAsStringAsync());
JArray meniji = (JArray)menijson["items"];
List<Meni> list = meniji.ToObject<List<Meni>>();
foreach (Meni meni in list)
{
await meni.dodajjela();
}
return list;
}
else
{
MessageBox.Show("Greška pri prustupanju popisa menija", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
}
private async Task dodajjela()
{
using (var handler = new LoggingHttpClientHandler())
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromSeconds(5);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Console.WriteLine("Sending request");
var response = await client.GetAsync($"https://apex.oracle.com/pls/apex/student12345/Meni/GetRelated?men={this.id_menija}");
Console.WriteLine("Request sent. Awaiting response...");
if (Convert.ToInt32(response.StatusCode) == 200)
{
JObject jelajson = JObject.Parse(await response.Content.ReadAsStringAsync());
JArray jela = (JArray)jelajson["items"];
foreach (var jel in jela)
{
var resp = await Jela.Retrieve(jel["idjela"].ToString());
this.jelas.Add(resp);
}
}
}
}
public static async Task<Jela> Retrieve(string id)
{
using (var handler = new LoggingHttpClientHandler())
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromSeconds(5);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Console.WriteLine("Sending request");
var response = await client.GetAsync($"https://apex.oracle.com/pls/apex/student12345/Meni/Jelo?je={id}");
Console.WriteLine("Request sent. Awaiting response...");
if (Convert.ToInt32(response.StatusCode) == 200)
{
JObject jelajson = JObject.Parse(await response.Content.ReadAsStringAsync());
jelajson.Remove("links");
return CreateObj(jelajson);
}
return null;
}
}
private static Jela CreateObj(JObject objekt)
{
Image slikajelo = null;
using (WebClient client = new WebClient())
{
byte[] slikabiteovi = client.DownloadData($"https://apex.oracle.com/pls/apex/student12345/Meni/image?br={objekt.Property("id_jelo").Value.ToString()}");
using (MemoryStream ms = new MemoryStream(slikabiteovi))
{
slikajelo = Image.FromStream(ms);
}
}
Jela jelo = new Jela
{
id_jela = objekt.Property("id_jelo").Value.ToString(),
slika= slikajelo,
ime_jela= objekt.Property("ime_jela").Value.ToString()
};
return jelo;
}
New contributor
DorianK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.